-
Notifications
You must be signed in to change notification settings - Fork 115
/
heap.h
352 lines (322 loc) · 9.92 KB
/
heap.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Source for Sk3wlDbg IdaPro plugin
Copyright (c) 2016 Chris Eagle
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __TEMPLATE_HEAP_H
#define __TEMPLATE_HEAP_H
#include <stdint.h>
#define PREV_IN_USE 1
template <class T>
class heap {
protected:
struct _chunk {
T prev_size;
T size;
T fd; //heap_chunk32*
T bk; //heap_chunk32*
};
struct _list {
T fd; //heap_chunk32*
T bk; //heap_chunk32*
};
void *user_mem;
T heap_base;
T heap_end;
T tail;
void *to_user(T addr);
inline T CHUNK_ADDR(T a) {return a - 2 * sizeof(T);};
inline _chunk* CHUNK_PTR(T c) {return (_chunk*)to_user(c);};
inline T CHUNK_SIZE(_chunk *c) {return c->size & ~1;};
T marker;
_list free_list;
void unlink(T chunk);
void link(T chunk);
T best_fit(T size);
public:
heap(void *heap_mem, T _heap_base, uint32_t _heap_size);
~heap();
T malloc(T size);
T calloc(T nmemb, T size);
T realloc(T ptr, T sz);
void free(T ptr);
};
template <class T>
void *heap<T>::to_user(T addr) {
return (addr - heap_base) + (char*)user_mem;
}
template <class T>
heap<T>::heap(void *heap_mem, T _heap_base, uint32_t _heap_size) {
user_mem = heap_mem;
heap_base = _heap_base;
heap_end = heap_base + _heap_size;
tail = heap_base;
marker = ~(2 * sizeof(T) - 1) & (T)(uint64_t)&free_list;
free_list.fd = marker;
free_list.bk = marker;
//setup initial chunk
_chunk *chunk = (_chunk *)user_mem;
chunk->prev_size = 0;
chunk->size = _heap_size | PREV_IN_USE;
}
template <class T>
heap<T>::~heap() {
}
template <class T>
void heap<T>::unlink(T chunk) {
_chunk *mchunk = CHUNK_PTR(chunk);
if (mchunk->fd == marker) {
free_list.bk = mchunk->bk;
}
else {
_chunk *next = CHUNK_PTR(mchunk->fd);
next->bk = mchunk->bk;
}
if (mchunk->bk == marker) {
free_list.fd = mchunk->fd;
}
else {
_chunk *prev = CHUNK_PTR(mchunk->bk);
prev->fd = mchunk->fd;
}
}
template <class T>
void heap<T>::link(T chunk) {
_chunk *mchunk = CHUNK_PTR(chunk);
mchunk->fd = free_list.fd;
mchunk->bk = marker;
free_list.fd = chunk;
if (mchunk->fd != marker) {
_chunk *nchunk = CHUNK_PTR(mchunk->fd);
nchunk->bk = chunk;
}
else {
free_list.bk = chunk;
}
}
template <class T>
T heap<T>::best_fit(T size) {
T best = 0;
_chunk *best_chunk = NULL;
for (T c = free_list.fd; c != marker;) {
_chunk *chunk = CHUNK_PTR(c);
if (chunk->size == size) {
best = c;
best_chunk = chunk;
break;
}
if (chunk->size > size) {
if (best == 0) {
best = c;
best_chunk = chunk;
}
else if ((chunk->size - size) < (best_chunk->size - size)) {
best = c;
best_chunk = chunk;
}
}
}
if (best) {
unlink(best);
_chunk *nchunk = CHUNK_PTR(best + CHUNK_SIZE(best_chunk));
if (best_chunk->size >= (size + sizeof(_chunk))) {
T split = best + size;
_chunk *new_chunk = CHUNK_PTR(split);
new_chunk->size = best_chunk->size - size; //this will have PREV_IN_USE set already
best_chunk->size = size | PREV_IN_USE;
nchunk->prev_size = CHUNK_SIZE(new_chunk);
link(split);
}
else {
//not enough room for a separate chunk, just give back the entire chunk
//in which case there is no need to adjust the next chunk much
nchunk->size |= PREV_IN_USE;
}
}
else {
//take it off the tail chunk
_chunk *chunk = CHUNK_PTR(tail);
if (size <= (chunk->size + 2 * sizeof(T))) {
_chunk *tchunk = CHUNK_PTR(tail + size);
best = tail;
tchunk->size = (chunk->size - size) | PREV_IN_USE; //chunk before tail is always in use
chunk->size = size | PREV_IN_USE; //chunk before tail is always in use
tail = tail + size;
}
else {
//ideally we could extend heap at this point
}
}
return best;
}
template <class T>
T heap<T>::malloc(T size) {
size += 3 * sizeof(T) - 1;
size &= ~(2 * sizeof(T) - 1);
if (size < (4 * sizeof(T))) {
size = 4 * sizeof(T);
}
T chunk = best_fit(size);
if (chunk) {
chunk += 2 * sizeof(T);
}
return chunk;
}
template <class T>
T heap<T>::calloc(T nmemb, T size) {
T sz = nmemb * size;
T block = malloc(sz);
if (block) {
void *p = to_user(block);
memset(p, 0, sz);
}
return block;
}
template <class T>
T heap<T>::realloc(T ptr, T sz) {
if (ptr == 0) {
return malloc(sz);
}
if (sz == 0) {
free(ptr);
return 0;
}
T need = (sz + (3 * sizeof(T) - 1)) & ~(2 * sizeof(T) - 1);
if (need < (4 * sizeof(T))) {
need = 4 * sizeof(T);
}
T chunk = CHUNK_ADDR(ptr);
_chunk *mchunk = CHUNK_PTR(chunk);
T next_chunk = chunk + CHUNK_SIZE(mchunk);
_chunk *nchunk = CHUNK_PTR(next_chunk);
if (next_chunk == tail) {
//adjacent to tail, it either fits or it doesn't
T max = CHUNK_SIZE(mchunk) + CHUNK_SIZE(nchunk) - 2 * sizeof(T);
if (sz < max) {
//it fits, this will accomodate growing or shrinking realloc
T new_chunk = chunk + need;
_chunk *newchunk = CHUNK_PTR(new_chunk);
newchunk->size = CHUNK_SIZE(mchunk) + CHUNK_SIZE(nchunk) - need;
newchunk->size |= PREV_IN_USE;
tail = new_chunk;
mchunk->size = need | (mchunk->size & PREV_IN_USE);
return ptr;
}
}
else {
T next_next = next_chunk + CHUNK_SIZE(nchunk);
_chunk *nnchunk = CHUNK_PTR(next_next);
bool next_in_use = (nnchunk->size & 1) == 0;
if (need <= mchunk->size) {
//smaller or same
if (need <= (mchunk->size - sizeof(_chunk))) {
//enough room to split
T new_chunk = chunk + need;
_chunk *newchunk = CHUNK_PTR(new_chunk);
if (!next_in_use) {
T nsize = CHUNK_SIZE(mchunk) - need + CHUNK_SIZE(nchunk);
newchunk->size = nsize | PREV_IN_USE;
unlink(next_chunk);
nnchunk->prev_size = nsize;
}
else {
newchunk->size = (CHUNK_SIZE(mchunk) - need) | PREV_IN_USE;
nchunk->size &= ~1;
}
link(new_chunk);
mchunk->size = need | (mchunk->size & PREV_IN_USE);
}
else {
//don't change a thing
}
return ptr;
}
if (!next_in_use) {
//maybe we can grow into the next chunk
if (need <= (CHUNK_SIZE(mchunk) + CHUNK_SIZE(nchunk))) {
T tsize = CHUNK_SIZE(nchunk);
unlink(next_chunk); //we are going to use at least some of this
if (need <= ((CHUNK_SIZE(mchunk) + tsize) - sizeof(_chunk))) {
//enough room to split
T new_chunk = chunk + need;
_chunk *newchunk = CHUNK_PTR(new_chunk);
T nsize = CHUNK_SIZE(mchunk) + tsize - need;
newchunk->size = nsize | PREV_IN_USE;
nnchunk->prev_size = nsize;
link(new_chunk);
}
else {
//used all of next chunk
nnchunk->size |= PREV_IN_USE;
}
mchunk->size = need | (mchunk->size & PREV_IN_USE);
return ptr;
}
}
}
//need to do a malloc and copy at this point
T new_block = malloc(sz);
if (new_block) {
T ncopy = CHUNK_SIZE(mchunk) - 2 * sizeof(T) + sizeof(T); //take next->prev_size field too
void *dest = to_user(new_block);
memcpy(dest, &mchunk->fd, ncopy);
free(ptr);
}
return new_block;
}
template <class T>
void heap<T>::free(T ptr) {
T chunk = CHUNK_ADDR(ptr);
_chunk *mchunk = CHUNK_PTR(chunk);
T next_chunk = chunk + CHUNK_SIZE(mchunk);
_chunk *nchunk = CHUNK_PTR(next_chunk);
if (next_chunk == tail) {
mchunk->size += CHUNK_SIZE(nchunk); //add this into tail
tail = chunk;
}
else {
T next_next = next_chunk + CHUNK_SIZE(nchunk);
_chunk *nnchunk = CHUNK_PTR(next_next);
if ((nnchunk->size & PREV_IN_USE) == 0) {
//next chunk is not in use, so consolidate forward
mchunk->size += CHUNK_SIZE(nchunk); //grow size to include next
unlink(next_chunk); //unlink next chunk
nnchunk->prev_size = CHUNK_SIZE(mchunk); //update prev_size in new next
}
else {
nchunk->prev_size = CHUNK_SIZE(mchunk); //this chunk not in use so set prev_size
nchunk->size &= ~1; //this chunk is not in use anymore
}
link(chunk);
}
if ((mchunk->size & PREV_IN_USE) == 0) {
//prev is not in use, need to consolidate backwards
T prev_chunk = chunk - mchunk->prev_size;
unlink(prev_chunk);
_chunk *pchunk = CHUNK_PTR(prev_chunk);
pchunk->size += mchunk->size;
if (chunk == tail) {
tail = prev_chunk;
}
else {
//chunk was linked during forward consolidation check
unlink(chunk);
T next_chunk = chunk + mchunk->size;
//next chunk already knows that this chunk is not in use from forward consolidation
_chunk *nchunk = CHUNK_PTR(next_chunk);
nchunk->prev_size = CHUNK_SIZE(pchunk);
link(prev_chunk);
}
}
}
#endif