-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
137 lines (112 loc) · 2.92 KB
/
heap.c
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
#include "heap.h"
static size_t availableMemory = HEAP_CAPACITY;
static uintptr_t heap[HEAP_CAPACITY] = {0};
static hChunk chunks[HEAP_CAPACITY] = {0};
static size_t chunkCount = 0;
// Returns the next avaiable position of the chunks array
static unsigned int getNextChunkPosition()
{
for (unsigned int i = 0; i < chunkCount; i++) {
if (chunks[i].chunkSize == 0 && chunks[i].start == NULL && chunks[i].next == NULL) {
return i;
}
}
return (unsigned int)chunkCount;
}
/**
* Adds a chunk to our chunks list
* @param hChunk *chunk - pointer to a constant chunk
*/
static void addChunk(hChunk *chunk)
{
assert(chunk != NULL && "Cannot add a null pointer!");
if (availableMemory == HEAP_CAPACITY && chunkCount == 0) {
chunks[0] = *chunk;
}
else {
const unsigned int pos = getNextChunkPosition();
chunks[pos] = *chunk;
hChunk *current = chunks;
while (current != NULL) {
if ((current->start - heap) < (chunk->start - heap)) {
if (current->next == NULL) {
current->next = chunks + chunkCount;
break;
}
else if ((current->next->start - heap) > (chunk->start - heap)) {
chunks[pos].next = current->next;
current->next = chunks + pos;
break;
}
}
current = current->next;
}
}
chunkCount++;
}
/**
* Finds the start of the next memory point
* that is big enough to contain the given size
* @param size_t size - size to allocate
* @return uintptr_t* - pointer to the heap memory address
*/
static uintptr_t *getStart(size_t size)
{
if (availableMemory == HEAP_CAPACITY && chunkCount == 0)
return heap;
hChunk *current = chunks;
while (current != NULL) {
if (current->next != NULL) {
// Space gap between chunks in the list
const size_t gap = (size_t)(current->next->start - current->start);
if ((gap - current->chunkSize) > size)
return current->start + size;
else
current = current->next;
}
else
return current->start + size;
}
LOG_ERROR_AND_EXIT("Not enough avaiable space in the heap!");
}
void *hAlloc(size_t size)
{
assert((size < availableMemory) &&
"Entered size exceeds the available memory limit!");
uintptr_t *start;
if (size < MIN_ALLOC_SIZE || (start = getStart(size)) == NULL)
return NULL;
hChunk chunk = {
.chunkSize = (int)size,
.start = start,
.next = NULL
};
addChunk(&chunk);
availableMemory -= size;
return chunk.start;
}
void hFree(void *ptr)
{
hChunk *current = chunks, *previous = chunks;
while (current != NULL && current->start != ptr) {
previous = current;
current = current->next;
}
if (current == NULL) {
LOG_ERROR_AND_EXIT("Value not found in chunks list");
}
for (size_t i = 0; i < current->chunkSize; i++) {
*(current->start + i) = 0;
}
previous->next = current->next;
for (int i = 0; i < chunkCount; i++) {
if (chunks[i].start == ptr) {
chunks[i].chunkSize = 0;
chunks[i].start = NULL;
chunks[i].next = NULL;
chunkCount--;
break;
}
}
availableMemory += current->chunkSize;
}