| 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 | #pragma once |
| 19 | #ifndef SYSMALLOCHEAP_HPP |
| 20 | #define SYSMALLOCHEAP_HPP |
| 21 | |
| 22 | namespace HL { |
| 23 | |
| 24 | /** |
| 25 | * @class SysMallocHeap |
| 26 | * @brief Forwards heap requests to the original system heap. |
| 27 | * @author Emery Berger, Juan Altmayer Pizzorno |
| 28 | **/ |
| 29 | |
| 30 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) |
| 31 | |
| 32 | // FIXME use the non-__APPLE__ code on __APPLE__ as well to reduce unnecessary variants? |
| 33 | class SysMallocHeap { |
| 34 | public: |
| 35 | enum { Alignment = alignof(max_align_t) }; |
| 36 | |
| 37 | inline void *malloc(size_t sz) { |
| 38 | return ::malloc(sz); |
| 39 | } |
| 40 | |
| 41 | inline void *memalign(size_t alignment, size_t size) { |
| 42 | void *buf; |
| 43 | if (::posix_memalign(&buf, alignment, size) != 0) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | return buf; |
| 47 | } |
| 48 | |
| 49 | inline bool free(void *ptr) { |
| 50 | ::free(ptr); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | inline size_t getSize(void *ptr) { |
| 55 | #if defined(__APPLE__) |
| 56 | return ::malloc_size(ptr); |
| 57 | #else |
| 58 | return ::malloc_usable_size(const_cast<void *>(ptr)); |
| 59 | #endif |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | #elif !defined(_WIN32) // __APPLE__ |
| 64 | |
| 65 | #include <dlfcn.h> |
| 66 | |
| 67 | extern "C" void * my_dlsym(void *, const char*); |
| 68 | |
| 69 | /** |
| 70 | * Provides access to the original system heap, even if the standard malloc/free/... |
| 71 | * have been redirected by way of LD_PRELOAD, MacOS interpose, etc. |
| 72 | */ |
| 73 | class SysMallocHeap { |
| 74 | |
| 75 | decltype(::malloc)* _malloc{0}; |
| 76 | decltype(::free)* _free{0}; |
| 77 | decltype(::memalign)* _memalign{0}; |
| 78 | decltype(::malloc_usable_size)* _malloc_usable_size{0}; |
| 79 | |
| 80 | public: |
| 81 | enum { Alignment = alignof(max_align_t) }; |
| 82 | |
| 83 | SysMallocHeap() |
| 84 | : _malloc ((decltype(::malloc) *) my_dlsym(RTLD_NEXT, "malloc")), |
| 85 | _free ((decltype(::free) *) my_dlsym(RTLD_NEXT, "free")), |
| 86 | _memalign ((decltype(::memalign) *) my_dlsym(RTLD_NEXT, "memalign")), |
| 87 | _malloc_usable_size ((decltype(::malloc_usable_size) *) my_dlsym(RTLD_NEXT, "malloc_usable_size")) |
| 88 | {} |
| 89 | |
| 90 | inline void *malloc(size_t sz) { |
| 91 | auto ptr = (*_malloc)(sz); |
| 92 | return ptr; |
| 93 | } |
| 94 | |
| 95 | inline void *memalign(size_t alignment, size_t sz) { |
| 96 | return (*_memalign)(alignment, sz); |
| 97 | } |
| 98 | |
| 99 | inline void free(void *ptr) { |
| 100 | (*_free)(ptr); |
| 101 | } |
| 102 | |
| 103 | inline size_t getSize(void *ptr) { |
| 104 | auto sz = (*_malloc_usable_size)(ptr); |
| 105 | return sz; |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | #endif |
| 110 | |
| 111 | } // namespace |
| 112 | |
| 113 | #endif |