Small edits
[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 static mspace sStaticSpace = NULL;
22
23 /** Non-snapshotting calloc for our use. */
24 void *model_calloc(size_t count, size_t size)
25 {
26         if (!sStaticSpace)
27                 sStaticSpace = create_shared_mspace();
28         return mspace_calloc(sStaticSpace, count, size);
29 }
30
31 /** Non-snapshotting malloc for our use. */
32 void *model_malloc(size_t size)
33 {
34         if (!sStaticSpace)
35                 sStaticSpace = create_shared_mspace();
36         return mspace_malloc(sStaticSpace, size);
37 }
38
39 /** Non-snapshotting malloc for our use. */
40 void *model_realloc(void *ptr, size_t size)
41 {
42         if (!sStaticSpace)
43                 sStaticSpace = create_shared_mspace();
44         return mspace_realloc(sStaticSpace, ptr, size);
45 }
46
47 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
48 void * snapshot_malloc(size_t size)
49 {
50         void *tmp = mspace_malloc(model_snapshot_space, size);
51         ASSERT(tmp);
52         return tmp;
53 }
54
55 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
56 void * snapshot_calloc(size_t count, size_t size)
57 {
58         void *tmp = mspace_calloc(model_snapshot_space, count, size);
59         ASSERT(tmp);
60         return tmp;
61 }
62
63 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
64 void *snapshot_realloc(void *ptr, size_t size)
65 {
66         void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
67         ASSERT(tmp);
68         return tmp;
69 }
70
71 /** @brief Snapshotting free, for use by model-checker (not user progs) */
72 void snapshot_free(void *ptr)
73 {
74         mspace_free(model_snapshot_space, ptr);
75 }
76
77 /** Non-snapshotting free for our use. */
78 void model_free(void *ptr)
79 {
80         mspace_free(sStaticSpace, ptr);
81 }
82
83 /** Bootstrap allocation. Problem is that the dynamic linker calls require
84  *  calloc to work and calloc requires the dynamic linker to work. */
85
86 #define BOOTSTRAPBYTES 131072
87 char bootstrapmemory[BOOTSTRAPBYTES];
88 size_t offset = 0;
89
90 void * HandleEarlyAllocationRequest(size_t sz)
91 {
92         /* Align to 8 byte boundary */
93         sz = (sz + 7) & ~7;
94
95         if (sz > (BOOTSTRAPBYTES-offset)) {
96                 model_print("OUT OF BOOTSTRAP MEMORY.  Increase the size of BOOTSTRAPBYTES in mymemory.cc\n");
97                 exit(EXIT_FAILURE);
98         }
99
100         void *pointer = (void *)&bootstrapmemory[offset];
101         offset += sz;
102         return pointer;
103 }
104
105 /** @brief Global mspace reference for the model-checker's snapshotting heap */
106 mspace model_snapshot_space = NULL;
107
108 /** @brief Snapshotting allocation function for use by the Thread class only */
109 void * Thread_malloc(size_t size)
110 {
111         return snapshot_malloc(size);
112 }
113
114 /** @brief Snapshotting free function for use by the Thread class only */
115 void Thread_free(void *ptr)
116 {
117         snapshot_free(ptr);
118 }