3 #include "snapshotimp.h"
6 #define MSPACE_SIZE ( 1 << 20 )
8 static mspace sStaticSpace = NULL;
11 void *MYMALLOC(size_t size) {
13 static void *(*mallocp)(size_t size);
17 /* get address of libc malloc */
19 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
20 if ((error = dlerror()) != NULL) {
29 createSharedLibrary();
31 if( NULL == sStaticSpace )
32 sStaticSpace = create_mspace_with_base( ( void * )( sTheRecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct Snapshot_t ), 1 );
33 return mspace_malloc( sStaticSpace, size );
37 void MYFREE(void *ptr) {
39 static void (*freep)(void *);
42 /* get address of libc free */
44 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
45 if ((error = dlerror()) != NULL) {
52 mspace_free( sStaticSpace, ptr );
55 static mspace mySpace = NULL;
56 void *malloc( size_t size ) {
57 if( NULL == mySpace ){
58 //void * mem = MYMALLOC( MSPACE_SIZE );
59 mySpace = create_mspace( MSPACE_SIZE, 1 );
60 AddUserHeapToSnapshot();
62 return mspace_malloc( mySpace, size );
65 void free( void * ptr ){
66 mspace_free( mySpace, ptr );
69 void AddUserHeapToSnapshot(){
70 static bool alreadySnapshotted = false;
71 if( alreadySnapshotted ) return;
72 addMemoryRegionToSnapShot( mySpace, MSPACE_SIZE / PAGESIZE );
76 void * operator new(size_t size) throw(std::bad_alloc) {
77 return MYMALLOC(size);
80 void operator delete(void *p) throw() {