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 ), 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 /** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator
104 A bit circumspect about adding an sbrk. linux docs say to avoid using it... */
106 void * HandleEarlyAllocationRequest( size_t sz ){
108 void * returnAddress = sbrk( sz );
109 if( nextRequest >= REQUESTS_BEFORE_ALLOC ){
110 exit( EXIT_FAILURE );
112 allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress;
113 return returnAddress;
118 /** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/
120 bool DontFree( void * ptr ){
121 if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call....
122 if( NULL == ptr ) return true;
123 for( int i = nextRequest - 1; i >= 0; --i ){
124 if( allocatedReqs[ i ] == ( size_t )ptr ) {
132 /** Snapshotting malloc implementation for user programs. */
134 void *malloc( size_t size ) {
135 void * earlyReq = HandleEarlyAllocationRequest( size );
136 if( earlyReq ) return earlyReq;
137 return mspace_malloc( mySpace, size );
140 /** Snapshotting free implementation for user programs. */
142 void free( void * ptr ){
143 if( DontFree( ptr ) ) return;
144 mspace_free( mySpace, ptr );
147 /** Snapshotting realloc implementation for user programs. */
149 void *realloc( void *ptr, size_t size ){
150 return mspace_realloc( mySpace, ptr, size );
153 /** Snapshotting calloc implementation for user programs. */
155 void * calloc( size_t num, size_t size ){
156 void * earlyReq = HandleEarlyAllocationRequest( size * num );
158 std::memset( earlyReq, 0, size * num );
161 return mspace_calloc( mySpace, num, size );
164 /** Snapshotting new operator for user programs. */
166 void * operator new(size_t size) throw(std::bad_alloc) {
170 /** Snapshotting delete operator for user programs. */
172 void operator delete(void *p) throw() {
176 /** Snapshotting new[] operator for user programs. */
178 void * operator new[](size_t size) throw(std::bad_alloc) {
182 /** Snapshotting delete[] operator for user programs. */
184 void operator delete[](void *p, size_t size) {