-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.h
148 lines (109 loc) · 3.59 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
#ifndef HEAP_H
#define HEAP_H
#include <stdint.h>
struct memory_manager_t
{
void *memory_start;
size_t memory_size;
struct memory_chunk_t *first_memory_chunk;
};
struct memory_chunk_t
{
struct memory_chunk_t* prev;
struct memory_chunk_t* next;
size_t size;
int free;
};
struct memory_manager_t memory_manager;
void memory_init(void *address, size_t size)
{
memory_manager.memory_start = (uint8_t*)address;
memory_manager.memory_size = size;
memory_manager.first_memory_chunk = NULL;
}
void *memory_malloc(size_t size)
{
if(size == 0 || size > memory_manager.memory_size - sizeof(struct memory_chunk_t)) return NULL;
struct memory_chunk_t* cur = memory_manager.first_memory_chunk;
if(memory_manager.first_memory_chunk == NULL)
{
cur = (void*)(memory_manager.memory_start);
memory_manager.first_memory_chunk = cur;
cur->prev = NULL;
cur->next = NULL;
cur->free = 0;
cur->size = size;
return (void*)((uint8_t*)cur + sizeof(struct memory_chunk_t));
}
size_t size_in = 0;
while(cur->next != NULL)
{
if(cur->size >= size + sizeof(struct memory_chunk_t) && cur->free == 1) break;
size_in += cur->size + sizeof(struct memory_chunk_t);
cur = cur->next;
}
if(cur->size >= size + sizeof(struct memory_chunk_t) && cur->free == 1)
{
cur->free = 0;
cur->size = size;
return (void*)((uint8_t*)cur + sizeof(struct memory_chunk_t));
}
size_in += cur->size + sizeof(struct memory_chunk_t) + size;
if(size_in > memory_manager.memory_size - sizeof(struct memory_chunk_t)) return NULL;
if(cur->next == NULL)
{
struct memory_chunk_t* next = (void*)((uint8_t*)cur + sizeof(struct memory_chunk_t) + cur->size);
next->prev = cur;
next->next = NULL;
next->free = 0;
next->size = size;
cur->next = next;
cur = next;
return (void*)((uint8_t*)cur + sizeof(struct memory_chunk_t));
}
return NULL;
}
void memory_free(void *address)
{
if(address != NULL)
{
struct memory_chunk_t* cur = (struct memory_chunk_t*)((uint8_t*)address - sizeof(struct memory_chunk_t));
if(cur->size == 0 || (cur->free != 1 && cur->free != 0)) return;
int found = 0;
struct memory_chunk_t* find = memory_manager.first_memory_chunk;
while(find != NULL)
{
if(cur == find)
{
found = 1;
break;
}
find = find->next;
}
if(found == 0) return;
if(cur->next != NULL)
{
size_t new_size = ((uint8_t*)cur->next) - ((uint8_t*)cur + sizeof(struct memory_chunk_t));
cur->size = new_size;
}
cur->free = 1;
if(cur->prev != NULL && cur->prev->free == 1)
{
struct memory_chunk_t* prev = cur->prev;
prev->next = cur->next;
if(cur->next != NULL) cur->next->prev = prev;
prev->size += cur->size + sizeof(struct memory_chunk_t);
cur = prev;
}
if(cur->next != NULL && cur->next->free == 1)
{
struct memory_chunk_t* next = cur->next;
cur->next = next->next;
if(next->next != NULL) next->next->prev = cur;
cur->size += next->size + sizeof(struct memory_chunk_t);
}
if(cur->next == NULL && cur->prev != NULL) cur->prev->next = NULL;
if(cur->next == NULL && memory_manager.first_memory_chunk == cur) memory_manager.first_memory_chunk = NULL;
}
}
#endif //HEAP_H