source: webkit/trunk/JavaScriptCore/wtf/TCPageMap.h@ 27402

Last change on this file since 27402 was 27186, checked in by [email protected], 18 years ago

2007-10-28 Mark Rowe <[email protected]>

We don't include "config.h" in headers.

  • bindings/jni/jni_instance.h:
  • kjs/regexp.h:
  • wtf/TCPageMap.h:
  • wtf/TCSpinLock.h:

2007-10-28 Mark Rowe <[email protected]>

We don't include "config.h" in headers.

  • dom/XMLTokenizer.h:
  • platform/graphics/svg/SVGResourceFilter.h:
  • platform/image-decoders/ImageDecoder.h:
  • platform/wx/FontPlatformData.h:
  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
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 <[email protected]>
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
61template <int BITS>
62class 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 // REQUIRES "k" is in range "[0,2^BITS-1]".
82 // REQUIRES "k" has been ensured before.
83 //
84 // Return the current value for KEY. Returns "Value()" if not
85 // yet set.
86 void* get(Number k) const {
87 return array_[k];
88 }
89
90 // REQUIRES "k" is in range "[0,2^BITS-1]".
91 // REQUIRES "k" has been ensured before.
92 //
93 // Sets the value for KEY.
94 void set(Number k, void* v) {
95 array_[k] = v;
96 }
97};
98
99// Two-level radix tree
100template <int BITS>
101class TCMalloc_PageMap2 {
102 private:
103 // Put 32 entries in the root and (2^BITS)/32 entries in each leaf.
104 static const int ROOT_BITS = 5;
105 static const int ROOT_LENGTH = 1 << ROOT_BITS;
106
107 static const int LEAF_BITS = BITS - ROOT_BITS;
108 static const int LEAF_LENGTH = 1 << LEAF_BITS;
109
110 // Leaf node
111 struct Leaf {
112 void* values[LEAF_LENGTH];
113 };
114
115 Leaf* root_[ROOT_LENGTH]; // Pointers to 32 child nodes
116 void* (*allocator_)(size_t); // Memory allocator
117
118 public:
119 typedef uintptr_t Number;
120
121 void init(void* (*allocator)(size_t)) {
122 allocator_ = allocator;
123 memset(root_, 0, sizeof(root_));
124 }
125
126 void* get(Number k) const {
127 ASSERT(k >> BITS == 0);
128 const Number i1 = k >> LEAF_BITS;
129 const Number i2 = k & (LEAF_LENGTH-1);
130 return root_[i1]->values[i2];
131 }
132
133 void set(Number k, void* v) {
134 ASSERT(k >> BITS == 0);
135 const Number i1 = k >> LEAF_BITS;
136 const Number i2 = k & (LEAF_LENGTH-1);
137 root_[i1]->values[i2] = v;
138 }
139
140 bool Ensure(Number start, size_t n) {
141 for (Number key = start; key <= start + n - 1; ) {
142 const Number i1 = key >> LEAF_BITS;
143
144 // Make 2nd level node if necessary
145 if (root_[i1] == NULL) {
146 Leaf* leaf = reinterpret_cast<Leaf*>((*allocator_)(sizeof(Leaf)));
147 if (leaf == NULL) return false;
148 memset(leaf, 0, sizeof(*leaf));
149 root_[i1] = leaf;
150 }
151
152 // Advance key past whatever is covered by this leaf node
153 key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
154 }
155 return true;
156 }
157
158#ifdef WTF_CHANGES
159 template<class Visitor, class MemoryReader>
160 void visit(const Visitor& visitor, const MemoryReader& reader)
161 {
162 for (int i = 0; i < ROOT_LENGTH; i++) {
163 if (!root_[i])
164 continue;
165
166 Leaf* l = reader(reinterpret_cast<Leaf*>(root_[i]));
167 for (int j = 0; j < LEAF_LENGTH; j += visitor.visit(l->values[j]))
168 ;
169 }
170 }
171#endif
172};
173
174// Three-level radix tree
175template <int BITS>
176class TCMalloc_PageMap3 {
177 private:
178 // How many bits should we consume at each interior level
179 static const int INTERIOR_BITS = (BITS + 2) / 3; // Round-up
180 static const int INTERIOR_LENGTH = 1 << INTERIOR_BITS;
181
182 // How many bits should we consume at leaf level
183 static const int LEAF_BITS = BITS - 2*INTERIOR_BITS;
184 static const int LEAF_LENGTH = 1 << LEAF_BITS;
185
186 // Interior node
187 struct Node {
188 Node* ptrs[INTERIOR_LENGTH];
189 };
190
191 // Leaf node
192 struct Leaf {
193 void* values[LEAF_LENGTH];
194 };
195
196 Node* root_; // Root of radix tree
197 void* (*allocator_)(size_t); // Memory allocator
198
199 Node* NewNode() {
200 Node* result = reinterpret_cast<Node*>((*allocator_)(sizeof(Node)));
201 if (result != NULL) {
202 memset(result, 0, sizeof(*result));
203 }
204 return result;
205 }
206
207 public:
208 typedef uintptr_t Number;
209
210 void init(void* (*allocator)(size_t)) {
211 allocator_ = allocator;
212 root_ = NewNode();
213 }
214
215 void* get(Number k) const {
216 ASSERT(k >> BITS == 0);
217 const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
218 const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH-1);
219 const Number i3 = k & (LEAF_LENGTH-1);
220 return reinterpret_cast<Leaf*>(root_->ptrs[i1]->ptrs[i2])->values[i3];
221 }
222
223 void set(Number k, void* v) {
224 ASSERT(k >> BITS == 0);
225 const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
226 const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH-1);
227 const Number i3 = k & (LEAF_LENGTH-1);
228 reinterpret_cast<Leaf*>(root_->ptrs[i1]->ptrs[i2])->values[i3] = v;
229 }
230
231 bool Ensure(Number start, size_t n) {
232 for (Number key = start; key <= start + n - 1; ) {
233 const Number i1 = key >> (LEAF_BITS + INTERIOR_BITS);
234 const Number i2 = (key >> LEAF_BITS) & (INTERIOR_LENGTH-1);
235
236 // Make 2nd level node if necessary
237 if (root_->ptrs[i1] == NULL) {
238 Node* n = NewNode();
239 if (n == NULL) return false;
240 root_->ptrs[i1] = n;
241 }
242
243 // Make leaf node if necessary
244 if (root_->ptrs[i1]->ptrs[i2] == NULL) {
245 Leaf* leaf = reinterpret_cast<Leaf*>((*allocator_)(sizeof(Leaf)));
246 if (leaf == NULL) return false;
247 memset(leaf, 0, sizeof(*leaf));
248 root_->ptrs[i1]->ptrs[i2] = reinterpret_cast<Node*>(leaf);
249 }
250
251 // Advance key past whatever is covered by this leaf node
252 key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
253 }
254 return true;
255 }
256
257#ifdef WTF_CHANGES
258 template<class Visitor, class MemoryReader>
259 void visit(const Visitor& visitor, const MemoryReader& reader) {
260 Node* root = reader(root_);
261 for (int i = 0; i < INTERIOR_LENGTH; i++) {
262 if (!root->ptrs[i])
263 continue;
264
265 Node* n = reader(root->ptrs[i]);
266 for (int j = 0; j < INTERIOR_LENGTH; j++) {
267 if (!n->ptrs[j])
268 continue;
269
270 Leaf* l = reader(reinterpret_cast<Leaf*>(n->ptrs[j]));
271 for (int k = 0; k < LEAF_LENGTH; k += visitor.visit(l->values[k]))
272 ;
273 }
274 }
275 }
276#endif
277};
278
279#endif // TCMALLOC_PAGEMAP_H__
Note: See TracBrowser for help on using the repository browser.