switch everything over to our own hashtable
[model-checker.git] / mymemory.cc
1 #include "mymemory.h"
2 #include "snapshot.h"
3 #include "snapshotimp.h"
4 #include <stdio.h>
5 #include <dlfcn.h>
6 #include <unistd.h>
7 #include <cstring>
8 #define REQUESTS_BEFORE_ALLOC 1024
9 size_t allocatedReqs[ REQUESTS_BEFORE_ALLOC ] = { 0 };
10 int nextRequest = 0;
11 int howManyFreed = 0;
12 #if !USE_MPROTECT_SNAPSHOT
13 static mspace sStaticSpace = NULL;
14 #endif
15
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;
20         char *error;
21         void *ptr;
22
23         /* get address of libc malloc */
24         if (!callocp) {
25                 callocp = ( void * ( * )( size_t, size_t ) )dlsym(RTLD_NEXT, "calloc");
26                 if ((error = dlerror()) != NULL) {
27                         fputs(error, stderr);
28                         exit(EXIT_FAILURE);
29                 }
30         }
31         ptr = callocp(count, size);
32         return ptr;
33 #else
34         if( !snapshotrecord) {
35                 createSharedMemory();
36         }
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 );
40 #endif
41 }
42
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;
47         char *error;
48         void *ptr;
49
50         /* get address of libc malloc */
51         if (!mallocp) {
52                 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
53                 if ((error = dlerror()) != NULL) {
54                         fputs(error, stderr);
55                         exit(EXIT_FAILURE);
56                 }
57         }
58         ptr = mallocp(size);
59         return ptr;
60 #else
61         if( !snapshotrecord) {
62                 createSharedMemory();
63         }
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 );
67 #endif
68 }
69
70 void *system_malloc( size_t size ){
71         static void *(*mallocp)(size_t size);
72         char *error;
73         void *ptr;
74
75         /* get address of libc malloc */
76         if (!mallocp) {
77                 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
78                 if ((error = dlerror()) != NULL) {
79                         fputs(error, stderr);
80                         exit(EXIT_FAILURE);
81                 }
82         }
83         ptr = mallocp(size);
84         return ptr;
85 }
86
87 void system_free( void * ptr ){
88         static void (*freep)(void *);
89         char *error;
90
91         /* get address of libc free */
92         if (!freep) {
93                 freep = ( void  ( * )( void * ) )dlsym(RTLD_NEXT, "free");
94                 if ((error = dlerror()) != NULL) {
95                         fputs(error, stderr);
96                         exit(EXIT_FAILURE);
97                 }
98         }
99         freep(ptr);
100 }
101
102 /** Non-snapshotting free for our use. */
103 void MYFREE(void *ptr) {
104 #if USE_MPROTECT_SNAPSHOT
105         static void (*freep)(void *);
106         char *error;
107
108         /* get address of libc free */
109         if (!freep) {
110                 freep = ( void  ( * )( void * ) )dlsym(RTLD_NEXT, "free");
111                 if ((error = dlerror()) != NULL) {
112                         fputs(error, stderr);
113                         exit(EXIT_FAILURE);
114                 }
115         }
116         freep(ptr);
117 #else
118         mspace_free( sStaticSpace, ptr );
119 #endif
120 }
121
122
123 /** This global references the mspace for the snapshotting heap */
124 mspace mySpace = NULL;
125
126 /** This global references the unaligned memory address that was malloced for the snapshotting heap */
127 void * basemySpace = NULL;
128
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 ){
132         if( 0 == mySpace ){
133                 void * returnAddress = sbrk( sz );
134                 if( nextRequest >= REQUESTS_BEFORE_ALLOC ){
135                         exit( EXIT_FAILURE );
136                 }
137                 allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress;
138                 return returnAddress;
139         }
140         return NULL;
141 }
142
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 ) {
149                         ++howManyFreed;
150                         return true;
151                 }
152         }
153         return false;
154 }
155
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 );
161 }
162
163 /** Snapshotting free implementation for user programs. */
164 void free( void * ptr ){
165         if( DontFree( ptr ) ) return;
166         mspace_free( mySpace, ptr );
167 }
168
169 /** Snapshotting realloc implementation for user programs. */
170 void *realloc( void *ptr, size_t size ){
171         return mspace_realloc( mySpace, ptr, size );
172 }
173
174 /** Snapshotting calloc implementation for user programs. */
175 void * calloc( size_t num, size_t size ){
176         void * earlyReq = HandleEarlyAllocationRequest( size * num );
177         if( earlyReq ) {
178                 std::memset( earlyReq, 0, size * num );
179                 return earlyReq;
180         }
181         return mspace_calloc( mySpace, num, size );
182 }
183
184 /** Snapshotting new operator for user programs. */
185 void * operator new(size_t size) throw(std::bad_alloc) {
186         return malloc(size);
187 }
188
189 /** Snapshotting delete operator for user programs. */
190 void operator delete(void *p) throw() {
191         free(p);
192 }
193
194 /** Snapshotting new[] operator for user programs. */
195 void * operator new[](size_t size) throw(std::bad_alloc) {
196         return malloc(size);
197 }
198
199 /** Snapshotting delete[] operator for user programs. */
200 void operator delete[](void *p, size_t size) {
201         free(p);
202 }