]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | // Copyright (c) 2005, Google Inc. |
2 | // All rights reserved. | |
3 | // | |
4 | // Redistribution and use in source and binary forms, with or without | |
5 | // modification, are permitted provided that the following conditions are | |
6 | // met: | |
7 | // | |
8 | // * Redistributions of source code must retain the above copyright | |
9 | // notice, this list of conditions and the following disclaimer. | |
10 | // * Redistributions in binary form must reproduce the above | |
11 | // copyright notice, this list of conditions and the following disclaimer | |
12 | // in the documentation and/or other materials provided with the | |
13 | // distribution. | |
14 | // * Neither the name of Google Inc. nor the names of its | |
15 | // contributors may be used to endorse or promote products derived from | |
16 | // this software without specific prior written permission. | |
17 | // | |
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | ||
30 | // --- | |
31 | // Author: Sanjay Ghemawat <opensource@google.com> | |
32 | // | |
33 | // A data structure used by the caching malloc. It maps from page# to | |
34 | // a pointer that contains info about that page. We use two | |
35 | // representations: one for 32-bit addresses, and another for 64 bit | |
36 | // addresses. Both representations provide the same interface. The | |
37 | // first representation is implemented as a flat array, the seconds as | |
38 | // a three-level radix tree that strips away approximately 1/3rd of | |
39 | // the bits every time. | |
40 | // | |
41 | // The BITS parameter should be the number of bits required to hold | |
42 | // a page number. E.g., with 32 bit pointers and 4K pages (i.e., | |
43 | // page offset fits in lower 12 bits), BITS == 20. | |
44 | ||
45 | #ifndef TCMALLOC_PAGEMAP_H__ | |
46 | #define TCMALLOC_PAGEMAP_H__ | |
47 | ||
48 | #if HAVE(STDINT_H) | |
49 | #include <stdint.h> | |
50 | #elif HAVE(INTTYPES_H) | |
51 | #include <inttypes.h> | |
52 | #else | |
53 | #include <sys/types.h> | |
54 | #endif | |
55 | ||
56 | #include <string.h> | |
57 | ||
58 | #include "Assertions.h" | |
59 | ||
60 | // Single-level array | |
61 | template <int BITS> | |
62 | class TCMalloc_PageMap1 { | |
63 | private: | |
64 | void** array_; | |
65 | ||
66 | public: | |
67 | typedef uintptr_t Number; | |
68 | ||
69 | void init(void* (*allocator)(size_t)) { | |
70 | array_ = reinterpret_cast<void**>((*allocator)(sizeof(void*) << BITS)); | |
71 | memset(array_, 0, sizeof(void*) << BITS); | |
72 | } | |
73 | ||
74 | // Ensure that the map contains initialized entries "x .. x+n-1". | |
75 | // Returns true if successful, false if we could not allocate memory. | |
76 | bool Ensure(Number x, size_t n) { | |
77 | // Nothing to do since flat array was allocate at start | |
78 | return true; | |
79 | } | |
80 | ||
81 | void PreallocateMoreMemory() {} | |
82 | ||
83 | // REQUIRES "k" is in range "[0,2^BITS-1]". | |
84 | // REQUIRES "k" has been ensured before. | |
85 | // | |
86 | // Return the current value for KEY. Returns "Value()" if not | |
87 | // yet set. | |
88 | void* get(Number k) const { | |
89 | return array_[k]; | |
90 | } | |
91 | ||
92 | // REQUIRES "k" is in range "[0,2^BITS-1]". | |
93 | // REQUIRES "k" has been ensured before. | |
94 | // | |
95 | // Sets the value for KEY. | |
96 | void set(Number k, void* v) { | |
97 | array_[k] = v; | |
98 | } | |
99 | }; | |
100 | ||
101 | // Two-level radix tree | |
102 | template <int BITS> | |
103 | class TCMalloc_PageMap2 { | |
104 | private: | |
105 | // Put 32 entries in the root and (2^BITS)/32 entries in each leaf. | |
106 | static const int ROOT_BITS = 5; | |
107 | static const int ROOT_LENGTH = 1 << ROOT_BITS; | |
108 | ||
109 | static const int LEAF_BITS = BITS - ROOT_BITS; | |
110 | static const int LEAF_LENGTH = 1 << LEAF_BITS; | |
111 | ||
112 | // Leaf node | |
113 | struct Leaf { | |
114 | void* values[LEAF_LENGTH]; | |
115 | }; | |
116 | ||
117 | Leaf* root_[ROOT_LENGTH]; // Pointers to 32 child nodes | |
118 | void* (*allocator_)(size_t); // Memory allocator | |
119 | ||
120 | public: | |
121 | typedef uintptr_t Number; | |
122 | ||
123 | void init(void* (*allocator)(size_t)) { | |
124 | allocator_ = allocator; | |
125 | memset(root_, 0, sizeof(root_)); | |
126 | } | |
127 | ||
128 | void* get(Number k) const { | |
129 | ASSERT(k >> BITS == 0); | |
130 | const Number i1 = k >> LEAF_BITS; | |
131 | const Number i2 = k & (LEAF_LENGTH-1); | |
132 | return root_[i1]->values[i2]; | |
133 | } | |
134 | ||
135 | void set(Number k, void* v) { | |
136 | ASSERT(k >> BITS == 0); | |
137 | const Number i1 = k >> LEAF_BITS; | |
138 | const Number i2 = k & (LEAF_LENGTH-1); | |
139 | root_[i1]->values[i2] = v; | |
140 | } | |
141 | ||
142 | bool Ensure(Number start, size_t n) { | |
143 | for (Number key = start; key <= start + n - 1; ) { | |
144 | const Number i1 = key >> LEAF_BITS; | |
145 | ||
146 | // Make 2nd level node if necessary | |
147 | if (root_[i1] == NULL) { | |
148 | Leaf* leaf = reinterpret_cast<Leaf*>((*allocator_)(sizeof(Leaf))); | |
149 | if (leaf == NULL) return false; | |
150 | memset(leaf, 0, sizeof(*leaf)); | |
151 | root_[i1] = leaf; | |
152 | } | |
153 | ||
154 | // Advance key past whatever is covered by this leaf node | |
155 | key = ((key >> LEAF_BITS) + 1) << LEAF_BITS; | |
156 | } | |
157 | return true; | |
158 | } | |
159 | ||
160 | void PreallocateMoreMemory() { | |
161 | // Allocate enough to keep track of all possible pages | |
162 | Ensure(0, 1 << BITS); | |
163 | } | |
164 | ||
165 | #ifdef WTF_CHANGES | |
166 | template<class Visitor, class MemoryReader> | |
167 | void visit(const Visitor& visitor, const MemoryReader& reader) | |
168 | { | |
169 | for (int i = 0; i < ROOT_LENGTH; i++) { | |
170 | if (!root_[i]) | |
171 | continue; | |
172 | ||
173 | Leaf* l = reader(reinterpret_cast<Leaf*>(root_[i])); | |
174 | for (int j = 0; j < LEAF_LENGTH; j += visitor.visit(l->values[j])) | |
175 | ; | |
176 | } | |
177 | } | |
178 | #endif | |
179 | }; | |
180 | ||
181 | // Three-level radix tree | |
182 | template <int BITS> | |
183 | class TCMalloc_PageMap3 { | |
184 | private: | |
185 | // How many bits should we consume at each interior level | |
186 | static const int INTERIOR_BITS = (BITS + 2) / 3; // Round-up | |
187 | static const int INTERIOR_LENGTH = 1 << INTERIOR_BITS; | |
188 | ||
189 | // How many bits should we consume at leaf level | |
190 | static const int LEAF_BITS = BITS - 2*INTERIOR_BITS; | |
191 | static const int LEAF_LENGTH = 1 << LEAF_BITS; | |
192 | ||
193 | // Interior node | |
194 | struct Node { | |
195 | Node* ptrs[INTERIOR_LENGTH]; | |
196 | }; | |
197 | ||
198 | // Leaf node | |
199 | struct Leaf { | |
200 | void* values[LEAF_LENGTH]; | |
201 | }; | |
202 | ||
203 | Node* root_; // Root of radix tree | |
204 | void* (*allocator_)(size_t); // Memory allocator | |
205 | ||
206 | Node* NewNode() { | |
207 | Node* result = reinterpret_cast<Node*>((*allocator_)(sizeof(Node))); | |
208 | if (result != NULL) { | |
209 | memset(result, 0, sizeof(*result)); | |
210 | } | |
211 | return result; | |
212 | } | |
213 | ||
214 | public: | |
215 | typedef uintptr_t Number; | |
216 | ||
217 | void init(void* (*allocator)(size_t)) { | |
218 | allocator_ = allocator; | |
219 | root_ = NewNode(); | |
220 | } | |
221 | ||
222 | void* get(Number k) const { | |
223 | ASSERT(k >> BITS == 0); | |
224 | const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS); | |
225 | const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH-1); | |
226 | const Number i3 = k & (LEAF_LENGTH-1); | |
227 | return reinterpret_cast<Leaf*>(root_->ptrs[i1]->ptrs[i2])->values[i3]; | |
228 | } | |
229 | ||
230 | void set(Number k, void* v) { | |
231 | ASSERT(k >> BITS == 0); | |
232 | const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS); | |
233 | const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH-1); | |
234 | const Number i3 = k & (LEAF_LENGTH-1); | |
235 | reinterpret_cast<Leaf*>(root_->ptrs[i1]->ptrs[i2])->values[i3] = v; | |
236 | } | |
237 | ||
238 | bool Ensure(Number start, size_t n) { | |
239 | for (Number key = start; key <= start + n - 1; ) { | |
240 | const Number i1 = key >> (LEAF_BITS + INTERIOR_BITS); | |
241 | const Number i2 = (key >> LEAF_BITS) & (INTERIOR_LENGTH-1); | |
242 | ||
243 | // Make 2nd level node if necessary | |
244 | if (root_->ptrs[i1] == NULL) { | |
245 | Node* n = NewNode(); | |
246 | if (n == NULL) return false; | |
247 | root_->ptrs[i1] = n; | |
248 | } | |
249 | ||
250 | // Make leaf node if necessary | |
251 | if (root_->ptrs[i1]->ptrs[i2] == NULL) { | |
252 | Leaf* leaf = reinterpret_cast<Leaf*>((*allocator_)(sizeof(Leaf))); | |
253 | if (leaf == NULL) return false; | |
254 | memset(leaf, 0, sizeof(*leaf)); | |
255 | root_->ptrs[i1]->ptrs[i2] = reinterpret_cast<Node*>(leaf); | |
256 | } | |
257 | ||
258 | // Advance key past whatever is covered by this leaf node | |
259 | key = ((key >> LEAF_BITS) + 1) << LEAF_BITS; | |
260 | } | |
261 | return true; | |
262 | } | |
263 | ||
264 | void PreallocateMoreMemory() { | |
265 | } | |
266 | ||
267 | #ifdef WTF_CHANGES | |
268 | template<class Visitor, class MemoryReader> | |
269 | void visit(const Visitor& visitor, const MemoryReader& reader) { | |
270 | Node* root = reader(root_); | |
271 | for (int i = 0; i < INTERIOR_LENGTH; i++) { | |
272 | if (!root->ptrs[i]) | |
273 | continue; | |
274 | ||
275 | Node* n = reader(root->ptrs[i]); | |
276 | for (int j = 0; j < INTERIOR_LENGTH; j++) { | |
277 | if (!n->ptrs[j]) | |
278 | continue; | |
279 | ||
280 | Leaf* l = reader(reinterpret_cast<Leaf*>(n->ptrs[j])); | |
281 | for (int k = 0; k < LEAF_LENGTH; k += visitor.visit(l->values[k])) | |
282 | ; | |
283 | } | |
284 | } | |
285 | } | |
286 | #endif | |
287 | }; | |
288 | ||
289 | #endif // TCMALLOC_PAGEMAP_H__ |