fixing calloc(), fix Makefile
[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 malloc for our use. */
17
18 void *MYMALLOC(size_t size) {
19 #if USE_MPROTECT_SNAPSHOT
20         static void *(*mallocp)(size_t size);
21         char *error;
22         void *ptr;
23   
24         /* get address of libc malloc */
25         if (!mallocp) {
26                 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
27                 if ((error = dlerror()) != NULL) {
28                         fputs(error, stderr);
29                         exit(EXIT_FAILURE);
30                 }
31         }
32         ptr = mallocp(size);     
33         return ptr;
34 #else
35         if( !sTheRecord ){
36                 createSharedLibrary();
37         }
38         if( NULL == sStaticSpace )
39                 sStaticSpace = create_mspace_with_base( ( void * )( sTheRecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct Snapshot_t ), 1 );
40         return mspace_malloc( sStaticSpace, size );
41 #endif
42 }
43
44 void *system_malloc( size_t size ){
45         static void *(*mallocp)(size_t size);
46         char *error;
47         void *ptr;
48
49         /* get address of libc malloc */
50         if (!mallocp) {
51                 mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
52                 if ((error = dlerror()) != NULL) {
53                         fputs(error, stderr);
54                         exit(EXIT_FAILURE);
55                 }
56         }
57         ptr = mallocp(size);
58         return ptr;
59 }
60
61 void system_free( void * ptr ){
62         static void (*freep)(void *);
63         char *error;
64
65         /* get address of libc free */
66         if (!freep) {
67                 freep = ( void  ( * )( void * ) )dlsym(RTLD_NEXT, "free");
68                 if ((error = dlerror()) != NULL) {
69                         fputs(error, stderr);
70                         exit(EXIT_FAILURE);
71                 }
72         }
73         freep(ptr);
74 }
75
76 /** Non-snapshotting free for our use. */
77 void MYFREE(void *ptr) {
78 #if USE_MPROTECT_SNAPSHOT
79         static void (*freep)(void *);
80         char *error;
81
82         /* get address of libc free */
83         if (!freep) {
84                 freep = ( void  ( * )( void * ) )dlsym(RTLD_NEXT, "free");
85                 if ((error = dlerror()) != NULL) {
86                         fputs(error, stderr);
87                         exit(EXIT_FAILURE);
88                 }
89         }
90         freep(ptr);
91 #else
92         mspace_free( sStaticSpace, ptr );
93 #endif
94 }
95
96
97 /** This global references the mspace for the snapshotting heap */
98 mspace mySpace = NULL;
99
100 /** This global references the unaligned memory address that was malloced for the snapshotting heap */
101 void * basemySpace = NULL;
102
103 //Subramanian --- please make these work for the fork based approach
104
105 /** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator
106 A bit circumspect about adding an sbrk. linux docs say to avoid using it... */
107
108 void * HandleEarlyAllocationRequest( size_t sz ){
109         if( 0 == mySpace ){
110                 void * returnAddress = sbrk( sz );
111                 if( nextRequest >= REQUESTS_BEFORE_ALLOC ){
112                         exit( EXIT_FAILURE );
113                 }
114                 allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress;
115                 return returnAddress;
116         }
117         return NULL;
118 }
119
120 /** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/
121
122 bool DontFree( void * ptr ){
123         if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call....
124         if( NULL == ptr ) return true;
125         for( int i =  nextRequest - 1; i >= 0; --i ){
126                 if( allocatedReqs[ i ] ==  ( size_t )ptr ) {
127                         ++howManyFreed;
128                         return true;
129                 }
130         }
131         return false;
132 }
133
134 /** Snapshotting malloc implementation for user programs. */
135
136 void *malloc( size_t size ) {
137         void * earlyReq = HandleEarlyAllocationRequest( size );
138         if( earlyReq ) return earlyReq;
139         return mspace_malloc( mySpace, size );
140 }
141
142 /** Snapshotting free implementation for user programs. */
143
144 void free( void * ptr ){
145         if( DontFree( ptr ) ) return;
146         mspace_free( mySpace, ptr );
147 }
148
149 /** Snapshotting realloc implementation for user programs. */
150
151 void *realloc( void *ptr, size_t size ){
152         return mspace_realloc( mySpace, ptr, size );
153 }
154
155 /** Snapshotting calloc implementation for user programs. */
156
157 void * calloc( size_t num, size_t size ){
158         void * earlyReq = HandleEarlyAllocationRequest( size * num );
159         if( earlyReq ) {
160                 std::memset( earlyReq, 0, size * num );
161                 return earlyReq;
162         }
163         return mspace_calloc( mySpace, num, size );
164 }
165
166
167 /** Snapshotting new operator for user programs. */
168
169
170 void * operator new(size_t size) throw(std::bad_alloc) {
171         return malloc(size);
172 }
173
174 /** Snapshotting delete operator for user programs. */
175
176 void operator delete(void *p) throw() {
177         free(p);
178 }
179
180 /** Snapshotting new[] operator for user programs. */
181
182 void * operator new[](size_t size) throw(std::bad_alloc) {
183         return malloc(size);
184 }
185
186 /** Snapshotting delete[] operator for user programs. */
187
188 void operator delete[](void *p, size_t size) {
189         free(p);
190 }