-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhandlevec.cc
71 lines (58 loc) · 1.96 KB
/
handlevec.cc
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
#include "Util/handlevec.h"
#include <memory.h>
#include "Util/assert.h"
#include "Util/mem_pool.h"
namespace cwerg {
namespace {
MemPool<32> GlobalHandleVecStorage;
} // namespace
HandleVec HandleVec::New(unsigned num_entries) {
ASSERT(num_entries <= HANDLEVEC_MAX_ENTRIES,
"too many bits requested " << num_entries);
// granularity is 8 entries = 32 bytes
const unsigned raw_width = (num_entries + 7) / 8;
const uint32_t offset = GlobalHandleVecStorage.New(raw_width);
memset(GlobalHandleVecStorage.BackingStorage(offset), 0, raw_width * 32);
HandleVec out = {offset << 8 | raw_width};
return out;
}
void HandleVec::Del(HandleVec bv) {
if (bv.raw_width() == 0) return;
GlobalHandleVecStorage.Del(bv.index >> 8, bv.index & 0xff);
}
Handle* HandleVec::BackingStorage() const {
return static_cast<Handle*>(
GlobalHandleVecStorage.BackingStorage(index >> 8));
}
void HandleVec::Set(unsigned pos, Handle handle) {
ASSERT(pos < entry_width(), "");
BackingStorage()[pos] = handle;
}
Handle HandleVec::Get(unsigned pos) const {
ASSERT(pos < entry_width(), "");
return BackingStorage()[pos];
}
void HandleVec::ClearWith(Handle handle) {
Handle* data = BackingStorage();
const unsigned width = entry_width();
for (unsigned i = 0; i < width; ++i) data[i] = handle;
}
void HandleVec::CopyFrom(HandleVec other) {
uint64_t* data1 = (uint64_t*)BackingStorage();
uint64_t* data2 = (uint64_t*)other.BackingStorage();
const unsigned width = raw_width();
ASSERT(width == other.raw_width(), "");
// copy two handles at a time
for (unsigned i = 0; i < width * 4; ++i) data1[i] = data2[i];
}
bool HandleVec::Equal(HandleVec other) const {
uint64_t* data1 = (uint64_t*)BackingStorage();
uint64_t* data2 = (uint64_t*)other.BackingStorage();
const unsigned width = raw_width();
ASSERT(width == other.raw_width(), "");
for (unsigned i = 0; i < width * 4; ++i) {
if (data1[i] != data2[i]) return false;
}
return true;
}
} // namespace cwerg