-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitfieldAllocator.cpp
More file actions
40 lines (35 loc) · 827 Bytes
/
BitfieldAllocator.cpp
File metadata and controls
40 lines (35 loc) · 827 Bytes
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
#include "BitfieldAllocator.h"
#include <stdio.h>
#include <inttypes.h>
BitfieldAllocator::BitfieldAllocator():
fAllocSet(0), fTotalSize(0), fAllocSize(0)
{}
void BitfieldAllocator::Register(uint64_t ptr, size_t size)
{
for (uint32_t i = 0; i < size; i++) {
fAllocSet |= 1 << (uint32_t(ptr) + i);
}
fTotalSize += size;
}
bool BitfieldAllocator::Alloc(uint64_t &ptr, size_t size)
{
if (size != 1) return false;
for (uint32_t i = 0; i < 32; i++) {
if ((1 << i) & fAllocSet) {
fAllocSet &= ~(1 << i);
fAllocSize += 1;
ptr = i;
return true;
}
}
return false;
}
void BitfieldAllocator::Free(uint64_t ptr)
{
if (ptr >= 32 || ((1 << uint32_t(ptr)) & fAllocSet)) {
printf("[!] Free(%#" PRIx64 "): pointer is not allocated\n", ptr);
return;
}
fAllocSet |= (1 << uint32_t(ptr));
fAllocSize -= 1;
}