#ifndef TCMALLOC_PAGEMAP_H__
#define TCMALLOC_PAGEMAP_H__
#include <stdint.h>
#include <string.h>
#include <wtf/Assertions.h>
template <int BITS>
class TCMalloc_PageMap1 {
private:
void** array_;
public:
typedef uintptr_t Number;
void init(void* (*allocator)(size_t)) {
array_ = reinterpret_cast<void**>((*allocator)(sizeof(void*) << BITS));
memset(array_, 0, sizeof(void*) << BITS);
}
bool Ensure(Number, size_t) {
return true;
}
void PreallocateMoreMemory() {}
void* get(Number k) const {
return array_[k];
}
void set(Number k, void* v) {
array_[k] = v;
}
};
template <int BITS>
class TCMalloc_PageMap2 {
private:
static const int ROOT_BITS = 5;
static const int ROOT_LENGTH = 1 << ROOT_BITS;
static const int LEAF_BITS = BITS - ROOT_BITS;
static const int LEAF_LENGTH = 1 << LEAF_BITS;
struct Leaf {
void* values[LEAF_LENGTH];
};
Leaf* root_[ROOT_LENGTH]; void* (*allocator_)(size_t);
public:
typedef uintptr_t Number;
void init(void* (*allocator)(size_t)) {
allocator_ = allocator;
memset(root_, 0, sizeof(root_));
}
void* get(Number k) const {
ASSERT(k >> BITS == 0);
const Number i1 = k >> LEAF_BITS;
const Number i2 = k & (LEAF_LENGTH-1);
return root_[i1]->values[i2];
}
void set(Number k, void* v) {
ASSERT(k >> BITS == 0);
const Number i1 = k >> LEAF_BITS;
const Number i2 = k & (LEAF_LENGTH-1);
root_[i1]->values[i2] = v;
}
bool Ensure(Number start, size_t n) {
for (Number key = start; key <= start + n - 1; ) {
const Number i1 = key >> LEAF_BITS;
if (root_[i1] == NULL) {
Leaf* leaf = reinterpret_cast<Leaf*>((*allocator_)(sizeof(Leaf)));
if (leaf == NULL) return false;
memset(leaf, 0, sizeof(*leaf));
root_[i1] = leaf;
}
key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
}
return true;
}
void PreallocateMoreMemory() {
Ensure(0, 1 << BITS);
}
#ifdef WTF_CHANGES
template<class Visitor, class MemoryReader>
void visitValues(Visitor& visitor, const MemoryReader& reader)
{
const Number leafIndexMask = LEAF_LENGTH - 1;
const Number maxKey = (1l << BITS) - 1;
const Number invalidIndex = maxKey;
Number previousRootIndex = invalidIndex;
Leaf* leaf = 0;
for (Number key = 0; key < maxKey; ) {
const Number rootIndex = key >> LEAF_BITS;
const Number leafIndex = key & leafIndexMask;
if (rootIndex != previousRootIndex) {
if (!root_[rootIndex]) {
key += 1 << LEAF_BITS;
key &= ~leafIndexMask;
continue;
}
leaf = reader(root_[rootIndex]);
previousRootIndex = rootIndex;
}
key += visitor.visit(leaf->values[leafIndex]);
}
}
template<class Visitor, class MemoryReader>
void visitAllocations(Visitor& visitor, const MemoryReader&) {
for (int i = 0; i < ROOT_LENGTH; i++) {
if (root_[i])
visitor.visit(root_[i], sizeof(Leaf));
}
}
#endif
};
template <int BITS>
class TCMalloc_PageMap3 {
private:
static const int INTERIOR_BITS = (BITS + 2) / 3; static const int INTERIOR_LENGTH = 1 << INTERIOR_BITS;
static const int LEAF_BITS = BITS - 2*INTERIOR_BITS;
static const int LEAF_LENGTH = 1 << LEAF_BITS;
struct Node {
Node* ptrs[INTERIOR_LENGTH];
};
struct Leaf {
void* values[LEAF_LENGTH];
};
Node* root_; void* (*allocator_)(size_t);
Node* NewNode() {
Node* result = reinterpret_cast<Node*>((*allocator_)(sizeof(Node)));
if (result != NULL) {
memset(result, 0, sizeof(*result));
}
return result;
}
public:
typedef uintptr_t Number;
void init(void* (*allocator)(size_t)) {
allocator_ = allocator;
root_ = NewNode();
}
void* get(Number k) const {
ASSERT(k >> BITS == 0);
const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH-1);
const Number i3 = k & (LEAF_LENGTH-1);
return reinterpret_cast<Leaf*>(root_->ptrs[i1]->ptrs[i2])->values[i3];
}
void set(Number k, void* v) {
ASSERT(k >> BITS == 0);
const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH-1);
const Number i3 = k & (LEAF_LENGTH-1);
reinterpret_cast<Leaf*>(root_->ptrs[i1]->ptrs[i2])->values[i3] = v;
}
bool Ensure(Number start, size_t n) {
for (Number key = start; key <= start + n - 1; ) {
const Number i1 = key >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (key >> LEAF_BITS) & (INTERIOR_LENGTH-1);
if (root_->ptrs[i1] == NULL) {
Node* n = NewNode();
if (n == NULL) return false;
root_->ptrs[i1] = n;
}
if (root_->ptrs[i1]->ptrs[i2] == NULL) {
Leaf* leaf = reinterpret_cast<Leaf*>((*allocator_)(sizeof(Leaf)));
if (leaf == NULL) return false;
memset(leaf, 0, sizeof(*leaf));
root_->ptrs[i1]->ptrs[i2] = reinterpret_cast<Node*>(leaf);
}
key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
}
return true;
}
void PreallocateMoreMemory() {
}
#ifdef WTF_CHANGES
template<class Visitor, class MemoryReader>
void visitValues(Visitor& visitor, const MemoryReader& reader) {
const Number intermediateIndexMask = (INTERIOR_LENGTH - 1) << LEAF_BITS;
const Number leafIndexMask = LEAF_LENGTH - 1;
const Number maxKey = (1l << BITS) - 1;
const Number invalidIndex = maxKey;
Number previousRootIndex = invalidIndex;
Number previousIntermediateIndex = invalidIndex;
Node* intermediateNode = 0;
Leaf* leaf = 0;
Node* root = reader(root_);
for (Number key = 0; key < maxKey; ) {
const Number rootIndex = key >> (LEAF_BITS + INTERIOR_BITS);
const Number intermediateIndex = (key & intermediateIndexMask) >> LEAF_BITS;
const Number leafIndex = key & leafIndexMask;
if (rootIndex != previousRootIndex) {
if (!root->ptrs[rootIndex]) {
key += 1 << (LEAF_BITS + INTERIOR_BITS);
key &= ~(leafIndexMask | intermediateIndexMask);
continue;
}
intermediateNode = reader(root->ptrs[rootIndex]);
previousRootIndex = rootIndex;
previousIntermediateIndex = invalidIndex;
}
if (intermediateIndex != previousIntermediateIndex) {
if (!intermediateNode->ptrs[intermediateIndex]) {
key += 1 << LEAF_BITS;
key &= ~leafIndexMask;
continue;
}
leaf = reader(reinterpret_cast<Leaf*>(intermediateNode->ptrs[intermediateIndex]));
previousIntermediateIndex = intermediateIndex;
}
key += visitor.visit(leaf->values[leafIndex]);
}
}
template<class Visitor, class MemoryReader>
void visitAllocations(Visitor& visitor, const MemoryReader& reader) {
visitor.visit(root_, sizeof(Node));
Node* root = reader(root_);
for (int i = 0; i < INTERIOR_LENGTH; i++) {
if (!root->ptrs[i])
continue;
visitor.visit(root->ptrs[i], sizeof(Node));
Node* n = reader(root->ptrs[i]);
for (int j = 0; j < INTERIOR_LENGTH; j++) {
if (!n->ptrs[j])
continue;
visitor.visit(n->ptrs[j], sizeof(Leaf));
}
}
}
#endif
};
#endif // TCMALLOC_PAGEMAP_H__