4 static mc_mem_region_t MC_region_new(void *start_addr, size_t size);
5 static void MC_region_restore(mc_mem_region_t reg);
6 static void MC_region_destroy(mc_mem_region_t reg);
8 static void MC_snapshot_add_region(mc_snapshot_t snapshot, void *start_addr, size_t size);
10 static mc_mem_region_t MC_region_new(void *start_addr, size_t size)
12 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
13 new_reg->start_addr = start_addr;
15 new_reg->data = xbt_malloc0(size);
16 memcpy(new_reg->data, start_addr, size);
20 static void MC_region_restore(mc_mem_region_t reg)
22 /*FIXME: check if start_addr is still mapped, if it is not, then map it
23 before copying the data */
24 memcpy(reg->start_addr, reg->data, reg->size);
27 static void MC_region_destroy(mc_mem_region_t reg)
33 static void MC_snapshot_add_region(mc_snapshot_t snapshot, void *start_addr, size_t size)
35 mc_mem_region_t new_reg = MC_region_new(start_addr, size);
36 snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
37 snapshot->regions[snapshot->num_reg] = new_reg;
42 void MC_take_snapshot(mc_snapshot_t snapshot)
47 memory_map_t maps = get_memory_map();
49 /* Save the std heap and the writtable mapped pages of libsimgrid */
50 while(i < maps->mapsize
51 && (maps->regions[i].pathname == NULL
52 || memcmp(maps->regions[i].pathname, "/lib/ld", 7))){
53 reg = maps->regions[i];
54 if((reg.prot & PROT_WRITE)){
55 if(reg.start_addr == std_heap){
56 MC_snapshot_add_region(snapshot, reg.start_addr,
57 (char*)reg.end_addr - (char*)reg.start_addr);
59 }else if(copy || (reg.pathname != NULL
60 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10))){
61 MC_snapshot_add_region(snapshot, reg.start_addr,
62 (char*)reg.end_addr - (char*)reg.start_addr);
63 /* This will force the save of the regions in the next iterations,
64 * but we assume that ld will be found mapped and break the loop
65 * before saving a wrong region.(This is ugly I know). */
72 /* FIXME: free the memory map */
75 void MC_restore_snapshot(mc_snapshot_t snapshot)
78 for(i=0; i < snapshot->num_reg; i++)
79 MC_region_restore(snapshot->regions[i]);
82 void MC_free_snapshot(mc_snapshot_t snapshot)
85 for(i=0; i < snapshot->num_reg; i++)
86 MC_region_destroy(snapshot->regions[i]);