| 1 | /* -*- C++ -*- */cxx . . $ `<file>/vendor/Heap-Layers/heaps/top/mmapheap.h`/ |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_MMAPHEAP_H |
| 19 | #define HL_MMAPHEAP_Hcxx . . $ `vendor/Heap-Layers/heaps/top/mmapheap.h:19:9`! |
| 20 | |
| 21 | #if defined(_WIN32) |
| 22 | #include <windows.h> |
| 23 | #else |
| 24 | // UNIX |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <map> |
| 31 | #endif |
| 32 | |
| 33 | #include <new> |
| 34 | #include <unordered_map> |
| 35 | |
| 36 | #include "heaps/buildingblock/freelistheap.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/buildingblock/freelistheap.h`/ |
| 37 | #include "heaps/special/zoneheap.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/special/zoneheap.h`/ |
| 38 | #include "heaps/special/bumpalloc.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/special/bumpalloc.h`/ |
| 39 | #include "heaps/threads/lockedheap.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/threads/lockedheap.h`/ |
| 40 | #include "locks/posixlock.h"cxx . . $ `<file>/vendor/Heap-Layers/locks/posixlock.h`/ |
| 41 | #include "threads/cpuinfo.h"cxx . . $ `<file>/vendor/Heap-Layers/threads/cpuinfo.h`/ |
| 42 | #include "wrappers/mmapwrapper.h"cxx . . $ `<file>/vendor/Heap-Layers/wrappers/mmapwrapper.h`/ |
| 43 | #include "wrappers/stlallocator.h"cxx . . $ `<file>/vendor/Heap-Layers/wrappers/stlallocator.h`/ |
| 44 | |
| 45 | #ifndef HL_MMAP_PROTECTION_MASKcxx . . $ `vendor/Heap-Layers/wrappers/mmapwrapper.h:54:9`! |
| 46 | #if HL_EXECUTABLE_HEAP |
| 47 | #define HL_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE | PROT_EXEC) |
| 48 | #else |
| 49 | #define HL_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE) |
| 50 | #endif |
| 51 | #endif |
| 52 | |
| 53 | |
| 54 | #if !defined(MAP_ANONYMOUScxx . . $ `<external>/17226767460805440860/mman-linux.h:55:10`!) && defined(MAP_ANONcxx . . $ `<external>/17226767460805440860/mman-linux.h:57:9`!) |
| 55 | #define MAP_ANONYMOUS MAP_ANON |
| 56 | #endif |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * @class MmapHeap |
| 61 | * @brief A "source heap" that manages memory via calls to the VM interface. |
| 62 | * @author Emery Berger |
| 63 | */ |
| 64 | |
| 65 | namespace HLcxx . . $ HL/ { |
| 66 | |
| 67 | /** |
| 68 | * @class SizedMmapHeap |
| 69 | * @brief A heap around mmap, but only a sized-free is supported for Unix-like systems. |
| 70 | */ |
| 71 | class SizedMmapHeapcxx . . $ HL/SizedMmapHeap# { |
| 72 | public: |
| 73 | |
| 74 | /// All memory from here is zeroed. |
| 75 | enumcxx . . $ HL/SizedMmapHeap#$anonymous_type_0# { ZeroMemorycxx . . $ HL/SizedMmapHeap#ZeroMemory. = 1 }; |
| 76 | |
| 77 | enumcxx . . $ HL/SizedMmapHeap#$anonymous_type_1# { Alignmentcxx . . $ HL/SizedMmapHeap#Alignment. = MmapWrappercxx . . $ HL/MmapWrapper#::Alignmentcxx . . $ HL/MmapWrapper#Alignment. }; |
| 78 | |
| 79 | #if defined(_WIN32) |
| 80 | |
| 81 | static inline void * malloc (size_t sz) { |
| 82 | #if HL_EXECUTABLE_HEAP |
| 83 | char * ptr = (char *) VirtualAlloc (NULL, sz, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_EXECUTE_READWRITE); |
| 84 | #else |
| 85 | char * ptr = (char *) VirtualAlloc (NULL, sz, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE); |
| 86 | #endif |
| 87 | return (void *) ptr; |
| 88 | } |
| 89 | |
| 90 | static inline void free (void * ptr, size_t) { |
| 91 | // No need to keep track of sizes in Windows. |
| 92 | VirtualFree (ptr, 0, MEM_RELEASE); |
| 93 | } |
| 94 | |
| 95 | static inline void free (void * ptr) { |
| 96 | // No need to keep track of sizes in Windows. |
| 97 | VirtualFree (ptr, 0, MEM_RELEASE); |
| 98 | } |
| 99 | |
| 100 | inline static size_t getSize (void * ptr) { |
| 101 | MEMORY_BASIC_INFORMATION mbi; |
| 102 | VirtualQuery (ptr, &mbi, sizeof(mbi)); |
| 103 | return (size_t) mbi.RegionSize; |
| 104 | } |
| 105 | |
| 106 | #else |
| 107 | |
| 108 | static inline void * malloccxx . . $ HL/SizedMmapHeap#malloc(bfcfa4d6b7f7ef64). (size_t sz) { |
| 109 | // Round up to the size of a page. |
| 110 | sz = (sz + CPUInfocxx . . $ HL/CPUInfo#::PageSizecxx . . $ HL/CPUInfo#PageSize. - 1) & (size_t) ~(CPUInfocxx . . $ HL/CPUInfo#::PageSizecxx . . $ HL/CPUInfo#PageSize. - 1); |
| 111 | void * addr = 0; |
| 112 | int flags = 0; |
| 113 | static int fd = -1; |
| 114 | #if defined(MAP_ALIGN) && defined(MAP_ANONcxx . . $ `<external>/17226767460805440860/mman-linux.h:57:9`!) |
| 115 | addr = (void *)Alignment; |
| 116 | flags |= MAP_PRIVATE | MAP_ALIGN | MAP_ANON; |
| 117 | #elif !defined(MAP_ANONYMOUScxx . . $ `<external>/17226767460805440860/mman-linux.h:55:10`!) |
| 118 | if (fd == -1) { |
| 119 | fd = ::open ("/dev/zero", O_RDWR); |
| 120 | } |
| 121 | flags |= MAP_PRIVATE; |
| 122 | #else |
| 123 | flags |= MAP_PRIVATEcxx . . $ `<external>/17226767460805440860/mman-linux.h:43:9`! | MAP_ANONYMOUScxx . . $ `<external>/17226767460805440860/mman-linux.h:55:10`!; |
| 124 | #if HL_EXECUTABLE_HEAPcxx . . $ `vendor/Heap-Layers/heaplayers.h:41:9`! |
| 125 | #if defined(MAP_JIT) |
| 126 | flags |= MAP_JIT; |
| 127 | #endif |
| 128 | #endif |
| 129 | #endif |
| 130 | |
| 131 | auto ptr = mmap (addr, sz, HL_MMAP_PROTECTION_MASKcxx . . $ `vendor/Heap-Layers/wrappers/mmapwrapper.h:54:9`!, flags, fd, 0); |
| 132 | if (ptr == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) { |
| 133 | ptr = nullptr; |
| 134 | } |
| 135 | return ptr; |
| 136 | } |
| 137 | |
| 138 | static void freecxx . . $ HL/SizedMmapHeap#free(5500c4293b1bbef0). (void * ptr, size_t sz) |
| 139 | { |
| 140 | if ((long) sz < 0) { |
| 141 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 142 | } |
| 143 | munmap (reinterpret_cast<char *>(ptr), sz); |
| 144 | } |
| 145 | |
| 146 | #endif |
| 147 | |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | class MmapHeapcxx . . $ HL/MmapHeap# : public SizedMmapHeapcxx . . $ HL/SizedMmapHeap# { |
| 152 | #if !defined(_WIN32) |
| 153 | |
| 154 | private: |
| 155 | |
| 156 | // Note: we never reclaim memory obtained for MyHeap, even when |
| 157 | // this heap is destroyed. |
| 158 | class MyHeapcxx . . $ HL/MmapHeap#MyHeap# : public LockedHeapcxx . . $ HL/LockedHeap#<PosixLockTypecxx . . $ HL/PosixLockType#, FreelistHeapcxx . . $ HL/FreelistHeap#<BumpAlloccxx . . $ HL/BumpAlloc#<65536, SizedMmapHeapcxx . . $ HL/SizedMmapHeap#>>> {}; |
| 159 | //class MyHeap : public LockedHeap<PosixLockType, FreelistHeap<ZoneHeap<SizedMmapHeap, 65536>>> {}; |
| 160 | |
| 161 | typedef stdcxx . . $ std/::unordered_mapcxx . . $ std/unordered_map#<void *, |
| 162 | size_t, |
| 163 | stdcxx . . $ std/::hashcxx . . $ std/hash#<void *>, |
| 164 | stdcxx . . $ std/::equal_tocxx . . $ std/equal_to#<void *>, |
| 165 | HLcxx . . $ HL/::STLAllocatorcxx . . $ HL/STLAllocator#<stdcxx . . $ std/::paircxx . . $ std/pair#<void * const, size_t>, MyHeapcxx . . $ HL/MmapHeap#MyHeap#>> |
| 166 | mapTypecxx . . $ HL/MmapHeap#mapType#; |
| 167 | |
| 168 | mapTypecxx . . $ HL/MmapHeap#mapType# MyMapcxx . . $ HL/MmapHeap#MyMap.; |
| 169 | PosixLockTypecxx . . $ HL/PosixLockType# MyMapLockcxx . . $ HL/MmapHeap#MyMapLock.; |
| 170 | |
| 171 | public: |
| 172 | |
| 173 | enumcxx . . $ HL/MmapHeap#$anonymous_type_2# { Alignmentcxx . . $ HL/MmapHeap#Alignment. = SizedMmapHeapcxx . . $ HL/SizedMmapHeap#::Alignmentcxx . . $ HL/SizedMmapHeap#Alignment. }; |
| 174 | |
| 175 | inline void * malloccxx . . $ HL/MmapHeap#malloc(bfcfa4d6b7f7ef64). (size_t sz) { |
| 176 | void * ptr = SizedMmapHeapcxx . . $ HL/SizedMmapHeap#::malloccxx . . $ HL/SizedMmapHeap#malloc(bfcfa4d6b7f7ef64). (sz); |
| 177 | MyMapLockcxx . . $ HL/MmapHeap#MyMapLock..lockcxx . . $ HL/PosixLockType#lock(49f6e7a06ebc5aa8).(); |
| 178 | MyMapcxx . . $ HL/MmapHeap#MyMap.[cxx . . $ std/unordered_map#`operator[]`(4f10b6009f0ee734).ptr] = sz; |
| 179 | MyMapLockcxx . . $ HL/MmapHeap#MyMapLock..unlockcxx . . $ HL/PosixLockType#unlock(49f6e7a06ebc5aa8).(); |
| 180 | assertcxx . . $ `/usr/include/assert.h:50:10`! (reinterpret_cast<size_t>(ptr) % Alignment == 0); |
| 181 | return const_cast<void *>(ptr); |
| 182 | } |
| 183 | |
| 184 | inline size_t getSizecxx . . $ HL/MmapHeap#getSize(751e5d26f8574c32). (void * ptr) { |
| 185 | MyMapLockcxx . . $ HL/MmapHeap#MyMapLock..lockcxx . . $ HL/PosixLockType#lock(49f6e7a06ebc5aa8).(); |
| 186 | size_t sz = MyMapcxx . . $ HL/MmapHeap#MyMap.[cxx . . $ std/unordered_map#`operator[]`(4f10b6009f0ee734).ptr]; |
| 187 | MyMapLockcxx . . $ HL/MmapHeap#MyMapLock..unlockcxx . . $ HL/PosixLockType#unlock(49f6e7a06ebc5aa8).(); |
| 188 | return sz; |
| 189 | } |
| 190 | |
| 191 | #if 1 |
| 192 | void freecxx . . $ HL/MmapHeap#free(5500c4293b1bbef0). (void * ptr, size_t sz) { |
| 193 | SizedMmapHeapcxx . . $ HL/SizedMmapHeap#::freecxx . . $ HL/SizedMmapHeap#free(5500c4293b1bbef0). (ptr, sz); |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | inline void freecxx . . $ HL/MmapHeap#free(5d22bdacc48458e8). (void * ptr) { |
| 198 | assertcxx . . $ `/usr/include/assert.h:50:10`! (reinterpret_cast<size_t>(ptr) % Alignment == 0); |
| 199 | MyMapLockcxx . . $ HL/MmapHeap#MyMapLock..lockcxx . . $ HL/PosixLockType#lock(49f6e7a06ebc5aa8).(); |
| 200 | size_t sz = MyMapcxx . . $ HL/MmapHeap#MyMap.[cxx . . $ std/unordered_map#`operator[]`(4f10b6009f0ee734).ptr]; |
| 201 | SizedMmapHeapcxx . . $ HL/SizedMmapHeap#::freecxx . . $ HL/SizedMmapHeap#free(5500c4293b1bbef0). (ptr, sz); |
| 202 | MyMapcxx . . $ HL/MmapHeap#MyMap..erasecxx . . $ std/unordered_map#erase(ff4e454bb663bf95). (ptr); |
| 203 | MyMapLockcxx . . $ HL/MmapHeap#MyMapLock..unlockcxx . . $ HL/PosixLockType#unlock(49f6e7a06ebc5aa8).(); |
| 204 | } |
| 205 | #endif |
| 206 | }; |
| 207 | |
| 208 | } |
| 209 | |
| 210 | #endif |