| 1 | /* -*- C++ -*- */ |
| 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_H |
| 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" |
| 37 | #include "heaps/special/zoneheap.h" |
| 38 | #include "heaps/special/bumpalloc.h" |
| 39 | #include "heaps/threads/lockedheap.h" |
| 40 | #include "locks/posixlock.h" |
| 41 | #include "threads/cpuinfo.h" |
| 42 | #include "wrappers/mmapwrapper.h" |
| 43 | #include "wrappers/stlallocator.h" |
| 44 | |
| 45 | #ifndef HL_MMAP_PROTECTION_MASK |
| 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_ANONYMOUS) && defined(MAP_ANON) |
| 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 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 SizedMmapHeap { |
| 72 | public: |
| 73 | |
| 74 | /// All memory from here is zeroed. |
| 75 | enum { ZeroMemory = 1 }; |
| 76 | |
| 77 | enum { Alignment = 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 * malloc (size_t sz) { |
| 109 | // Round up to the size of a page. |
| 110 | sz = (sz + CPUInfo::PageSize - 1) & (size_t) ~(CPUInfo::PageSize - 1); |
| 111 | void * addr = 0; |
| 112 | int flags = 0; |
| 113 | static int fd = -1; |
| 114 | #if defined(MAP_ALIGN) && defined(MAP_ANON) |
| 115 | addr = (void *)Alignment; |
| 116 | flags |= MAP_PRIVATE | MAP_ALIGN | MAP_ANON; |
| 117 | #elif !defined(MAP_ANONYMOUS) |
| 118 | if (fd == -1) { |
| 119 | fd = ::open ("/dev/zero", O_RDWR); |
| 120 | } |
| 121 | flags |= MAP_PRIVATE; |
| 122 | #else |
| 123 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
| 124 | #if HL_EXECUTABLE_HEAP |
| 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_MASK, flags, fd, 0); |
| 132 | if (ptr == MAP_FAILED) { |
| 133 | ptr = nullptr; |
| 134 | } |
| 135 | return ptr; |
| 136 | } |
| 137 | |
| 138 | static void free (void * ptr, size_t sz) |
| 139 | { |
| 140 | if ((long) sz < 0) { |
| 141 | abort(); |
| 142 | } |
| 143 | munmap (reinterpret_cast<char *>(ptr), sz); |
| 144 | } |
| 145 | |
| 146 | #endif |
| 147 | |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | class MmapHeap : public 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 MyHeap : public LockedHeap<PosixLockType, FreelistHeap<BumpAlloc<65536, SizedMmapHeap>>> {}; |
| 159 | //class MyHeap : public LockedHeap<PosixLockType, FreelistHeap<ZoneHeap<SizedMmapHeap, 65536>>> {}; |
| 160 | |
| 161 | typedef std::unordered_map<void *, |
| 162 | size_t, |
| 163 | std::hash<void *>, |
| 164 | std::equal_to<void *>, |
| 165 | HL::STLAllocator<std::pair<void * const, size_t>, MyHeap>> |
| 166 | mapType; |
| 167 | |
| 168 | mapType MyMap; |
| 169 | PosixLockType MyMapLock; |
| 170 | |
| 171 | public: |
| 172 | |
| 173 | enum { Alignment = SizedMmapHeap::Alignment }; |
| 174 | |
| 175 | inline void * malloc (size_t sz) { |
| 176 | void * ptr = SizedMmapHeap::malloc (sz); |
| 177 | MyMapLock.lock(); |
| 178 | MyMap[ptr] = sz; |
| 179 | MyMapLock.unlock(); |
| 180 | assert (reinterpret_cast<size_t>(ptr) % Alignment == 0); |
| 181 | return const_cast<void *>(ptr); |
| 182 | } |
| 183 | |
| 184 | inline size_t getSize (void * ptr) { |
| 185 | MyMapLock.lock(); |
| 186 | size_t sz = MyMap[ptr]; |
| 187 | MyMapLock.unlock(); |
| 188 | return sz; |
| 189 | } |
| 190 | |
| 191 | #if 1 |
| 192 | void free (void * ptr, size_t sz) { |
| 193 | SizedMmapHeap::free (ptr, sz); |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | inline void free (void * ptr) { |
| 198 | assert (reinterpret_cast<size_t>(ptr) % Alignment == 0); |
| 199 | MyMapLock.lock(); |
| 200 | size_t sz = MyMap[ptr]; |
| 201 | SizedMmapHeap::free (ptr, sz); |
| 202 | MyMap.erase (ptr); |
| 203 | MyMapLock.unlock(); |
| 204 | } |
| 205 | #endif |
| 206 | }; |
| 207 | |
| 208 | } |
| 209 | |
| 210 | #endif |