12 #include "threads-model.h"
15 #define REQUESTS_BEFORE_ALLOC 1024
17 size_t allocatedReqs[REQUESTS_BEFORE_ALLOC] = { 0 };
20 #if !USE_MPROTECT_SNAPSHOT
21 static mspace sStaticSpace = NULL;
24 /** Non-snapshotting calloc for our use. */
25 void *model_calloc(size_t count, size_t size)
27 #if USE_MPROTECT_SNAPSHOT
28 static void *(*callocp)(size_t count, size_t size) = NULL;
32 /* get address of libc malloc */
34 callocp = (void * (*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
35 if ((error = dlerror()) != NULL) {
40 ptr = callocp(count, size);
44 sStaticSpace = create_shared_mspace();
45 return mspace_calloc(sStaticSpace, count, size);
49 /** Non-snapshotting malloc for our use. */
50 void *model_malloc(size_t size)
52 #if USE_MPROTECT_SNAPSHOT
53 static void *(*mallocp)(size_t size) = NULL;
57 /* get address of libc malloc */
59 mallocp = (void * (*)(size_t))dlsym(RTLD_NEXT, "malloc");
60 if ((error = dlerror()) != NULL) {
69 sStaticSpace = create_shared_mspace();
70 return mspace_malloc(sStaticSpace, size);
74 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
75 void * snapshot_malloc(size_t size)
77 void *tmp = mspace_malloc(model_snapshot_space, size);
82 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
83 void * snapshot_calloc(size_t count, size_t size)
85 void *tmp = mspace_calloc(model_snapshot_space, count, size);
90 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
91 void *snapshot_realloc(void *ptr, size_t size)
93 void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
98 /** @brief Snapshotting free, for use by model-checker (not user progs) */
99 void snapshot_free(void *ptr)
101 mspace_free(model_snapshot_space, ptr);
104 /** Non-snapshotting free for our use. */
105 void model_free(void *ptr)
107 #if USE_MPROTECT_SNAPSHOT
108 static void (*freep)(void *);
111 /* get address of libc free */
113 freep = (void (*)(void *))dlsym(RTLD_NEXT, "free");
114 if ((error = dlerror()) != NULL) {
115 fputs(error, stderr);
121 mspace_free(sStaticSpace, ptr);
125 /** Bootstrap allocation. Problem is that the dynamic linker calls require
126 * calloc to work and calloc requires the dynamic linker to work. */
128 #define BOOTSTRAPBYTES 131072
129 char bootstrapmemory[BOOTSTRAPBYTES];
132 void * HandleEarlyAllocationRequest(size_t sz)
134 /* Align to 8 byte boundary */
137 if (sz > (BOOTSTRAPBYTES-offset)) {
138 model_print("OUT OF BOOTSTRAP MEMORY. Increase the size of BOOTSTRAPBYTES in mymemory.cc\n");
142 void *pointer = (void *)&bootstrapmemory[offset];
147 /** @brief Global mspace reference for the model-checker's snapshotting heap */
148 mspace model_snapshot_space = NULL;
150 #if USE_MPROTECT_SNAPSHOT
152 /** @brief Global mspace reference for the user's snapshotting heap */
153 mspace user_snapshot_space = NULL;
155 /** Check whether this is bootstrapped memory that we should not free */
156 static bool DontFree(void *ptr)
158 return (ptr >= (&bootstrapmemory[0]) && ptr < (&bootstrapmemory[BOOTSTRAPBYTES]));
162 * @brief The allocator function for "user" allocation
164 * Should only be used for allocations which will not disturb the allocation
165 * patterns of a user thread.
167 static void * user_malloc(size_t size)
169 void *tmp = mspace_malloc(user_snapshot_space, size);
175 * @brief Snapshotting malloc implementation for user programs
177 * Do NOT call this function from a model-checker context. Doing so may disrupt
178 * the allocation patterns of a user thread.
180 void *malloc(size_t size)
182 if (user_snapshot_space) {
183 /* Only perform user allocations from user context */
184 ASSERT(!model || thread_current());
185 return user_malloc(size);
187 return HandleEarlyAllocationRequest(size);
190 /** @brief Snapshotting free implementation for user programs */
191 void free(void * ptr)
193 if (!DontFree(ptr)) {
194 mspace_free(user_snapshot_space, ptr);
198 /** @brief Snapshotting realloc implementation for user programs */
199 void *realloc(void *ptr, size_t size)
201 void *tmp = mspace_realloc(user_snapshot_space, ptr, size);
206 /** @brief Snapshotting calloc implementation for user programs */
207 void * calloc(size_t num, size_t size)
209 if (user_snapshot_space) {
210 void *tmp = mspace_calloc(user_snapshot_space, num, size);
214 void *tmp = HandleEarlyAllocationRequest(size * num);
215 memset(tmp, 0, size * num);
220 /** @brief Snapshotting allocation function for use by the Thread class only */
221 void * Thread_malloc(size_t size)
223 return user_malloc(size);
226 /** @brief Snapshotting free function for use by the Thread class only */
227 void Thread_free(void *ptr)
232 /** @brief Snapshotting new operator for user programs */
233 void * operator new(size_t size) throw(std::bad_alloc)
238 /** @brief Snapshotting delete operator for user programs */
239 void operator delete(void *p) throw()
244 /** @brief Snapshotting new[] operator for user programs */
245 void * operator new[](size_t size) throw(std::bad_alloc)
250 /** @brief Snapshotting delete[] operator for user programs */
251 void operator delete[](void *p, size_t size)
256 #else /* !USE_MPROTECT_SNAPSHOT */
258 /** @brief Snapshotting allocation function for use by the Thread class only */
259 void * Thread_malloc(size_t size)
261 return snapshot_malloc(size);
264 /** @brief Snapshotting free function for use by the Thread class only */
265 void Thread_free(void *ptr)
270 #endif /* !USE_MPROTECT_SNAPSHOT */