-
Notifications
You must be signed in to change notification settings - Fork 1
/
onesizeheap.h
177 lines (138 loc) · 4.48 KB
/
onesizeheap.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// -*- C++ -*-
#ifndef _ONESIZEHEAP_H_
#define _ONESIZEHEAP_H_
#include "vamcommon.h"
namespace VAM {
// OneSizeHeap: a heap that allocates objects of a fixed size and creates new subheaps in exponentially increasing sizes
template <unsigned char MaxSubHeapType, class SubHeap, class SuperHeap>
class OneSizeHeap : public SuperHeap {
public:
OneSizeHeap() : _object_size(0), _next_subheap_type(1) {
INIT_LIST_HEAD(&_full_subheap_list);
INIT_LIST_HEAD(&_avai_subheap_list);
dbprintf("OneSizeHeap: sizeof(SubHeap)=%u\n", sizeof(SubHeap));
}
~OneSizeHeap() {
// free all subheaps
while (!list_empty(&_full_subheap_list)) {
list_head * node = _full_subheap_list.next;
SubHeap * subheap = SubHeap::listToHeap(node);
assert(subheap->getList() == node);
removeSubHeap(subheap);
}
while (!list_empty(&_avai_subheap_list)) {
list_head * node = _avai_subheap_list.next;
SubHeap * subheap = SubHeap::listToHeap(node);
assert(subheap->getList() == node);
removeSubHeap(subheap);
}
}
inline void * malloc(size_t size) {
sanityCheck();
void * ptr = NULL;
assert(_object_size == 0 || size == _object_size);
assert(size < PAGE_SIZE);
SubHeap * subheap;
// the first allocation sets the fixed object size
if (_object_size == 0)
_object_size = size;
// allocate the object in an available subheap
while (!list_empty(&_avai_subheap_list)) {
subheap = SubHeap::listToHeap(_avai_subheap_list.next);
ptr = subheap->malloc();
if (ptr != NULL)
break;
assert(subheap->getNumFree() == 0);
list_move(subheap->getList(), &_full_subheap_list);
}
// if all subheaps are full, create a new one
if (ptr == NULL) {
subheap = createSubHeap();
if (subheap != NULL) {
ptr = subheap->malloc();
assert(ptr != NULL);
}
}
assert(ptr == NULL || getSubHeap(ptr) == subheap);
sanityCheck();
return ptr;
}
inline void free(void * ptr) {
sanityCheck();
SubHeap * subheap = getSubHeap(ptr);
subheap->free(ptr);
if (subheap->getNumFree() == 1)
list_move(subheap->getList(), &_avai_subheap_list);
else if (subheap->getNumFree() == subheap->getNumTotal())
removeSubHeap(subheap);
sanityCheck();
}
inline size_t getSize(void * ptr) {
return getSubHeap(ptr)->getObjectSize();
}
void sanityCheck() {
#ifdef DEBUG
#if SANITY_CHECK
if (!list_empty(&_full_subheap_list)) {
list_head * node = _full_subheap_list.next;
while (node != &_full_subheap_list) {
SubHeap * subheap = SubHeap::listToHeap(node);
assert(reinterpret_cast<size_t>(subheap) % PAGE_SIZE == 0);
assert(subheap->getObjectSize() == _object_size);
assert(subheap->getNumFree() == 0);
assert(subheap->getList() == node);
node = node->next;
}
}
if (!list_empty(&_avai_subheap_list)) {
list_head * node = _avai_subheap_list.next;
while (node != &_avai_subheap_list) {
SubHeap * subheap = SubHeap::listToHeap(node);
assert(reinterpret_cast<size_t>(subheap) % PAGE_SIZE == 0);
assert(subheap->getObjectSize() == _object_size);
assert(subheap->getNumFree() <= subheap->getNumTotal());
assert(subheap->getList() == node);
node = node->next;
}
}
#endif
#endif
}
private:
list_head _full_subheap_list;
list_head _avai_subheap_list;
size_t _object_size;
unsigned char _next_subheap_type;
// create a new subheap with doubled size
inline SubHeap * createSubHeap() {
size_t subheap_size = PAGE_SIZE << (_next_subheap_type - 1);
SubHeap * subheap = reinterpret_cast<SubHeap *>(SuperHeap::malloc(subheap_size, _next_subheap_type));
assert(subheap != NULL);
assert(reinterpret_cast<size_t>(subheap) % subheap_size == 0);
if (subheap != NULL) {
// initialize the new subheap
subheap = new (subheap) SubHeap(subheap_size, _object_size);
list_add(subheap->getList(), &_avai_subheap_list);
if (_next_subheap_type < MaxSubHeapType)
_next_subheap_type++;
}
return subheap;
}
// remove the subheap when it's empty
inline void removeSubHeap(SubHeap * subheap) {
assert(subheap->getNumFree() == subheap->getNumTotal());
list_del(subheap->getList());
SuperHeap::free(subheap);
if (_next_subheap_type > 1)
_next_subheap_type--;
}
// find the subheap from any address inside the subheap
inline SubHeap * getSubHeap(void * ptr) {
unsigned char type = SuperHeap::ptrToType(ptr);
assert(type != 0);
SubHeap * subheap = reinterpret_cast<SubHeap *>(reinterpret_cast<size_t>(ptr) & (PAGE_MASK << (type - 1)));
return subheap;
}
}; // end of class OneSizeHeap
}; // end of namespace VAM
#endif