-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator.h
61 lines (49 loc) · 1.32 KB
/
allocator.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
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include <string>
#include "defines.h"
/**
* A class that manages indices in a previously allocated array. Deallocated
* elements are stacked for reuse. Does not perform boundary checks.
*/
class Allocator {
string name; // a human-readable string to be used in error messages
int numElements; // number of elements to accommodate
int firstFree; // first index that has never been allocated
int* stack; // stack of deallocated indices
bool* inUse; // keeps track of allocated indices
int stackSize; // number of deallocated indices
public:
/**
* @param name A human-readable name.
* @param numElements Number of elements to manage.
*/
Allocator(string name, int numElements);
/**
* Returns the index of a free element. Terminates the program if there are no
* free slots.
*/
int alloc();
/**
* Marks the given index as free, making it available for reuse. Does not
* perform sanity checks.
*/
void free(int index);
/**
* Returns true iff index is currently allocated.
*/
bool isInUse(int index);
/**
* Returns the number of used elements.
*/
int used();
/**
* Returns the number of free elements.
*/
int available();
/**
* Resets the allocator to an empty state.
*/
void reset();
};
#endif