1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include "stdafx.h"
#include "CodeTable.h"
#include "Gc.h"
#include <algorithm>
namespace storm {
/**
* The allocator.
*/
CodeTable::Allocator::Allocator() : first(null) {}
CodeTable::Allocator::~Allocator() {
for (size_t i = 0; i < chunks.size(); i++)
delete []chunks[i];
}
CodeTable::Elem *CodeTable::Allocator::alloc() {
if (!first)
allocChunk();
Elem *a = first;
first = (Elem *)first->code;
return a;
}
void CodeTable::Allocator::free(Elem *e) {
memset(e, 0, sizeof(Elem));
e->code = first;
first = e;
}
void CodeTable::Allocator::alive(vector<Elem *> &out) {
out.clear();
for (size_t chunk = 0; chunk < chunks.size(); chunk++) {
Elem *c = chunks[chunk];
for (size_t i = 0; i < chunkSize; i++) {
if (c[i].owner)
out.push_back(&c[i]);
}
}
}
void CodeTable::Allocator::allocChunk() {
Elem *chunk = new Elem[chunkSize];
memset(chunk, 0, sizeof(Elem)*chunkSize);
// Build the free-list.
for (size_t i = 1; i < chunkSize; i++)
chunk[i - 1].code = &chunk[i];
chunk[chunkSize - 1].code = first;
first = chunk;
chunks.push_back(chunk);
}
/**
* The actual table.
*/
CodeTable::CodeTable() : sorted(0) {}
CodeTable::~CodeTable() {}
CodeTable::Handle CodeTable::add(void *code) {
util::Lock::L z(lock);
Elem *e = mem.alloc();
e->code = code;
e->owner = this;
table.push_back(e);
atomicWrite(sorted, 0);
return e;
}
void CodeTable::update(Handle handle, void *code) {
Elem *e = (Elem *)handle;
e->code = code;
atomicWrite(e->owner->sorted, 0);
}
void CodeTable::remove(Handle handle) {
util::Lock::L z(lock);
Elem *e = (Elem *)handle;
mem.free(e);
table.clear();
atomicWrite(sorted, 0);
}
void *CodeTable::find(const void *addr) {
util::Lock::L z(lock);
Elem find = { (void *)addr, this };
Iter begin = table.begin();
Iter end = table.end();
if (atomicRead(sorted)) {
// Try to find using binary search.
Iter found = std::lower_bound(begin, end, &find, CodeCompare());
if (found != end && contains(*found, addr))
return (*found)->code;
if (found != begin && contains(*--found, addr))
return (*found)->code;
// If we did not find it. See if the array is still sorted.
if (atomicRead(sorted))
return null;
}
// The array is not sorted. Sort it while finding 'addr'.
mem.alive(table);
begin = table.begin();
end = table.end();
atomicWrite(sorted, 1);
std::make_heap(begin, end, CodeCompare());
void *result = null;
while (begin != end) {
std::pop_heap(begin, end--, CodeCompare());
if (contains(*end, addr))
result = (*end)->code;
}
return result;
}
bool CodeTable::contains(Elem *elem, const void *ptr) {
size_t start = size_t(elem->code);
size_t end = start + Gc::codeSize(elem->code);
size_t test = size_t(ptr);
return start <= test && test < end;
}
CodeTable &codeTable() {
static CodeTable t;
return t;
}
}
|