On failure returns NULL. */
-xbt_mheap_t mmalloc_attach(int fd, void *baseaddr)
+xbt_mheap_t xbt_mheap_new(int fd, void *baseaddr)
{
struct mdesc mtemp;
xbt_mheap_t mdp;
mdp = (struct mdesc *) mtemp.base;
mdp->fd = fd;
if(!mdp->refcount){
- sem_init(&mdp->sem, 1, 1);
+ sem_init(&mdp->sem, 0, 1);
mdp->refcount++;
}
}
}
}
- /* If the user provided NULL BASEADDR then fail */
+ /* NULL is not a valid baseaddr as we cannot map anything there.
+ C'mon, user. Think! */
if (baseaddr == NULL)
return (NULL);
if (mdp->fd < 0){
mdp->flags |= MMALLOC_ANONYMOUS;
- sem_init(&mdp->sem, 0, 1);
- }else{
- sem_init(&mdp->sem, 1, 1);
}
+ sem_init(&mdp->sem, 0, 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 = mmorecore(mdp, sizeof(mtemp))) != NULL) {
memcpy(mbase, mdp, sizeof(mtemp));
} else {
- THROWF(system_error,0,"morecore failed to get some memory!");
+ fprintf(stderr, "morecore failed to get some more memory!\n");
+ abort();
}
/* Add the new heap to the linked list of heaps attached by mmalloc */
* This is for example useful for the base region where ldl stores its data
* because it leaves the place after us.
*/
-void mmalloc_detach_no_free(xbt_mheap_t md)
+void xbt_mheap_destroy_no_free(xbt_mheap_t md)
{
struct mdesc *mdp = md;
Returns the malloc descriptor on failure, which can subsequently be used
for further action, such as obtaining more information about the nature of
- the failure by examining the preserved errno value.
+ the failure.
Note that the malloc descriptor that we are using is currently located in
region we are about to unmap, so we first make a local copy of it on the
stack and use the copy. */
-void *mmalloc_detach(xbt_mheap_t mdp)
+void *xbt_mheap_destroy(xbt_mheap_t mdp)
{
struct mdesc mtemp, *mdptemp;
mdptemp->next_mdesc = mdp->next_mdesc;
- mmalloc_detach_no_free(mdp);
+ xbt_mheap_destroy_no_free(mdp);
mtemp = *mdp;
/* Now unmap all the pages associated with this region by asking for a
if (__mmalloc_default_mdp == NULL) {
unsigned long mask = ~((unsigned long)getpagesize() - 1);
void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
- __mmalloc_default_mdp = mmalloc_attach(-1, addr);
+ __mmalloc_default_mdp = xbt_mheap_new(-1, addr);
/* Fixme? only the default mdp in protected against forks */
res = xbt_os_thread_atfork(mmalloc_fork_prepare,
mmalloc_fork_parent, mmalloc_fork_child);
{
/* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
// mmalloc_detach(__mmalloc_default_mdp);
- mmalloc_detach_no_free(__mmalloc_default_mdp);
+ xbt_mheap_destroy_no_free(__mmalloc_default_mdp);
}