12 #include "threads-model.h"
15 #define REQUESTS_BEFORE_ALLOC 1024
17 size_t allocatedReqs[REQUESTS_BEFORE_ALLOC] = { 0 };
21 #if !USE_MPROTECT_SNAPSHOT
22 static mspace sStaticSpace = NULL;
25 /** Non-snapshotting calloc for our use. */
26 void *model_calloc(size_t count, size_t size)
28 #if USE_MPROTECT_SNAPSHOT
29 static void *(*callocp)(size_t count, size_t size) = NULL;
33 /* get address of libc malloc */
35 callocp = (void * (*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
36 if ((error = dlerror()) != NULL) {
41 ptr = callocp(count, size);
45 sStaticSpace = create_shared_mspace();
46 return mspace_calloc(sStaticSpace, count, size);
50 /** Non-snapshotting malloc for our use. */
51 void *model_malloc(size_t size)
53 #if USE_MPROTECT_SNAPSHOT
54 static void *(*mallocp)(size_t size) = NULL;
58 /* get address of libc malloc */
60 mallocp = (void * (*)(size_t))dlsym(RTLD_NEXT, "malloc");
61 if ((error = dlerror()) != NULL) {
70 sStaticSpace = create_shared_mspace();
71 return mspace_malloc(sStaticSpace, size);
75 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
76 void * snapshot_malloc(size_t size)
78 void *tmp = mspace_malloc(model_snapshot_space, size);
83 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
84 void * snapshot_calloc(size_t count, size_t size)
86 void *tmp = mspace_calloc(model_snapshot_space, count, size);
91 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
92 void *snapshot_realloc(void *ptr, size_t size)
94 void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
99 /** @brief Snapshotting free, for use by model-checker (not user progs) */
100 void snapshot_free(void *ptr)
102 mspace_free(model_snapshot_space, ptr);
105 /** Non-snapshotting free for our use. */
106 void model_free(void *ptr)
108 #if USE_MPROTECT_SNAPSHOT
109 static void (*freep)(void *);
112 /* get address of libc free */
114 freep = (void (*)(void *))dlsym(RTLD_NEXT, "free");
115 if ((error = dlerror()) != NULL) {
116 fputs(error, stderr);
122 mspace_free(sStaticSpace, ptr);
126 /** Bootstrap allocation. Problem is that the dynamic linker calls require
127 * calloc to work and calloc requires the dynamic linker to work. */
129 #define BOOTSTRAPBYTES 131072
130 char bootstrapmemory[BOOTSTRAPBYTES];
133 void * HandleEarlyAllocationRequest(size_t sz)
135 /* Align to 8 byte boundary */
138 if (sz > (BOOTSTRAPBYTES-offset)) {
139 model_print("OUT OF BOOTSTRAP MEMORY. Increase the size of BOOTSTRAPBYTES in mymemory.cc\n");
143 void *pointer = (void *)&bootstrapmemory[offset];
148 /** @brief Global mspace reference for the model-checker's snapshotting heap */
149 mspace model_snapshot_space = NULL;
151 #if USE_MPROTECT_SNAPSHOT
153 /** @brief Global mspace reference for the user's snapshotting heap */
154 mspace user_snapshot_space = NULL;
156 /** Check whether this is bootstrapped memory that we should not free */
157 static bool DontFree(void *ptr)
159 return (ptr >= (&bootstrapmemory[0]) && ptr < (&bootstrapmemory[BOOTSTRAPBYTES]));
163 * @brief The allocator function for "user" allocation
165 * Should only be used for allocations which will not disturb the allocation
166 * patterns of a user thread.
168 static void * user_malloc(size_t size)
170 void *tmp = mspace_malloc(user_snapshot_space, size);
176 * @brief Snapshotting malloc implementation for user programs
178 * Do NOT call this function from a model-checker context. Doing so may disrupt
179 * the allocation patterns of a user thread.
181 void *malloc(size_t size)
183 if (user_snapshot_space) {
185 return model_malloc(size);
187 /* Only perform user allocations from user context */
188 ASSERT(!model || thread_current());
189 return user_malloc(size);
191 return HandleEarlyAllocationRequest(size);
194 /** @brief Snapshotting free implementation for user programs */
195 void free(void * ptr)
197 if (!DontFree(ptr)) {
199 return model_free(ptr);
201 mspace_free(user_snapshot_space, ptr);
205 /** @brief Snapshotting realloc implementation for user programs */
206 void *realloc(void *ptr, size_t size)
208 void *tmp = mspace_realloc(user_snapshot_space, ptr, size);
213 /** @brief Snapshotting calloc implementation for user programs */
214 void * calloc(size_t num, size_t size)
216 if (user_snapshot_space) {
217 void *tmp = mspace_calloc(user_snapshot_space, num, size);
221 void *tmp = HandleEarlyAllocationRequest(size * num);
222 memset(tmp, 0, size * num);
227 /** @brief Snapshotting allocation function for use by the Thread class only */
228 void * Thread_malloc(size_t size)
230 return user_malloc(size);
233 /** @brief Snapshotting free function for use by the Thread class only */
234 void Thread_free(void *ptr)
239 /** @brief Snapshotting new operator for user programs */
240 void * operator new(size_t size) throw(std::bad_alloc)
245 /** @brief Snapshotting delete operator for user programs */
246 void operator delete(void *p) throw()
251 /** @brief Snapshotting new[] operator for user programs */
252 void * operator new[](size_t size) throw(std::bad_alloc)
257 /** @brief Snapshotting delete[] operator for user programs */
258 void operator delete[](void *p, size_t size)
263 #else /* !USE_MPROTECT_SNAPSHOT */
265 /** @brief Snapshotting allocation function for use by the Thread class only */
266 void * Thread_malloc(size_t size)
268 return snapshot_malloc(size);
271 /** @brief Snapshotting free function for use by the Thread class only */
272 void Thread_free(void *ptr)
277 #endif /* !USE_MPROTECT_SNAPSHOT */