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