| 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_STLALLOCATOR_H |
| 19 | #define HL_STLALLOCATOR_H |
| 20 | |
| 21 | #include <cstdio> |
| 22 | #include <new> |
| 23 | #include <cstdlib> |
| 24 | #include <limits> |
| 25 | |
| 26 | #include <memory> // STL |
| 27 | |
| 28 | // Somewhere someone is defining a max macro (on Windows), |
| 29 | // and this is a problem -- solved by undefining it. |
| 30 | |
| 31 | #if defined(max) |
| 32 | #undef max |
| 33 | #endif |
| 34 | |
| 35 | using namespace std; |
| 36 | |
| 37 | /** |
| 38 | * @class STLAllocator |
| 39 | * @brief An allocator adapter for STL. |
| 40 | * |
| 41 | * This mixin lets you use any Heap Layers allocator as the allocator |
| 42 | * for an STL container. |
| 43 | * |
| 44 | * Example: |
| 45 | * <TT> |
| 46 | * typedef STLAllocator<int, MyHeapType> MyAllocator;<BR> |
| 47 | * list<int, MyAllocator> l;<BR> |
| 48 | * </TT> |
| 49 | */ |
| 50 | |
| 51 | namespace HL { |
| 52 | |
| 53 | template <class T, class Super> |
| 54 | class STLAllocator : public Super { |
| 55 | public: |
| 56 | |
| 57 | typedef T value_type; |
| 58 | typedef T * pointer; |
| 59 | typedef const T * const_pointer; |
| 60 | typedef T& reference; |
| 61 | typedef const T& const_reference; |
| 62 | typedef std::size_t size_type; |
| 63 | typedef std::ptrdiff_t difference_type; |
| 64 | typedef std::true_type propagate_on_container_move_assignment; |
| 65 | |
| 66 | template <class U> |
| 67 | struct rebind { |
| 68 | typedef STLAllocator<U,Super> other; |
| 69 | }; |
| 70 | |
| 71 | pointer address (reference x) const { |
| 72 | return &x; |
| 73 | } |
| 74 | |
| 75 | const_pointer address (const_reference x) const { |
| 76 | return &x; |
| 77 | } |
| 78 | |
| 79 | STLAllocator() throw() { |
| 80 | } |
| 81 | |
| 82 | STLAllocator (const STLAllocator& s) throw() |
| 83 | : Super (s) |
| 84 | {} |
| 85 | |
| 86 | virtual ~STLAllocator() throw() { |
| 87 | } |
| 88 | |
| 89 | /// Make the maximum size be the largest possible object. |
| 90 | size_type max_size() const |
| 91 | { |
| 92 | return std::numeric_limits<std::size_t>::max() / sizeof(T); |
| 93 | } |
| 94 | |
| 95 | #if defined(_WIN32) |
| 96 | char * _Charalloc (size_type n) { |
| 97 | return (char *) allocate (n); |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | #if defined(__SUNPRO_CC) |
| 102 | inline void * allocate (size_type n, |
| 103 | const void * = 0) { |
| 104 | if (n) { |
| 105 | return reinterpret_cast<void *>(Super::malloc (sizeof(T) * n)); |
| 106 | } else { |
| 107 | return (void *) 0; |
| 108 | } |
| 109 | } |
| 110 | #else |
| 111 | inline pointer allocate (size_type n, |
| 112 | const void * = 0) { |
| 113 | if (n) { |
| 114 | return reinterpret_cast<pointer>(Super::malloc (sizeof(T) * n)); |
| 115 | } else { |
| 116 | return 0; |
| 117 | } |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | inline void deallocate (void * p, size_type) { |
| 122 | Super::free (p); |
| 123 | } |
| 124 | |
| 125 | inline void deallocate (pointer p, size_type) { |
| 126 | Super::free (p); |
| 127 | } |
| 128 | |
| 129 | #if __cplusplus >= 201103L |
| 130 | // Updated API from C++11 to work with move constructors |
| 131 | template<class U, class... Args> |
| 132 | void construct (U* p, Args&&... args) { |
| 133 | new((void *) p) U(std::forward<Args>(args)...); |
| 134 | } |
| 135 | |
| 136 | template<class U> |
| 137 | void destroy (U* p) { |
| 138 | p->~U(); |
| 139 | } |
| 140 | |
| 141 | #else |
| 142 | // Legacy API for pre-C++11 |
| 143 | void construct (pointer p, const T& val) { |
| 144 | new ((void *) p) T (val); |
| 145 | } |
| 146 | |
| 147 | void destroy (pointer p) { |
| 148 | ((T*)p)->~T(); |
| 149 | } |
| 150 | |
| 151 | #endif |
| 152 | |
| 153 | template <class U> STLAllocator (const STLAllocator<U, Super> &) throw() |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | }; |
| 158 | |
| 159 | template <typename T, class S> |
| 160 | inline bool operator!=(const STLAllocator<T,S>& a, const STLAllocator<T,S>& b) { |
| 161 | return (&a != &b); |
| 162 | } |
| 163 | |
| 164 | template <typename T, class S> |
| 165 | inline bool operator==(const STLAllocator<T,S>& a, const STLAllocator<T,S>& b) { |
| 166 | return (&a == &b); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | } |
| 171 | |
| 172 | |
| 173 | #endif |