Add file
[c11tester.git] / mymemory.cc
1
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <dlfcn.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <new>
8
9 #include "mymemory.h"
10 #include "snapshot.h"
11 #include "common.h"
12 #include "threads-model.h"
13 #include "model.h"
14 #include "datarace.h"
15
16 #define REQUESTS_BEFORE_ALLOC 1024
17
18 size_t allocatedReqs[REQUESTS_BEFORE_ALLOC] = { 0 };
19 int nextRequest = 0;
20 int howManyFreed = 0;
21 mspace sStaticSpace = NULL;
22
23 /** Non-snapshotting calloc for our use. */
24 void *model_calloc(size_t count, size_t size)
25 {
26         return mspace_calloc(sStaticSpace, count, size);
27 }
28
29 /** Non-snapshotting malloc for our use. */
30 void *model_malloc(size_t size)
31 {
32         return mspace_malloc(sStaticSpace, size);
33 }
34
35 /** Non-snapshotting malloc for our use. */
36 void *model_realloc(void *ptr, size_t size)
37 {
38         return mspace_realloc(sStaticSpace, ptr, size);
39 }
40
41 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
42 void * snapshot_malloc(size_t size)
43 {
44         void *tmp = mspace_malloc(model_snapshot_space, size);
45         ASSERT(tmp);
46         return tmp;
47 }
48
49 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
50 void * snapshot_calloc(size_t count, size_t size)
51 {
52         void *tmp = mspace_calloc(model_snapshot_space, count, size);
53         ASSERT(tmp);
54         return tmp;
55 }
56
57 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
58 void *snapshot_realloc(void *ptr, size_t size)
59 {
60         void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
61         ASSERT(tmp);
62         return tmp;
63 }
64
65 /** @brief Snapshotting free, for use by model-checker (not user progs) */
66 void snapshot_free(void *ptr)
67 {
68         mspace_free(model_snapshot_space, ptr);
69 }
70
71 /** Non-snapshotting free for our use. */
72 void model_free(void *ptr)
73 {
74         mspace_free(sStaticSpace, ptr);
75 }
76
77 /** Bootstrap allocation. Problem is that the dynamic linker calls require
78  *  calloc to work and calloc requires the dynamic linker to work. */
79
80 #define BOOTSTRAPBYTES 131072
81 char bootstrapmemory[BOOTSTRAPBYTES];
82 size_t offset = 0;
83
84 void * HandleEarlyAllocationRequest(size_t sz)
85 {
86         /* Align to 8 byte boundary */
87         sz = (sz + 7) & ~7;
88
89         if (sz > (BOOTSTRAPBYTES-offset)) {
90                 model_print("OUT OF BOOTSTRAP MEMORY.  Increase the size of BOOTSTRAPBYTES in mymemory.cc\n");
91                 exit(EXIT_FAILURE);
92         }
93
94         void *pointer = (void *)&bootstrapmemory[offset];
95         offset += sz;
96         return pointer;
97 }
98
99 /** @brief Global mspace reference for the model-checker's snapshotting heap */
100 mspace model_snapshot_space = NULL;
101
102 /** @brief Snapshotting allocation function for use by the Thread class only */
103 void * Thread_malloc(size_t size)
104 {
105         return snapshot_malloc(size);
106 }
107
108 /** @brief Snapshotting free function for use by the Thread class only */
109 void Thread_free(void *ptr)
110 {
111         snapshot_free(ptr);
112 }