4 #include "xbt/sysdep.h"
10 extern char *basename (__const char *__filename);
12 #define HEAP_OFFSET 20480000 /* Safety gap from the heap's break adress */
13 #define STD_HEAP_SIZE 20480000 /* Maximum size of the system's heap */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory, mc,
16 "Logging specific to MC (memory)");
18 /* Pointers to each of the heap regions to use */
21 void *actual_heap=NULL;
23 /* Pointers to the begining and end of the .data and .bss segment of libsimgrid */
24 /* They are initialized once at memory_init */
25 void *libsimgrid_data_addr_start = NULL;
26 size_t libsimgrid_data_size = 0;
28 /* Initialize the model-checker memory subsystem */
29 /* It creates the heap regions and set the default one */
32 /* Create the first region HEAP_OFFSET bytes after the heap break address */
33 std_heap = mmalloc_attach(-1, (char *)sbrk(0) + HEAP_OFFSET);
34 xbt_assert(std_heap != NULL);
36 /* Create the sencond region a page after the first one ends */
37 /* FIXME: do not hardcode the page size to 4096 */
38 raw_heap = mmalloc_attach(-1, (char *)(std_heap) + STD_HEAP_SIZE + 4096);
39 xbt_assert(raw_heap != NULL);
43 /* Get the start address and size of libsimgrid's data segment */
44 /* CAVEAT: Here we are assuming several things, first that get_memory_map() */
45 /* returns an array with the maps sorted from lower addresses to higher */
46 /* ones. Second, that libsimgrid's data takes ONLY ONE page. */
52 maps = get_memory_map();
54 for(i=0; i < maps->mapsize; i++){
56 if(maps->regions[i].inode != 0){
58 tmp = xbt_strdup(maps->regions[i].pathname);
59 libname = basename(tmp);
61 if( strncmp("libsimgrid.so.2.0.0", libname, 18) == 0 && maps->regions[i].perms & MAP_WRITE){ //FIXME: do not hardcode
62 libsimgrid_data_addr_start = maps->regions[i].start_addr;
63 libsimgrid_data_size = (size_t)((char *)maps->regions[i+1].end_addr - (char *)maps->regions[i].start_addr);
71 xbt_assert0(libsimgrid_data_addr_start != NULL,
72 "Cannot determine libsimgrid's .data segment address");
77 /* Finish the memory subsystem */
80 mmalloc_detach(std_heap);
81 mmalloc_detach(raw_heap);
85 void *malloc(size_t n)
87 void *ret = mmalloc(actual_heap, n);
89 DEBUG2("%zu bytes were allocated at %p",n, ret);
93 void *calloc(size_t nmemb, size_t size)
95 size_t total_size = nmemb * size;
96 void *ret = mmalloc(actual_heap, total_size);
98 /* Fill the allocated memory with zeroes to mimic calloc behaviour */
99 memset(ret,'\0', total_size);
101 DEBUG2("%zu bytes were mallocated and zeroed at %p",total_size, ret);
105 void *realloc(void *p, size_t s)
111 ret = mrealloc(actual_heap, p,s);
113 ret = malloc(s); /* FIXME: shouldn't this be mmalloc? */
120 DEBUG2("%zu bytes were reallocated at %p",s,ret);
126 DEBUG1("%p was freed",p);
127 // xbt_assert(actual_heap != NULL); FIXME: I had to comment this
128 return mfree(actual_heap, p);
131 /* FIXME: Horrible hack! because the mmalloc library doesn't provide yet of */
132 /* an API to query about the status of a heap, we simply call mmstats and */
133 /* because I now how does structure looks like, then I redefine it here */
137 size_t bytes_total; /* Total size of the heap. */
138 size_t chunks_used; /* Chunks allocated by the user. */
139 size_t bytes_used; /* Byte total of user-allocated chunks. */
140 size_t chunks_free; /* Chunks in the free list. */
141 size_t bytes_free; /* Byte total of chunks in the free list. */
144 extern struct mstats mmstats(void *);
146 /* Copy std_heap to "to_heap" allocating the required space for it */
147 size_t MC_save_heap(void **to_heap)
149 size_t heap_size = mmstats(std_heap).bytes_total;
151 *to_heap = calloc(heap_size, 1);
153 xbt_assert(*to_heap != NULL);
155 memcpy(*to_heap, std_heap, heap_size);
160 /* Copy the data segment of libsimgrid to "data" allocating the space for it */
161 size_t MC_save_dataseg(void **data)
163 *data = calloc(libsimgrid_data_size, 1);
164 memcpy(*data, libsimgrid_data_addr_start, libsimgrid_data_size);
165 return libsimgrid_data_size;
168 /* Restore std_heap from "src_heap" */
169 void MC_restore_heap(void *src_heap, size_t size)
171 memcpy(std_heap, src_heap, size);
174 /* Restore the data segment of libsimgrid from "src_data" */
175 void MC_restore_dataseg(void *src_data, size_t size)
177 memcpy(libsimgrid_data_addr_start, src_data, size);