3 #include "snapshotimp.h"
8 #define REQUESTS_BEFORE_ALLOC 1024
9 size_t allocatedReqs[ REQUESTS_BEFORE_ALLOC ] = { 0 };
12 #if !USE_MPROTECT_SNAPSHOT
13 static mspace sStaticSpace = NULL;
16 /** Non-snapshotting malloc for our use. */
18 void *MYMALLOC(size_t size) {
19 #if USE_MPROTECT_SNAPSHOT
20 static void *(*mallocp)(size_t size);
24 /* get address of libc malloc */
26 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
27 if ((error = dlerror()) != NULL) {
36 createSharedLibrary();
38 if( NULL == sStaticSpace )
39 sStaticSpace = create_mspace_with_base( ( void * )( sTheRecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct Snapshot_t ), 1 );
40 return mspace_malloc( sStaticSpace, size );
44 void *system_malloc( size_t size ){
45 static void *(*mallocp)(size_t size);
49 /* get address of libc malloc */
51 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
52 if ((error = dlerror()) != NULL) {
61 void system_free( void * ptr ){
62 static void (*freep)(void *);
65 /* get address of libc free */
67 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
68 if ((error = dlerror()) != NULL) {
76 /** Non-snapshotting free for our use. */
77 void MYFREE(void *ptr) {
78 #if USE_MPROTECT_SNAPSHOT
79 static void (*freep)(void *);
82 /* get address of libc free */
84 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
85 if ((error = dlerror()) != NULL) {
92 mspace_free( sStaticSpace, ptr );
97 /** This global references the mspace for the snapshotting heap */
98 mspace mySpace = NULL;
100 /** This global references the unaligned memory address that was malloced for the snapshotting heap */
101 void * basemySpace = NULL;
103 //Subramanian --- please make these work for the fork based approach
105 /** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator
106 A bit circumspect about adding an sbrk. linux docs say to avoid using it... */
108 void * HandleEarlyAllocationRequest( size_t sz ){
110 void * returnAddress = sbrk( sz );
111 if( nextRequest >= REQUESTS_BEFORE_ALLOC ){
112 exit( EXIT_FAILURE );
114 allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress;
115 return returnAddress;
120 /** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/
122 bool DontFree( void * ptr ){
123 if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call....
124 if( NULL == ptr ) return true;
125 for( int i = nextRequest - 1; i >= 0; --i ){
126 if( allocatedReqs[ i ] == ( size_t )ptr ) {
134 /** Snapshotting malloc implementation for user programs. */
136 void *malloc( size_t size ) {
137 void * earlyReq = HandleEarlyAllocationRequest( size );
138 if( earlyReq ) return earlyReq;
139 return mspace_malloc( mySpace, size );
142 /** Snapshotting free implementation for user programs. */
144 void free( void * ptr ){
145 if( DontFree( ptr ) ) return;
146 mspace_free( mySpace, ptr );
149 /** Snapshotting realloc implementation for user programs. */
151 void *realloc( void *ptr, size_t size ){
152 return mspace_realloc( mySpace, ptr, size );
155 /** Snapshotting calloc implementation for user programs. */
157 void * calloc( size_t num, size_t size ){
158 void * earlyReq = HandleEarlyAllocationRequest( size * num );
160 std::memset( earlyReq, 0, size * num );
163 return mspace_calloc( mySpace, num, size );
167 /** Snapshotting new operator for user programs. */
170 void * operator new(size_t size) throw(std::bad_alloc) {
174 /** Snapshotting delete operator for user programs. */
176 void operator delete(void *p) throw() {
180 /** Snapshotting new[] operator for user programs. */
182 void * operator new[](size_t size) throw(std::bad_alloc) {
186 /** Snapshotting delete[] operator for user programs. */
188 void operator delete[](void *p, size_t size) {