| 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 |
#if !defined(HL_BINS64K_H) |
| 19 |
#define HL_BINS64K_H |
| 20 |
|
| 21 |
#include <cstdlib> |
| 22 |
#include <assert.h> |
| 23 |
#include <stddef.h> |
| 24 |
|
| 25 |
#include "bins.h" |
| 26 |
#include "ilog2.h" |
| 27 |
|
| 28 |
namespace HL { |
| 29 |
|
| 30 |
template <class Header> |
| 31 |
class bins<Header, 65536> { |
| 32 |
public: |
| 33 |
|
| 34 |
bins() |
| 35 |
{ |
| 36 |
static_assert(BIG_OBJECT > 0, "BIG_OBJECT must be positive."); |
| 37 |
static_assert(getClassSize(0) < getClassSize(1), "Need distinct size classes."); |
| 38 |
static_assert(getSizeClass(getClassSize(0)) == 0, "Size class computation error."); |
| 39 |
static_assert(getSizeClass(0) >= sizeof(max_align_t), "Min size must be at least max_align_t."); |
| 40 |
static_assert(getSizeClass(0) >= alignof(max_align_t), "Min size must be at least alignof(max_align_t)."); |
| 41 |
#ifndef NDEBUG |
| 42 |
for (int i = sizeof(double); i < BIG_OBJECT; i++) { |
| 43 |
int sc = getSizeClass(i); |
| 44 |
assert (getClassSize(sc) >= i); |
| 45 |
assert (getClassSize(sc-1) < i); |
| 46 |
assert (getSizeClass(getClassSize(sc)) == sc); |
| 47 |
} |
| 48 |
#endif |
| 49 |
} |
| 50 |
|
| 51 |
enum { BIG_OBJECT = 8192 }; |
| 52 |
// enum { NUM_BINS = 512 }; |
| 53 |
enum { NUM_BINS = 11 }; |
| 54 |
|
| 55 |
static inline constexpr int getSizeClass (size_t sz) { |
| 56 |
#if 0 |
| 57 |
if (sz < 16) { |
| 58 |
sz = 16; |
| 59 |
} else { |
| 60 |
sz = ((sz + 15) & ~15UL); |
| 61 |
} |
| 62 |
return (sz >> 4) - 1; |
| 63 |
// |
| 64 |
sz = ((sz + 15) & ~15UL); |
| 65 |
const int szClass[] = { 0, 1, 2, 2, 3, 3, 3, 3 }; |
| 66 |
const auto szIndex = sz >> 3; |
| 67 |
if (szIndex < 8) { |
| 68 |
return szClass[szIndex]; |
| 69 |
} |
| 70 |
#endif |
| 71 |
sz = (sz < sizeof(double)) ? sizeof(double) : sz; |
| 72 |
return (int) HL::ilog2(sz) - 3; |
| 73 |
} |
| 74 |
|
| 75 |
static constexpr inline size_t getClassSize (int i) { |
| 76 |
#if 0 |
| 77 |
assert (i >= 0); |
| 78 |
return (i+1) << 4; |
| 79 |
#endif |
| 80 |
return (sizeof(double) << i); |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#endif |