X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/f2d062a16daf5602b56944a78c51c36ae317a7fa..057f189342acabd431a079455e0800d1abcd8057:/src/mc/mc_memory.c diff --git a/src/mc/mc_memory.c b/src/mc/mc_memory.c index 3f08a5992c..7b191a9348 100644 --- a/src/mc/mc_memory.c +++ b/src/mc/mc_memory.c @@ -7,17 +7,16 @@ #include #define _GNU_SOURCE -extern char *basename (__const char *__filename); +extern char *basename(__const char *__filename); -#define STD_HEAP_SIZE 20480000 /* Maximum size of the system's heap */ +#define STD_HEAP_SIZE 20480000 /* Maximum size of the system's heap */ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory, mc, - "Logging specific to MC (memory)"); + "Logging specific to MC (memory)"); /* Pointers to each of the heap regions to use */ -void *std_heap=NULL; /* memory erased each time the MC stuff rollbacks to the beginning. Almost everything goes here */ -void *raw_heap=NULL; /* memory persistent over the MC rollbacks. Only MC stuff should go there */ -void *actual_heap=NULL; /* The heap we are currently using. Either std_heap or raw_heap. Controlled by macros MC_SET_RAW_MEM/MC_UNSET_RAW_MEM */ +void *std_heap = NULL; /* memory erased each time the MC stuff rollbacks to the beginning. Almost everything goes here */ +void *raw_heap = NULL; /* memory persistent over the MC rollbacks. Only MC stuff should go there */ /* Pointers to the beginning and end of the .data and .bss segment of libsimgrid */ /* They are initialized once at memory_init */ @@ -33,7 +32,9 @@ void MC_memory_init() xbt_assert(std_heap != NULL); /* Create the second region a page after the first one ends */ - raw_heap = mmalloc_attach(-1, (char *)(std_heap) + STD_HEAP_SIZE + getpagesize()); + raw_heap = + mmalloc_attach(-1, + (char *) (std_heap) + STD_HEAP_SIZE + getpagesize()); xbt_assert(raw_heap != NULL); MC_SET_RAW_MEM; @@ -47,120 +48,76 @@ void MC_memory_init() /* Get memory map */ memory_map_t maps; maps = get_memory_map(); - - for(i=0; i < maps->mapsize; i++){ - - if(maps->regions[i].inode != 0){ - + + for (i = 0; i < maps->mapsize; i++) { + + if (maps->regions[i].inode != 0) { + tmp = xbt_strdup(maps->regions[i].pathname); libname = basename(tmp); - + #if 0 - if (maps->regions[i].perms & MAP_WRITE && - maps->regions[i].pathname[0] != '\0' && /* do not take anonymous segments: what are they? */ - strcmp("[heap]",maps->regions[i].pathname) && /* do not take standard heap: mmalloc saves it already */ - strcmp("[stack]",maps->regions[i].pathname) /* this is maestro's stack. No need to save it */ + if (maps->regions[i].perms & MAP_WRITE && maps->regions[i].pathname[0] != '\0' && /* do not take anonymous segments: what are they? */ + strcmp("[heap]", maps->regions[i].pathname) && /* do not take standard heap: mmalloc saves it already */ + strcmp("[stack]", maps->regions[i].pathname) /* this is maestro's stack. No need to save it */ ) { /* FIXME: we should save the data of more segments, in a list of block to save */ } #endif /* for now, we only save the data of libsimgrid (FIXME) */ - if( strncmp("libsimgrid.so.", libname, 14) == 0 && maps->regions[i].perms & MAP_WRITE){ + if (strncmp("libsimgrid.so.", libname, 14) == 0 + && maps->regions[i].perms & MAP_WRITE) { libsimgrid_data_addr_start = maps->regions[i].start_addr; - libsimgrid_data_size = (size_t)((char *)maps->regions[i+1].start_addr - (char *)maps->regions[i].start_addr); + libsimgrid_data_size = + (size_t) ((char *) maps->regions[i + 1].start_addr - + (char *) maps->regions[i].start_addr); xbt_free(tmp); break; - } + } xbt_free(tmp); } } - - xbt_assert0(libsimgrid_data_addr_start != NULL, - "Cannot determine libsimgrid's .data segment address"); - + + xbt_assert0(libsimgrid_data_addr_start != NULL, + "Cannot determine libsimgrid's .data segment address"); + MC_UNSET_RAW_MEM; } /* Finish the memory subsystem */ -void MC_memory_exit() +#include "xbt_modinter.h" +void MC_memory_exit(void) { - mmalloc_detach(std_heap); - mmalloc_detach(raw_heap); - actual_heap = NULL; -} - -void *malloc(size_t n) { - void *ret = mmalloc(actual_heap, n); - - DEBUG2("%zu bytes were allocated at %p",n, ret); - return ret; + if (raw_heap) + mmalloc_detach(raw_heap); } -void *calloc(size_t nmemb, size_t size) -{ - size_t total_size = nmemb * size; - - void *ret = mmalloc(actual_heap, total_size); - -/* Fill the allocated memory with zeroes to mimic calloc behaviour */ - memset(ret,'\0', total_size); - - DEBUG2("%zu bytes were mallocated and zeroed at %p",total_size, ret); - return ret; -} - -void *realloc(void *p, size_t s) -{ - void *ret = NULL; - - if (s) { - if (p) - ret = mrealloc(actual_heap, p,s); - else - ret = malloc(s); /* FIXME: shouldn't this be mmalloc? */ - } else { - if (p) { - free(p); - } - } - - DEBUG2("%zu bytes were reallocated at %p",s,ret); - return ret; -} - -void free(void *p) -{ - DEBUG1("%p was freed",p); -// xbt_assert(actual_heap != NULL); FIXME: I had to comment this - return mfree(actual_heap, p); -} /* FIXME: Horrible hack! because the mmalloc library doesn't provide yet of */ /* an API to query about the status of a heap, we simply call mmstats and */ /* because I now how does structure looks like, then I redefine it here */ -struct mstats - { - size_t bytes_total; /* Total size of the heap. */ - size_t chunks_used; /* Chunks allocated by the user. */ - size_t bytes_used; /* Byte total of user-allocated chunks. */ - size_t chunks_free; /* Chunks in the free list. */ - size_t bytes_free; /* Byte total of chunks in the free list. */ - }; +struct mstats { + size_t bytes_total; /* Total size of the heap. */ + size_t chunks_used; /* Chunks allocated by the user. */ + size_t bytes_used; /* Byte total of user-allocated chunks. */ + size_t chunks_free; /* Chunks in the free list. */ + size_t bytes_free; /* Byte total of chunks in the free list. */ +}; extern struct mstats mmstats(void *); /* Copy std_heap to "to_heap" allocating the required space for it */ size_t MC_save_heap(void **to_heap) -{ +{ size_t heap_size = mmstats(std_heap).bytes_total; - + *to_heap = calloc(heap_size, 1); xbt_assert(*to_heap != NULL); - + memcpy(*to_heap, std_heap, heap_size); - + return heap_size; } @@ -174,7 +131,7 @@ size_t MC_save_dataseg(void **data) /* Restore std_heap from "src_heap" */ void MC_restore_heap(void *src_heap, size_t size) -{ +{ memcpy(std_heap, src_heap, size); } @@ -183,8 +140,3 @@ void MC_restore_dataseg(void *src_data, size_t size) { memcpy(libsimgrid_data_addr_start, src_data, size); } - - - - -