| 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_MALLOCHEAP_H |
| 19 | #define HL_MALLOCHEAP_H |
| 20 | |
| 21 | #include <cstdlib> |
| 22 | |
| 23 | #if defined(__SVR4) |
| 24 | extern "C" size_t malloc_usable_size (void *); |
| 25 | #elif defined(__APPLE__) |
| 26 | #include <malloc/malloc.h> |
| 27 | #elif defined(__linux__) |
| 28 | #include <malloc.h> |
| 29 | #elif defined(__FreeBSD__) |
| 30 | #include <malloc_np.h> |
| 31 | #else |
| 32 | extern "C" size_t malloc_usable_size (void *) throw (); |
| 33 | #endif |
| 34 | |
| 35 | /** |
| 36 | * @class MallocHeap |
| 37 | * @brief A "source heap" that uses malloc and free. |
| 38 | */ |
| 39 | |
| 40 | #include "wrappers/mallocinfo.h" |
| 41 | |
| 42 | |
| 43 | namespace HL { |
| 44 | |
| 45 | class MallocHeap { |
| 46 | public: |
| 47 | |
| 48 | enum { Alignment = MallocInfo::Alignment }; |
| 49 | |
| 50 | inline void * malloc (size_t sz) { |
| 51 | return ::malloc (sz); |
| 52 | } |
| 53 | |
| 54 | inline void free (void * ptr) { |
| 55 | ::free (ptr); |
| 56 | } |
| 57 | |
| 58 | #if defined(_MSC_VER) |
| 59 | inline size_t getSize (void * ptr) { |
| 60 | return ::_msize (ptr); |
| 61 | } |
| 62 | #elif defined(__APPLE__) |
| 63 | inline size_t getSize (void * ptr) { |
| 64 | return ::malloc_size (ptr); |
| 65 | } |
| 66 | #elif defined(__GNUC__) && !defined(__SVR4) |
| 67 | inline size_t getSize (void * ptr) { |
| 68 | return ::malloc_usable_size (ptr); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | }; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | #endif |