#include <unistd.h> /* Prototypes for lseek */
#endif
#include "mmprivate.h"
+#include "xbt/ex.h"
#ifndef SEEK_SET
#define SEEK_SET 0
mdp->morecore = __mmalloc_mmap_morecore;
mdp->fd = fd;
mdp->base = mdp->breakval = mdp->top = baseaddr;
-
+ mdp->next_mdesc = NULL;
+ mdp->refcount = 1;
+
/* If we have not been passed a valid open file descriptor for the file
to map to, then we go for an anonymous map */
- if (mdp->fd < 0)
+ if (mdp->fd < 0){
mdp->flags |= MMALLOC_ANONYMOUS;
-
+ sem_init(&mdp->sem, 0, 1);
+ }else{
+ sem_init(&mdp->sem, 1, 1);
+ }
+
/* If we have not been passed a valid open file descriptor for the file
to map to, then open /dev/zero and use that to map to. */
if ((mbase = mdp->morecore(mdp, sizeof(mtemp))) != NULL) {
memcpy(mbase, mdp, sizeof(mtemp));
- // mdp = (struct mdesc *) mbase;
} else {
- abort();
- // mdp = NULL;
+ THROWF(system_error,0,"morecore failed to get some memory!");
}
- { /* create the mutex within that heap */
- void *old_heap = mmalloc_get_current_heap();
- mmalloc_set_current_heap(mbase);
- mdp->mutex = xbt_os_mutex_init();
- mmalloc_set_current_heap(old_heap);
- }
+ /* Add the new heap to the linked list of heaps attached by mmalloc */
+ if(__mmalloc_default_mdp){
+ mdp = __mmalloc_default_mdp;
+ while(mdp->next_mdesc)
+ mdp = mdp->next_mdesc;
+ LOCK(mdp);
+ mdp->next_mdesc = (struct mdesc *)mbase;
+ UNLOCK(mdp);
+ }
+
return ((void *) mbase);
}
static struct mdesc *reuse(int fd)
{
struct mdesc mtemp;
- struct mdesc *mdp = NULL;
+ struct mdesc *mdp = NULL, *mdptemp = NULL;
if (lseek(fd, 0L, SEEK_SET) != 0)
return NULL;
mdp = (struct mdesc *) mtemp.base;
mdp->fd = fd;
mdp->morecore = __mmalloc_mmap_morecore;
- mdp->mutex = xbt_os_mutex_init();
+ if(!mdp->refcount){
+ sem_init(&mdp->sem, 1, 1);
+ mdp->refcount++;
+ }
if (mdp->mfree_hook != NULL) {
mmcheckf((void *) mdp, (void (*)(void)) NULL, 1);
}
}
-
- { /* create the mutex within that heap */
- void *old_heap = mmalloc_get_current_heap();
- mmalloc_set_current_heap(mdp);
- mdp->mutex = xbt_os_mutex_init();
- mmalloc_set_current_heap(old_heap);
- }
-
+
+ /* Add the new heap to the linked list of heaps attached by mmalloc */
+ mdptemp = __mmalloc_default_mdp;
+ while(mdptemp->next_mdesc)
+ mdptemp = mdptemp->next_mdesc;
+
+ LOCK(mdptemp);
+ mdptemp->next_mdesc = mdp;
+ UNLOCK(mdptemp);
+
return (mdp);
}