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 calloc for our use. */
17 void *MYCALLOC(size_t count, size_t size) {
18 #if USE_MPROTECT_SNAPSHOT
19 static void *(*callocp)(size_t count, size_t size)=NULL;
23 /* get address of libc malloc */
25 callocp = ( void * ( * )( size_t, size_t ) )dlsym(RTLD_NEXT, "calloc");
26 if ((error = dlerror()) != NULL) {
31 ptr = callocp(count, size);
34 if( !snapshotrecord) {
37 if( NULL == sStaticSpace )
38 sStaticSpace = create_mspace_with_base( ( void * )( snapshotrecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct SnapShot ), 1 );
39 return mspace_calloc( sStaticSpace, count, size );
43 /** Non-snapshotting malloc for our use. */
44 void *MYMALLOC(size_t size) {
45 #if USE_MPROTECT_SNAPSHOT
46 static void *(*mallocp)(size_t size)=NULL;
50 /* get address of libc malloc */
52 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
53 if ((error = dlerror()) != NULL) {
61 if( !snapshotrecord) {
64 if( NULL == sStaticSpace )
65 sStaticSpace = create_mspace_with_base( ( void * )( snapshotrecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct SnapShot ), 1 );
66 return mspace_malloc( sStaticSpace, size );
70 void *system_malloc( size_t size ){
71 static void *(*mallocp)(size_t size);
75 /* get address of libc malloc */
77 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
78 if ((error = dlerror()) != NULL) {
87 void system_free( void * ptr ){
88 static void (*freep)(void *);
91 /* get address of libc free */
93 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
94 if ((error = dlerror()) != NULL) {
102 /** Non-snapshotting free for our use. */
103 void MYFREE(void *ptr) {
104 #if USE_MPROTECT_SNAPSHOT
105 static void (*freep)(void *);
108 /* get address of libc free */
110 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
111 if ((error = dlerror()) != NULL) {
112 fputs(error, stderr);
118 mspace_free( sStaticSpace, ptr );
123 /** This global references the mspace for the snapshotting heap */
124 mspace mySpace = NULL;
126 /** This global references the unaligned memory address that was malloced for the snapshotting heap */
127 void * basemySpace = NULL;
129 /** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator
130 A bit circumspect about adding an sbrk. linux docs say to avoid using it... */
131 void * HandleEarlyAllocationRequest( size_t sz ){
133 void * returnAddress = sbrk( sz );
134 if( nextRequest >= REQUESTS_BEFORE_ALLOC ){
135 exit( EXIT_FAILURE );
137 allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress;
138 return returnAddress;
143 /** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/
144 bool DontFree( void * ptr ){
145 if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call....
146 if( NULL == ptr ) return true;
147 for( int i = nextRequest - 1; i >= 0; --i ){
148 if( allocatedReqs[ i ] == ( size_t )ptr ) {
156 /** Snapshotting malloc implementation for user programs. */
157 void *malloc( size_t size ) {
158 void * earlyReq = HandleEarlyAllocationRequest( size );
159 if( earlyReq ) return earlyReq;
160 return mspace_malloc( mySpace, size );
163 /** Snapshotting free implementation for user programs. */
164 void free( void * ptr ){
165 if( DontFree( ptr ) ) return;
166 mspace_free( mySpace, ptr );
169 /** Snapshotting realloc implementation for user programs. */
170 void *realloc( void *ptr, size_t size ){
171 return mspace_realloc( mySpace, ptr, size );
174 /** Snapshotting calloc implementation for user programs. */
175 void * calloc( size_t num, size_t size ){
176 void * earlyReq = HandleEarlyAllocationRequest( size * num );
178 std::memset( earlyReq, 0, size * num );
181 return mspace_calloc( mySpace, num, size );
184 /** Snapshotting new operator for user programs. */
185 void * operator new(size_t size) throw(std::bad_alloc) {
189 /** Snapshotting delete operator for user programs. */
190 void operator delete(void *p) throw() {
194 /** Snapshotting new[] operator for user programs. */
195 void * operator new[](size_t size) throw(std::bad_alloc) {
199 /** Snapshotting delete[] operator for user programs. */
200 void operator delete[](void *p, size_t size) {