| 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 "heaps/combining/strictsegheap.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* @file kingsleyheap.h |
| 25 |
* @brief Classes to implement a Kingsley (power-of-two, segregated fits) allocator. |
| 26 |
*/ |
| 27 |
|
| 28 |
/** |
| 29 |
* @namespace Kingsley |
| 30 |
* @brief Functions to implement KingsleyHeap. |
| 31 |
*/ |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
namespace Kingsley { |
| 36 |
|
| 37 |
size_t class2Size (const int i); |
| 38 |
|
| 39 |
#if defined(__sparc) && defined(__GNUC__) |
| 40 |
inline int popc (int v) { |
| 41 |
int r; |
| 42 |
asm volatile ("popc %1, %0" |
| 43 |
: "=r" (r) |
| 44 |
: "r" (v)); |
| 45 |
return r; |
| 46 |
} |
| 47 |
#endif |
| 48 |
|
| 49 |
/** |
| 50 |
* A speed optimization: |
| 51 |
* we use this array to quickly return the size class of objects |
| 52 |
* from 8 to 128 bytes. |
| 53 |
*/ |
| 54 |
const int cl[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; |
| 55 |
|
| 56 |
inline int size2Class (const size_t sz) { |
| 57 |
#if defined(__sparc) && defined(__GNUC__) |
| 58 |
// Adapted from _Hacker's Delight_, by Henry Warren (p.80) |
| 59 |
size_t x = sz; |
| 60 |
x = x | (x >> 1); |
| 61 |
x = x | (x >> 2); |
| 62 |
x = x | (x >> 4); |
| 63 |
x = x | (x >> 8); |
| 64 |
x = x | (x >> 16); |
| 65 |
return popc(x) - 3; |
| 66 |
#else |
| 67 |
if (sz < 128 ) { |
| 68 |
assert (class2Size(cl[sz >> 3]) >= sz); |
| 69 |
return cl[(sz - 1) >> 3]; |
| 70 |
} else { |
| 71 |
// |
| 72 |
// We know that the object is at least 128 bytes long, |
| 73 |
// so we can avoid iterating 4 times. |
| 74 |
// |
| 75 |
int c = 4; |
| 76 |
size_t sz1 = ((sz - 1) >> 4); |
| 77 |
while (sz1 > 7) { |
| 78 |
sz1 >>= 1; |
| 79 |
c++; |
| 80 |
} |
| 81 |
assert (class2Size(c) >= sz); |
| 82 |
return c; |
| 83 |
} |
| 84 |
#endif |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
inline size_t class2Size (const int i) { |
| 89 |
return (size_t) (1 << (i+3)); |
| 90 |
} |
| 91 |
|
| 92 |
enum { NUMBINS = 29 }; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @class KingsleyHeap |
| 98 |
* @brief The Kingsley-style allocator. |
| 99 |
* @param PerClassHeap The heap to use for each size class. |
| 100 |
* @param BigHeap The heap for "large" objects. |
| 101 |
* @see Kingsley |
| 102 |
*/ |
| 103 |
|
| 104 |
namespace HL { |
| 105 |
|
| 106 |
template <class PerClassHeap, class BigHeap> |
| 107 |
class KingsleyHeap : |
| 108 |
public StrictSegHeap<Kingsley::NUMBINS, |
| 109 |
Kingsley::size2Class, |
| 110 |
Kingsley::class2Size, |
| 111 |
PerClassHeap, |
| 112 |
BigHeap> {}; |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
#endif |