Debian Codegraph @

vendor/Heap-Layers/heaps/threads/threadspecificheap.h

raw symbols SCIP
1 /* -*- C++ -*- */cxx . . $ `<file>/vendor/Heap-Layers/heaps/threads/threadspecificheap.h`/
2
3 #ifndef HL_THREADSPECIFICHEAP_H
4 #define HL_THREADSPECIFICHEAP_Hcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:4:9`!
5
6 /*
7
8 Heap Layers: An Extensible Memory Allocation Infrastructure
9
10 Copyright (C) 2000-2020 by Emery Berger
11 http://www.emeryberger.com
12 [email protected]
13
14 Heap Layers is distributed under the terms of the Apache 2.0 license.
15
16 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 */
20
21 #include <assert.h>
22 #include <new>
23
24 #if !defined(_WIN32) // not implemented for Windows
25
26 #include <pthread.h>
27
28 #include "wrappers/mmapwrapper.h"cxx . . $ `<file>/vendor/Heap-Layers/wrappers/mmapwrapper.h`/
29
30 #if defined(__clang__cxx . . $ `<built-in>:3:9`!)
31 #pragma clang diagnostic push
32 #pragma clang diagnostic ignored "-Wunused-variable"
33 #endif
34
35 // Compute the version of gcc we're compiling with (if any).
36 #define GCC_VERSIONcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:36:9`! (__GNUC__cxx . . $ `<built-in>:8:9`! * 10000 \
37 + __GNUC_MINOR__cxx . . $ `<built-in>:9:9`! * 100 \
38 + __GNUC_PATCHLEVEL__cxx . . $ `<built-in>:10:9`!)
39
40 #if (((defined(GCC_VERSIONcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:36:9`!) && (GCC_VERSIONcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:36:9`! >= 30300)) && \
41 !defined(__SVR4)) \
42 && !defined(__APPLE__) \
43 || defined(__SUNPRO_CC) \
44 || defined(__FreeBSD__))
45 #define USE_THREAD_KEYWORDcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:45:9`! 1
46 #else
47 #define USE_THREAD_KEYWORD 0
48 #endif
49
50 namespace HLcxx . . $ HL/ {
51
52 #if USE_THREAD_KEYWORDcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:45:9`!
53 #if !defined(INITIAL_EXEC_ATTR)
54 #define INITIAL_EXEC_ATTRcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:54:9`! __attribute__((tls_model ("initial-exec")))
55 #endif
56
57 template <class PerThreadHeap>
58 class ThreadSpecificHeapcxx . . $ HL/ThreadSpecificHeap# {
59 private:
60 alignas(16) static __thread char heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.[sizeof(PerThreadHeap)];
61 static __thread PerThreadHeap * heapcxx . . $ HL/ThreadSpecificHeap#heap.;
62 public:
63
64 ThreadSpecificHeapcxx . . $ HL/ThreadSpecificHeap#`ThreadSpecificHeap<PerThreadHeap>`(49f6e7a06ebc5aa8).()
65 {
66 }
67
68 ~cxx . . $ HL/ThreadSpecificHeap#`~ThreadSpecificHeap<PerThreadHeap>`(49f6e7a06ebc5aa8).ThreadSpecificHeap() {
69 if (heapcxx . . $ HL/ThreadSpecificHeap#heap.) {
70 heapcxx . . $ HL/ThreadSpecificHeap#heap.->~PerThreadHeap();
71 }
72 }
73
74 inline void * malloccxx . . $ HL/ThreadSpecificHeap#malloc(bfcfa4d6b7f7ef64).(size_t sz) {
75 if (heapcxx . . $ HL/ThreadSpecificHeap#heap. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). nullptr) {
76 heapcxx . . $ HL/ThreadSpecificHeap#heap. = new (heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.) PerThreadHeap();
77 }
78 return heapcxx . . $ HL/ThreadSpecificHeap#heap.->malloc(sz);
79 }
80 #if HL_USE_XXREALLOCcxx . . $ `<built-in>:460:9`!
81 inline void * realloccxx . . $ HL/ThreadSpecificHeap#realloc(417ab160f6baadc6).(void* ptr, size_t sz) {
82 if (heapcxx . . $ HL/ThreadSpecificHeap#heap. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). nullptr) {
83 heapcxx . . $ HL/ThreadSpecificHeap#heap. = new (heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.) PerThreadHeap();
84 }
85 return heapcxx . . $ HL/ThreadSpecificHeap#heap.->realloc(ptr, sz);
86 }
87 #endif
88
89 inline void freecxx . . $ HL/ThreadSpecificHeap#free(5d22bdacc48458e8).(void * ptr) {
90 if (heapcxx . . $ HL/ThreadSpecificHeap#heap. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). nullptr) {
91 heapcxx . . $ HL/ThreadSpecificHeap#heap. = new (heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.) PerThreadHeap();
92 }
93 heapcxx . . $ HL/ThreadSpecificHeap#heap.->free(ptr);
94 }
95
96 inline void register_malloccxx . . $ HL/ThreadSpecificHeap#register_malloc(3bdecba87b0fe3d5).(size_t sz, void * ptr) {
97 if (heapcxx . . $ HL/ThreadSpecificHeap#heap. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). nullptr) {
98 heapcxx . . $ HL/ThreadSpecificHeap#heap. = new (heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.) PerThreadHeap();
99 }
100 heapcxx . . $ HL/ThreadSpecificHeap#heap.->register_malloc(sz, ptr);
101 }
102
103 inline void register_freecxx . . $ HL/ThreadSpecificHeap#register_free(3bdecba87b0fe3d5).(size_t sz, void * ptr) {
104 if (heapcxx . . $ HL/ThreadSpecificHeap#heap. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). nullptr) {
105 heapcxx . . $ HL/ThreadSpecificHeap#heap. = new (heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.) PerThreadHeap();
106 }
107 heapcxx . . $ HL/ThreadSpecificHeap#heap.->register_free(sz, ptr);
108 }
109
110 inline void * memaligncxx . . $ HL/ThreadSpecificHeap#memalign(63a7bf2941bfcd7a).(size_t alignment, size_t sz) {
111 if (heapcxx . . $ HL/ThreadSpecificHeap#heap. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). nullptr) {
112 heapcxx . . $ HL/ThreadSpecificHeap#heap. = new (heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.) PerThreadHeap();
113 }
114 return heapcxx . . $ HL/ThreadSpecificHeap#heap.->memalign(alignment, sz);
115 }
116
117 inline size_t getSizecxx . . $ HL/ThreadSpecificHeap#getSize(751e5d26f8574c32).(void * ptr) {
118 if (heapcxx . . $ HL/ThreadSpecificHeap#heap. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). nullptr) {
119 heapcxx . . $ HL/ThreadSpecificHeap#heap. = new (heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.) PerThreadHeap();
120 }
121 return heapcxx . . $ HL/ThreadSpecificHeap#heap.->getSize(ptr);
122 }
123
124 enumcxx . . $ HL/ThreadSpecificHeap#$anonymous_type_0# { Alignmentcxx . . $ HL/ThreadSpecificHeap#Alignment. = PerThreadHeap::Alignment };
125 };
126
127 template <class PerThreadHeap>
128 alignas(16) __thread char ThreadSpecificHeapcxx . . $ HL/ThreadSpecificHeap#<PerThreadHeap>::heapbufcxx . . $ HL/ThreadSpecificHeap#heapbuf.[sizeof(PerThreadHeap)] INITIAL_EXEC_ATTRcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:54:9`!;
129
130 template <class PerThreadHeap>
131 __thread PerThreadHeap * ThreadSpecificHeapcxx . . $ HL/ThreadSpecificHeap#<PerThreadHeap>::heapcxx . . $ HL/ThreadSpecificHeap#heap. INITIAL_EXEC_ATTRcxx . . $ `vendor/Heap-Layers/heaps/threads/threadspecificheap.h:54:9`! = nullptr;
132
133
134 #else // USE_THREAD_KEYWORD
135
136 template <class PerThreadHeap>
137 class ThreadSpecificHeap {
138 public:
139
140 ThreadSpecificHeap()
141 {
142 // Initialize the heap exactly once.
143 pthread_once (&(getOnce()), initializeHeap);
144 }
145
146 inline void * malloc (size_t sz) {
147 return getHeap()->malloc (sz);
148 }
149 #if HL_USE_XXREALLOC
150 inline void * realloc(void* ptr, size_t sz) {
151 return getHeap()->realloc(ptr, sz);
152 }
153 #endif
154
155 inline void free (void * ptr) {
156 getHeap()->free (ptr);
157 }
158
159 void register_malloc(size_t sz, void * ptr) {
160 getHeap()->register_malloc(sz, ptr);
161 }
162
163 void register_free(size_t sz, void * ptr) {
164 getHeap()->register_free(sz, ptr);
165 }
166
167 inline size_t getSize (void * ptr) {
168 return getHeap()->getSize(ptr);
169 }
170
171 inline void * memalign(size_t alignment, size_t sz) {
172 return getHeap()->memalign(alignment, sz);
173 }
174
175 enum { Alignment = PerThreadHeap::Alignment };
176
177 private:
178
179 static void initializeHeap() {
180 getHeap();
181 }
182
183 static pthread_key_t& getHeapKey() {
184 static pthread_key_t heapKey;
185 static int r = pthread_key_create (&heapKey, deleteHeap);
186 return heapKey;
187 }
188
189 static pthread_once_t& getOnce() {
190 static pthread_once_t initOnce = PTHREAD_ONCE_INIT;
191 return initOnce;
192 }
193
194 static void deleteHeap (void * heap) {
195 ((PerThreadHeap *) heap)->~PerThreadHeap();
196 HL::MmapWrapper::unmap (heap, sizeof(PerThreadHeap));
197 }
198
199 // Access the given heap.
200 static PerThreadHeap * getHeap() {
201 PerThreadHeap * heap =
202 (PerThreadHeap *) pthread_getspecific (getHeapKey());
203 if (heap == nullptr) {
204 // Grab some memory from a source, initialize the heap inside,
205 // and store it in the thread-local area.
206 void * buf = HL::MmapWrapper::map (sizeof(PerThreadHeap));
207 heap = new (buf) PerThreadHeap;
208 pthread_setspecific (getHeapKey(), (void *) heap);
209 }
210 return heap;
211 }
212 };
213
214 #endif
215
216 }
217
218 #if defined(__clang__cxx . . $ `<built-in>:3:9`!)
219 #pragma clang diagnostic pop
220 #endif
221
222 #endif
223
224 #endif