-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmem_pool.h
79 lines (67 loc) · 2.35 KB
/
mem_pool.h
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
#pragma once
// (c) Robert Muth - see LICENSE for more info
#include "Util/assert.h"
#include <cstdint>
#include <memory.h>
#include <iostream>
namespace cwerg {
// Memory pool for variable size entities whose size must be a multiple
// of `BYTE_GRANULARITY`.
// The result of an allocation is not a pointer but an index into the pool.
// Use `BackingStorage` to get a pointer to the actual memory.
//
// The pool currently uses realloc when it runs out of memory,
// which may invalidate the pointer returned by `BackingStorage()`
template <int BYTE_GRANULARITY>
struct MemPool {
uint8_t* data = nullptr;
uint8_t* alloc_bitmap = nullptr;
uint32_t allocated_size = 0;
uint32_t first_free = 0;
void UpdateBitmap(uint32_t start, uint32_t size, bool allocated) {
for (unsigned i = 0; i < size; ++i) {
unsigned pos = (start + i) / 8;
unsigned bit = (start + i) % 8;
if (allocated) {
// TODO: this only holds if never use `Del`
// ASSERT((alloc_bitmap[pos] & (1ull << bit)) == 0, "");
alloc_bitmap[pos] |= (1ull << bit);
} else {
ASSERT((alloc_bitmap[pos] & (1ull << bit)), "");
alloc_bitmap[pos] &= ~(1ull << bit);
}
}
}
// Allocate Storage
// Size is in multiples of granularity
uint32_t New(uint32_t size) {
if (data == nullptr || first_free + size > allocated_size) {
const bool first_alloc = (data == nullptr);
uint32_t old_size = allocated_size;
allocated_size += 1024 * 1024;
data = (uint8_t*)realloc(data, allocated_size * BYTE_GRANULARITY);
// 1 bit per element in data
alloc_bitmap = (uint8_t*)realloc(alloc_bitmap, allocated_size / 8);
memset(alloc_bitmap + old_size / 8, 0, (allocated_size - old_size) / 8);
if (first_alloc) {
// Make sure we never return a zero offset
UpdateBitmap(0, 1, true);
first_free = 1;
}
}
uint32_t start = first_free;
first_free += size;
UpdateBitmap(start, size, true);
return start;
}
// Free Storage, size must be the value or smaller that was used
// as the parameter to `New()`
void Del(uint32_t start, uint32_t size) {
UpdateBitmap(start, size, false);
}
void* BackingStorage(uint32_t start) const {
return data + start * BYTE_GRANULARITY;
}
uint32_t byte_granularity() const { return BYTE_GRANULARITY; }
};
} // namespace cwerg