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. */
17 void *MYMALLOC(size_t size) {
18 #if USE_MPROTECT_SNAPSHOT
19 static void *(*mallocp)(size_t size);
23 /* get address of libc malloc */
25 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
26 if ((error = dlerror()) != NULL) {
35 createSharedLibrary();
37 if( NULL == sStaticSpace )
38 sStaticSpace = create_mspace_with_base( ( void * )( sTheRecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct Snapshot ), 1 );
39 return mspace_malloc( sStaticSpace, size );
43 void *system_malloc( size_t size ){
44 static void *(*mallocp)(size_t size);
48 /* get address of libc malloc */
50 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
51 if ((error = dlerror()) != NULL) {
60 void system_free( void * ptr ){
61 static void (*freep)(void *);
64 /* get address of libc free */
66 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
67 if ((error = dlerror()) != NULL) {
75 /** Non-snapshotting free for our use. */
76 void MYFREE(void *ptr) {
77 #if USE_MPROTECT_SNAPSHOT
78 static void (*freep)(void *);
81 /* get address of libc free */
83 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
84 if ((error = dlerror()) != NULL) {
91 mspace_free( sStaticSpace, ptr );
96 /** This global references the mspace for the snapshotting heap */
97 mspace mySpace = NULL;
99 /** This global references the unaligned memory address that was malloced for the snapshotting heap */
100 void * basemySpace = NULL;
102 /** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator
103 A bit circumspect about adding an sbrk. linux docs say to avoid using it... */
104 void * HandleEarlyAllocationRequest( size_t sz ){
106 void * returnAddress = sbrk( sz );
107 if( nextRequest >= REQUESTS_BEFORE_ALLOC ){
108 exit( EXIT_FAILURE );
110 allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress;
111 return returnAddress;
116 /** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/
117 bool DontFree( void * ptr ){
118 if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call....
119 if( NULL == ptr ) return true;
120 for( int i = nextRequest - 1; i >= 0; --i ){
121 if( allocatedReqs[ i ] == ( size_t )ptr ) {
129 /** Snapshotting malloc implementation for user programs. */
130 void *malloc( size_t size ) {
131 void * earlyReq = HandleEarlyAllocationRequest( size );
132 if( earlyReq ) return earlyReq;
133 return mspace_malloc( mySpace, size );
136 /** Snapshotting free implementation for user programs. */
137 void free( void * ptr ){
138 if( DontFree( ptr ) ) return;
139 mspace_free( mySpace, ptr );
142 /** Snapshotting realloc implementation for user programs. */
143 void *realloc( void *ptr, size_t size ){
144 return mspace_realloc( mySpace, ptr, size );
147 /** Snapshotting calloc implementation for user programs. */
148 void * calloc( size_t num, size_t size ){
149 void * earlyReq = HandleEarlyAllocationRequest( size * num );
151 std::memset( earlyReq, 0, size * num );
154 return mspace_calloc( mySpace, num, size );
157 /** Snapshotting new operator for user programs. */
158 void * operator new(size_t size) throw(std::bad_alloc) {
162 /** Snapshotting delete operator for user programs. */
163 void operator delete(void *p) throw() {
167 /** Snapshotting new[] operator for user programs. */
168 void * operator new[](size_t size) throw(std::bad_alloc) {
172 /** Snapshotting delete[] operator for user programs. */
173 void operator delete[](void *p, size_t size) {