/* Initialization for access to a mmap'd malloc managed region. */
-/* Copyright (c) 2012-2022. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2012-2023. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
mdp->next_mdesc = NULL;
mdp->options = options;
- pthread_mutex_init(&mdp->mutex, NULL);
/* 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. */
while(mdp->next_mdesc)
mdp = mdp->next_mdesc;
- LOCK(mdp);
mdp->next_mdesc = (struct mdesc *)mbase;
- UNLOCK(mdp);
}
return mbase;
}
-
-
-/** Terminate access to a mmalloc managed region, but do not free its content.
- *
- * This is for example useful for the base region where ldl stores its data
- * because it leaves the place after us.
- */
-void xbt_mheap_destroy_no_free(xbt_mheap_t md)
-{
- struct mdesc *mdp = md;
-
- pthread_mutex_destroy(&mdp->mutex);
-}
-
/** Terminate access to a mmalloc managed region by unmapping all memory pages associated with the region, and closing
* the file descriptor if it is one that we opened.
mdptemp->next_mdesc = mdp->next_mdesc;
- xbt_mheap_destroy_no_free(mdp);
struct mdesc mtemp = *mdp;
/* Now unmap all the pages associated with this region by asking for a
* Try to increase this first if you experience strange errors under valgrind. */
#define HEAP_OFFSET (128UL<<20)
-static void mmalloc_fork_prepare(void)
-{
- xbt_mheap_t mdp = NULL;
- if ((mdp =__mmalloc_default_mdp)){
- while(mdp){
- LOCK(mdp);
- mdp = mdp->next_mdesc;
- }
- }
-}
-
-static void mmalloc_fork_parent(void)
-{
- xbt_mheap_t mdp = NULL;
- if ((mdp =__mmalloc_default_mdp)){
- while(mdp){
- UNLOCK(mdp);
- mdp = mdp->next_mdesc;
- }
- }
-}
-
-static void mmalloc_fork_child(void)
-{
- struct mdesc* mdp = NULL;
- if ((mdp =__mmalloc_default_mdp)){
- while(mdp){
- UNLOCK(mdp);
- mdp = mdp->next_mdesc;
- }
- }
-}
-
/* Initialize the default malloc descriptor.
*
* There is no malloc_postexit() destroying the default mdp, because it would break ldl trying to free its memory
xbt_mheap_t mmalloc_preinit(void)
{
if (__mmalloc_default_mdp == NULL) {
- if (!mmalloc_pagesize)
- mmalloc_pagesize = getpagesize();
- unsigned long mask = ~((unsigned long)mmalloc_pagesize - 1);
- void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
+ unsigned long mmalloc_pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
+ unsigned long mask = ~(mmalloc_pagesize - 1);
+ void* addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
__mmalloc_default_mdp = xbt_mheap_new(addr, XBT_MHEAP_OPTION_MEMSET);
-
- // atfork mandated at least on FreeBSD, or simgrid-mc will fail to fork the verified app
- int res = pthread_atfork(mmalloc_fork_prepare, mmalloc_fork_parent, mmalloc_fork_child);
- mmalloc_assert(res == 0, "pthread_atfork() failed: return value %d", res);
}
mmalloc_assert(__mmalloc_default_mdp != NULL, "__mmalloc_default_mdp cannot be NULL");