| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | #ifndef HL_THREADSPECIFICHEAP_H |
| 4 | #define HL_THREADSPECIFICHEAP_H |
| 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" |
| 29 | |
| 30 | #if defined(__clang__) |
| 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_VERSION (__GNUC__ * 10000 \ |
| 37 | + __GNUC_MINOR__ * 100 \ |
| 38 | + __GNUC_PATCHLEVEL__) |
| 39 | |
| 40 | #if (((defined(GCC_VERSION) && (GCC_VERSION >= 30300)) && \ |
| 41 | !defined(__SVR4)) \ |
| 42 | && !defined(__APPLE__) \ |
| 43 | || defined(__SUNPRO_CC) \ |
| 44 | || defined(__FreeBSD__)) |
| 45 | #define USE_THREAD_KEYWORD 1 |
| 46 | #else |
| 47 | #define USE_THREAD_KEYWORD 0 |
| 48 | #endif |
| 49 | |
| 50 | namespace HL { |
| 51 | |
| 52 | #if USE_THREAD_KEYWORD |
| 53 | #if !defined(INITIAL_EXEC_ATTR) |
| 54 | #define INITIAL_EXEC_ATTR __attribute__((tls_model ("initial-exec"))) |
| 55 | #endif |
| 56 | |
| 57 | template <class PerThreadHeap> |
| 58 | class ThreadSpecificHeap { |
| 59 | private: |
| 60 | alignas(16) static __thread char heapbuf[sizeof(PerThreadHeap)]; |
| 61 | static __thread PerThreadHeap * heap; |
| 62 | public: |
| 63 | |
| 64 | ThreadSpecificHeap() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | ~ThreadSpecificHeap() { |
| 69 | if (heap) { |
| 70 | heap->~PerThreadHeap(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | inline void * malloc(size_t sz) { |
| 75 | if (heap == nullptr) { |
| 76 | heap = new (heapbuf) PerThreadHeap(); |
| 77 | } |
| 78 | return heap->malloc(sz); |
| 79 | } |
| 80 | #if HL_USE_XXREALLOC |
| 81 | inline void * realloc(void* ptr, size_t sz) { |
| 82 | if (heap == nullptr) { |
| 83 | heap = new (heapbuf) PerThreadHeap(); |
| 84 | } |
| 85 | return heap->realloc(ptr, sz); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | inline void free(void * ptr) { |
| 90 | if (heap == nullptr) { |
| 91 | heap = new (heapbuf) PerThreadHeap(); |
| 92 | } |
| 93 | heap->free(ptr); |
| 94 | } |
| 95 | |
| 96 | inline void register_malloc(size_t sz, void * ptr) { |
| 97 | if (heap == nullptr) { |
| 98 | heap = new (heapbuf) PerThreadHeap(); |
| 99 | } |
| 100 | heap->register_malloc(sz, ptr); |
| 101 | } |
| 102 | |
| 103 | inline void register_free(size_t sz, void * ptr) { |
| 104 | if (heap == nullptr) { |
| 105 | heap = new (heapbuf) PerThreadHeap(); |
| 106 | } |
| 107 | heap->register_free(sz, ptr); |
| 108 | } |
| 109 | |
| 110 | inline void * memalign(size_t alignment, size_t sz) { |
| 111 | if (heap == nullptr) { |
| 112 | heap = new (heapbuf) PerThreadHeap(); |
| 113 | } |
| 114 | return heap->memalign(alignment, sz); |
| 115 | } |
| 116 | |
| 117 | inline size_t getSize(void * ptr) { |
| 118 | if (heap == nullptr) { |
| 119 | heap = new (heapbuf) PerThreadHeap(); |
| 120 | } |
| 121 | return heap->getSize(ptr); |
| 122 | } |
| 123 | |
| 124 | enum { Alignment = PerThreadHeap::Alignment }; |
| 125 | }; |
| 126 | |
| 127 | template <class PerThreadHeap> |
| 128 | alignas(16) __thread char ThreadSpecificHeap<PerThreadHeap>::heapbuf[sizeof(PerThreadHeap)] INITIAL_EXEC_ATTR; |
| 129 | |
| 130 | template <class PerThreadHeap> |
| 131 | __thread PerThreadHeap * ThreadSpecificHeap<PerThreadHeap>::heap INITIAL_EXEC_ATTR = 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__) |
| 219 | #pragma clang diagnostic pop |
| 220 | #endif |
| 221 | |
| 222 | #endif |
| 223 | |
| 224 | #endif |