Debian Codegraph @

vendor/Heap-Layers/utility/bins.h

raw symbols SCIP
1 /*
2
3 Heap Layers: An Extensible Memory Allocation Infrastructure
4
5 Copyright (C) 2000-2020 by Emery Berger
6 http://www.emeryberger.com
7 [email protected]
8
9 Heap Layers is distributed under the terms of the Apache 2.0 license.
10
11 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 */
15
16 #pragma once
17
18 #ifndef HL_BINS_H
19 #define HL_BINS_H
20
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <stddef.h>
24
25 #include "ilog2.h"
26 //#include "unistd.h"
27
28 namespace HL {
29
30 template <class Header, size_t Size>
31 class bins;
32
33 /// Default, generic, power of two bins for undefined sizes.
34 template <class Header, size_t Size>
35 class bins {
36 public:
37
38 bins()
39 {
40 static_assert(BIG_OBJECT > 0, "BIG_OBJECT must be positive.");
41 static_assert(getClassSize(0) < getClassSize(1), "Need distinct size classes.");
42 static_assert(getSizeClass(getClassSize(0)) == 0, "Size class computation error.");
43 static_assert(getSizeClass(0) >= getSizeClass(alignof(max_align_t)), "Min size must be at least alignof(max_align_t).");
44 #ifndef NDEBUG
45 int bins = 0;
46 for (auto i = alignof(max_align_t); i < BIG_OBJECT; i++) {
47 bins++;
48 int sc = getSizeClass(i);
49 assert (getClassSize(sc) >= i);
50 assert (sc == 0 ? true : (getClassSize(sc-1) < i));
51 assert (getSizeClass(getClassSize(sc)) == sc);
52 }
53 assert(bins == NUM_BINS);
54 #endif
55 }
56
57 private:
58
59 // constexpr integer log base two calculations, ONLY for use during compilation.
60
61 static constexpr inline unsigned int ilog2(const size_t n) {
62 return ((n<=1) ? 0 : 1 + ilog2(n/2));
63 }
64
65 static constexpr inline auto ilog2_ceil (const size_t n)
66 {
67 return ilog2(n * 2 - 1);
68 }
69
70
71 public:
72
73 enum { BIG_OBJECT = Size / 8 }; // - sizeof(Header) };
74 enum { NUM_BINS = ilog2_ceil(Size) - ilog2_ceil(alignof(max_align_t)) + 1 };
75 enum { NumBins = NUM_BINS };
76 enum { MaxObjectSize = BIG_OBJECT };
77 enum { LogMaxAlignT = ilog2(alignof(max_align_t)) };
78
79 static inline constexpr int getSizeClass (size_t sz) {
80 sz = (sz < alignof(max_align_t)) ? alignof(max_align_t) : sz;
81 auto sizeClass = (int) HL::ilog2(sz) - LogMaxAlignT; // (int) HL::ilog2(alignof(max_align_t));
82 return sizeClass;
83 }
84
85 static constexpr inline size_t getClassSize(int i) {
86 return (alignof(max_align_t) << i);
87 }
88
89 static constexpr inline size_t getClassMaxSize(int i) {
90 return getClassSize(i);
91 }
92
93 };
94
95 }
96
97
98 #endif