| 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_KINGSLEYHEAP_H |
| 19 | #define HL_KINGSLEYHEAP_H |
| 20 | |
| 21 | #include "utility/ilog2.h" |
| 22 | #include "heaps/combining/strictsegheap.h" |
| 23 | |
| 24 | /** |
| 25 | * @file kingsleyheap.h |
| 26 | * @brief Classes to implement a Kingsley (power-of-two, segregated fits) allocator. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * @namespace Kingsley |
| 31 | * @brief Functions to implement KingsleyHeap. |
| 32 | */ |
| 33 | |
| 34 | namespace Kingsley { |
| 35 | |
| 36 | inline size_t class2Size (const int i) { |
| 37 | auto sz = (size_t) (1ULL << (i+3)); |
| 38 | return sz; |
| 39 | } |
| 40 | |
| 41 | inline int size2Class (const size_t sz) { |
| 42 | auto cl = (int) HL::ilog2 ((sz < 8) ? 8 : sz) - 3; |
| 43 | return cl; |
| 44 | } |
| 45 | |
| 46 | enum { NUMBINS = 29 }; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @class KingsleyHeap |
| 52 | * @brief The Kingsley-style allocator. |
| 53 | * @param PerClassHeap The heap to use for each size class. |
| 54 | * @param BigHeap The heap for "large" objects. |
| 55 | * @see Kingsley |
| 56 | */ |
| 57 | |
| 58 | namespace HL { |
| 59 | |
| 60 | template <class PerClassHeap, class BigHeap> |
| 61 | class KingsleyHeap : |
| 62 | public StrictSegHeap<Kingsley::NUMBINS, |
| 63 | Kingsley::size2Class, |
| 64 | Kingsley::class2Size, |
| 65 | PerClassHeap, |
| 66 | BigHeap> {}; |
| 67 | |
| 68 | } |
| 69 | |
| 70 | #endif |