-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator.cpp
60 lines (53 loc) · 1.37 KB
/
allocator.cpp
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
#include <assert.h>
#include <stdlib.h>
#include "allocator.h"
#include "logging.h"
Allocator::Allocator(string name, int numElements) {
this->name = name;
this->numElements = numElements;
stack = (int*)malloc(numElements * sizeof(int));
inUse = (bool*)malloc(numElements * sizeof(bool));
reset();
}
int Allocator::alloc() {
int result;
if (stackSize) {
result = stack[--stackSize];
} else if (firstFree < numElements) {
result = firstFree++;
} else {
die("allocator %s is out of space", name.c_str());
result = 0; // please the compiler
}
assert(!inUse[result]);
inUse[result] = true;
return result;
}
void Allocator::free(int index) {
assert(stackSize < numElements);
assert(index >= 0);
assert(index < numElements);
assert(inUse[index]);
inUse[index] = false;
// log(LOG_DEBUG, "freed %s %d", name.c_str(), index);
stack[stackSize++] = index;
}
bool Allocator::isInUse(int index) {
return inUse[index];
}
int Allocator::used() {
return firstFree - stackSize;
}
int Allocator::available() {
// There are two types of free elements: those never yet allocated (from
// firstFree to numElements), and those previously freed, sitting on the
// stack.
return numElements - firstFree + stackSize;
}
void Allocator::reset() {
for (int i = 0; i < numElements; i++) {
inUse[i] = false;
}
firstFree = 0;
stackSize = 0;
}