-
Notifications
You must be signed in to change notification settings - Fork 2
/
alloc.c
195 lines (142 loc) · 3.69 KB
/
alloc.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdbool.h>
#ifdef PRINT_STACK
#include <execinfo.h>
#endif
#ifdef PC
#include <stdio.h>
#else
#define NULL 0x0
#endif
#include "alloc.h"
void print_trace (void)
{
#ifdef PRINT_STACK
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
printf(" Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
printf(" %s\n", strings[i]);
free (strings);
#endif
}
void mem_init(mem_allocator *allocator,
uint8_t *start,
uint32_t size)
{
allocator->start = start;
allocator->size = size;
// init the next cell
mem_allocator *next = (mem_allocator*) allocator->start;
next->start = NULL;
next->size = 0;
}
static void _mem_correct_size(uint32_t *size)
{
if (*size < sizeof(mem_allocator))
*size = sizeof(mem_allocator);
}
void * mem_alloc(mem_allocator *allocator,
uint32_t size)
{
if (size == 0)
return NULL;
_mem_correct_size(&size);
mem_allocator *iterator = allocator;
while(true) {
if (iterator->size >= size) {
void *ptr = iterator->start;
mem_allocator *next = (mem_allocator*) iterator->start;
mem_allocator *newnext = (mem_allocator*) ((uint8_t*)(iterator->start) + size);
// move cell
*newnext = *next;
iterator->start = (uint8_t*) newnext;
iterator->size -= size;
#ifdef TRACE_ALLOC
printf("ALLOC %p\t%u\n", ptr, size);
print_trace();
#endif
return ptr;
} else {
// move iterator to next cell
if (iterator->start == NULL) // end of list
return NULL;
else
iterator = (mem_allocator*) iterator->start; // go to next list cell
}
}
return NULL;
}
void mem_free(mem_allocator *allocator, void *ptr, uint32_t size)
{
if (ptr == NULL || size == 0)
return;
_mem_correct_size(&size);
uint8_t *start = (uint8_t*) ptr;
mem_allocator *iterator = allocator;
mem_allocator *prev_iterator = NULL;
while(true) {
if (start < iterator->start) {
mem_allocator *cell = (mem_allocator*) start;
bool can_merge_left = prev_iterator != NULL &&
(prev_iterator->start // == (uint8*)iterator
+ prev_iterator->size) == start;
bool can_merge_right = start + size == iterator->start;
if (can_merge_left && can_merge_right) {
mem_allocator *next_cell = (mem_allocator*)(iterator->start);
uint32_t next_size = iterator->size;
*iterator = *next_cell;
prev_iterator->size += size + next_size;
} else if (can_merge_right) {
mem_allocator *next = (mem_allocator*) iterator->start;
*cell = *next;
iterator->size += size;
iterator->start = start;
} else if (can_merge_left) {
prev_iterator->size += size;
// else, create intermediate cell
} else {
*cell = *iterator;
iterator->size = size;
iterator->start = start;
}
#ifdef TRACE_ALLOC
printf("FREE %p\t%u\n", ptr, size);
print_trace();
#endif
return;
} else {
// move iterator to next cell
if (iterator->start == NULL)
return;
prev_iterator = iterator;
iterator = (mem_allocator*) iterator->start;
}
}
}
void mem_debug(mem_allocator *allocator)
{
#ifdef PC
puts("* * * * * * * * * *");
puts("* DBG allocator");
uint32_t free_mem = 0;
uint32_t free_chunks = 0;
mem_allocator *iterator = allocator;
while (iterator != NULL) {
printf("* Chunk[%d] @%p\n", free_chunks, iterator);
printf("* nxt @%p\n", iterator->start);
printf("* siz %d\n", iterator->size);
free_mem += iterator->size;
iterator = (mem_allocator*)(iterator->start);
free_chunks++;
}
free_chunks--; // balance last chunk (guard cell)
printf("* Free mem : %u\n", free_mem);
printf("* Nb chunks : %u\n", free_chunks);
puts("* * * * * * * * * *");
print_trace();
#endif
}