Initializing variable which contains global snapshotting regions
[model-checker.git] / mymemory.cc~
1
2 #include "mymemory.h"
3 #include "snapshotimp.h"
4 #include <stdio.h>
5 #include <dlfcn.h>
6 #define MSPACE_SIZE ( 1 << 20 )
7 #if !USE_CHECKPOINTING
8 static mspace sStaticSpace = NULL;
9 #endif
10 void *MYMALLOC(size_t size)
11 {
12 #if USE_CHECKPOINTING
13   static void *(*mallocp)(size_t size);
14   char *error;
15   void *ptr;
16
17   /* get address of libc malloc */
18   if (!mallocp) {
19   mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
20   if ((error = dlerror()) != NULL) {
21       fputs(error, stderr);
22       exit(1);
23   }
24   }
25   ptr = mallocp(size);     
26   return ptr;
27 #else
28   if( !sTheRecord ){
29     createSharedLibrary();
30   }
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 );
34 #endif
35 }
36
37 void MYFREE(void *ptr)
38 {
39 #if USE_CHECKPOINTING
40   static void (*freep)(void *);
41   char *error;
42
43   /* get address of libc free */
44   if (!freep) {
45     freep = ( void  ( * )( void * ) )dlsym(RTLD_NEXT, "free");
46     if ((error = dlerror()) != NULL) {
47       fputs(error, stderr);
48       exit(1);
49     }
50   }
51   freep(ptr);
52 #else
53   mspace_free( sStaticSpace, ptr );
54 #endif
55 }
56 static mspace mySpace = NULL;
57 void *malloc( size_t size ){
58         if( NULL == mySpace ){
59                 //void * mem = MYMALLOC( MSPACE_SIZE );
60                 mySpace = create_mspace( MSPACE_SIZE, 1 );
61         }
62         return mspace_malloc( mySpace, size );
63 }
64
65 void free( void * ptr ){
66         mspace_free( mySpace, ptr );
67 }
68