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 //SUBRAMANIAN!!! PLEASE FIX THE MALLOC/FREE/CALLOC/ETC FOR FORK-BASED APPROACH
17 //YOU HAVE NOT DONE THIS!!!!!!!!!!!
19 /** Non-snapshotting malloc for our use. */
21 void *MYMALLOC(size_t size) {
22 #if USE_MPROTECT_SNAPSHOT
23 static void *(*mallocp)(size_t size);
27 /* get address of libc malloc */
29 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
30 if ((error = dlerror()) != NULL) {
39 createSharedLibrary();
41 if( NULL == sStaticSpace )
42 sStaticSpace = create_mspace_with_base( ( void * )( sTheRecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct Snapshot ), 1 );
43 return mspace_malloc( sStaticSpace, size );
47 void *system_malloc( size_t size ){
48 static void *(*mallocp)(size_t size);
52 /* get address of libc malloc */
54 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
55 if ((error = dlerror()) != NULL) {
64 void system_free( void * ptr ){
65 static void (*freep)(void *);
68 /* get address of libc free */
70 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
71 if ((error = dlerror()) != NULL) {
79 /** Non-snapshotting free for our use. */
80 void MYFREE(void *ptr) {
81 #if USE_MPROTECT_SNAPSHOT
82 static void (*freep)(void *);
85 /* get address of libc free */
87 freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
88 if ((error = dlerror()) != NULL) {
95 mspace_free( sStaticSpace, ptr );
100 /** This global references the mspace for the snapshotting heap */
101 mspace mySpace = NULL;
103 /** This global references the unaligned memory address that was malloced for the snapshotting heap */
104 void * basemySpace = NULL;
106 /** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator
107 A bit circumspect about adding an sbrk. linux docs say to avoid using it... */
109 void * HandleEarlyAllocationRequest( size_t sz ){
111 void * returnAddress = sbrk( sz );
112 if( nextRequest >= REQUESTS_BEFORE_ALLOC ){
113 exit( EXIT_FAILURE );
115 allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress;
116 return returnAddress;
121 /** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/
123 bool DontFree( void * ptr ){
124 if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call....
125 if( NULL == ptr ) return true;
126 for( int i = nextRequest - 1; i >= 0; --i ){
127 if( allocatedReqs[ i ] == ( size_t )ptr ) {
135 /** Snapshotting malloc implementation for user programs. */
137 void *malloc( size_t size ) {
138 void * earlyReq = HandleEarlyAllocationRequest( size );
139 if( earlyReq ) return earlyReq;
140 return mspace_malloc( mySpace, size );
143 /** Snapshotting free implementation for user programs. */
145 void free( void * ptr ){
146 if( DontFree( ptr ) ) return;
147 mspace_free( mySpace, ptr );
150 /** Snapshotting realloc implementation for user programs. */
152 void *realloc( void *ptr, size_t size ){
153 return mspace_realloc( mySpace, ptr, size );
156 /** Snapshotting calloc implementation for user programs. */
158 void * calloc( size_t num, size_t size ){
159 void * earlyReq = HandleEarlyAllocationRequest( size * num );
161 std::memset( earlyReq, 0, size * num );
164 return mspace_calloc( mySpace, num, size );
167 /** Snapshotting new operator for user programs. */
169 void * operator new(size_t size) throw(std::bad_alloc) {
173 /** Snapshotting delete operator for user programs. */
175 void operator delete(void *p) throw() {
179 /** Snapshotting new[] operator for user programs. */
181 void * operator new[](size_t size) throw(std::bad_alloc) {
185 /** Snapshotting delete[] operator for user programs. */
187 void operator delete[](void *p, size_t size) {