1 | // -*- C++ -*- |
---|
2 | |
---|
3 | /* |
---|
4 | |
---|
5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
6 | |
---|
7 | Copyright (C) 2000-2003 by Emery Berger |
---|
8 | http://www.cs.umass.edu/~emery |
---|
9 | emery@cs.umass.edu |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify |
---|
12 | it under the terms of the GNU General Public License as published by |
---|
13 | the Free Software Foundation; either version 2 of the License, or |
---|
14 | (at your option) any later version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | GNU General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
24 | |
---|
25 | */ |
---|
26 | |
---|
27 | #ifndef _KINGSLEYHEAP_H_ |
---|
28 | #define _KINGSLEYHEAP_H_ |
---|
29 | |
---|
30 | #include "segheap.h" |
---|
31 | |
---|
32 | /** |
---|
33 | * @file kingsleyheap.h |
---|
34 | * @brief Classes to implement a Kingsley (power-of-two, segregated fits) allocator. |
---|
35 | */ |
---|
36 | |
---|
37 | /** |
---|
38 | * @namespace Kingsley |
---|
39 | * @brief Functions to implement KingsleyHeap. |
---|
40 | */ |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | namespace Kingsley { |
---|
45 | |
---|
46 | size_t class2Size (const int i); |
---|
47 | |
---|
48 | #if defined(__sparc) && defined(__GNUC__) |
---|
49 | inline int popc (int v) { |
---|
50 | int r; |
---|
51 | asm volatile ("popc %1, %0" |
---|
52 | : "=r" (r) |
---|
53 | : "r" (v)); |
---|
54 | return r; |
---|
55 | } |
---|
56 | #endif |
---|
57 | |
---|
58 | /** |
---|
59 | * A speed optimization: |
---|
60 | * we use this array to quickly return the size class of objects |
---|
61 | * from 8 to 128 bytes. |
---|
62 | */ |
---|
63 | const int cl[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; |
---|
64 | |
---|
65 | inline int size2Class (const size_t sz) { |
---|
66 | #if defined(__sparc) && defined(__GNUC__) |
---|
67 | // Adapted from _Hacker's Delight_, by Henry Warren (p.80) |
---|
68 | size_t x = sz; |
---|
69 | x = x | (x >> 1); |
---|
70 | x = x | (x >> 2); |
---|
71 | x = x | (x >> 4); |
---|
72 | x = x | (x >> 8); |
---|
73 | x = x | (x >> 16); |
---|
74 | return popc(x) - 3; |
---|
75 | #else |
---|
76 | if (sz < 128 ) { |
---|
77 | assert (class2Size(cl[sz >> 3]) >= sz); |
---|
78 | return cl[(sz - 1) >> 3]; |
---|
79 | } else { |
---|
80 | // |
---|
81 | // We know that the object is more than 128 bytes long, |
---|
82 | // so we can avoid iterating 5 times. |
---|
83 | // |
---|
84 | int c = 5; |
---|
85 | size_t sz1 = ((sz - 1) >> 5); |
---|
86 | while (sz1 > 7) { |
---|
87 | sz1 >>= 1; |
---|
88 | c++; |
---|
89 | } |
---|
90 | assert (class2Size(c) >= sz); |
---|
91 | return c; |
---|
92 | } |
---|
93 | #endif |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | inline size_t class2Size (const int i) { |
---|
98 | return (size_t) (1 << (i+3)); |
---|
99 | } |
---|
100 | |
---|
101 | enum { NUMBINS = 29 }; |
---|
102 | |
---|
103 | }; |
---|
104 | |
---|
105 | /** |
---|
106 | * @class KingsleyHeap |
---|
107 | * @brief The Kingsley-style allocator. |
---|
108 | * @param PerClassHeap The heap to use for each size class. |
---|
109 | * @param BigHeap The heap for "large" objects. |
---|
110 | * @see Kingsley |
---|
111 | */ |
---|
112 | |
---|
113 | namespace HL { |
---|
114 | |
---|
115 | template <class PerClassHeap, class BigHeap> |
---|
116 | class KingsleyHeap : |
---|
117 | public StrictSegHeap<Kingsley::NUMBINS, |
---|
118 | Kingsley::size2Class, |
---|
119 | Kingsley::class2Size, |
---|
120 | PerClassHeap, |
---|
121 | BigHeap> {}; |
---|
122 | |
---|
123 | }; |
---|
124 | |
---|
125 | #endif |
---|