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
196
197
198
199
200
201
202
203
204
205
206
|
#include "arch.h"
#include "kern/mem.h"
#include "kern/common.h"
#ifdef ARCH_STM32L4
/* Provide a definition for memset() when not provided for the
* microcontroller. */
void* memset(void* dest, int c, size_t n)
{
uint8_t c8 = (uint8_t) c;
uint8_t* dest8 = (uint8_t*) dest;
uint8_t* to = dest8 + n;
while(dest8 < to) {
*(dest8 ++) = c8;
}
return dest;
}
#else
void* memset(void* dest, int c, size_t n);
#endif
typedef uint16_t halloc_off_t;
// The sizes will count the number of WORDS allocated.
// Since there's a max size of 16k, only 12 bits will be
// needed for this.
typedef struct HALLOC_NODE {
union {
uint32_t header;
struct {
/* Is this memory block currently in use (hasn't been hfree'd) */
bool used:1;
/* Number of words allocated. Does not include the header. */
uint16_t size:15;
/* The location of the previous block (in WORDS from offest) */
halloc_off_t prev;
} PACKED;
};
uint8_t mem[]; /* The memory to use. */
} halloc_node_t;
halloc_node_t* halloc_start;
#define halloc_node_next(cur) \
((halloc_node_t*)(((uint8_t*)(cur)) + (((cur)->size + 1) * 4)))
#define halloc_node_prev(cur) halloc_node_at_off(cur->prev)
#define halloc_node_at_off(offset) \
((halloc_node_t*)(((uint8_t*) halloc_start) + (offset) * 4))
#define halloc_node_get_off(node) \
(((uint32_t)(((uint8_t*)(node)) - ((uint8_t*)(halloc_start)))) / 4)
#define size_for(n) \
(((n) / 4) + ((n) % 4 != 0))
void* halloc(size_t size)
{
if (!halloc_start) {
halloc_start = (halloc_node_t*) DATA_SEGMENT_STOP_ADDR;
memset(halloc_start, 0, sizeof(halloc_node_t));
halloc_start->size = (MAX_HEAP_SIZE / 4) - 1;
}
size_t realsz = size_for(size); /* Clip the size to the nearest word. */
halloc_off_t offset = 0;
while (offset < (MAX_HEAP_SIZE / 4)) {
halloc_node_t* cur = halloc_node_at_off(offset);
if (!cur->used && (cur->size >= realsz)) {
cur->used = true;
size_t orig_size = cur->size;
cur->size = realsz;
if (orig_size > realsz) {
/* This halloc node needs to split into two blocks. */
halloc_node_t* next = halloc_node_next(cur);
next->used = 0;
next->size = orig_size - realsz - sizeof(halloc_node_t) / 4;
next->prev = offset;
halloc_node_t* nextnext = halloc_node_next(next);
if (halloc_node_get_off(nextnext) < (MAX_HEAP_SIZE / 4)) {
nextnext->prev = halloc_node_get_off(next);
}
}
return (void*) cur->mem;
}
offset += (sizeof(halloc_node_t) / 4) + cur->size;
}
return NULL;
}
/* Joins this node with the previous and next nodes if they're free. */
static void coalesce(halloc_node_t* cur)
{
if (cur->used) return;
halloc_node_t* next = halloc_node_next(cur);
if (!next->used) {
cur->size += next->size + sizeof(halloc_node_t) / 4;
}
if (cur != halloc_start) {
coalesce(halloc_node_prev(cur));
}
}
void hfree(void* mem)
{
/* TODO this should be a critical section. */
if (!mem) {
/* Consistent with C, freeing a NULL pointer does nothing. */
return;
}
halloc_node_t* header = ((halloc_node_t*) mem) - 1;
if (!header->used) {
#ifdef FOR_TESTING
assert(header->used);
#endif
return; // TODO make this fail on ARM.
}
header->used = 0;
coalesce(header);
}
#ifdef FOR_TESTING
#include <stdio.h>
void* debug_halloc_get_next_ptr(void* ptr)
{
halloc_node_t* node = ptr - sizeof(halloc_node_t);
halloc_node_t* next = halloc_node_next(node);
return next->mem;
}
void* debug_halloc_get_prev_ptr(void* ptr)
{
halloc_node_t* node = ptr - sizeof(halloc_node_t);
halloc_node_t* prev = halloc_node_prev(node);
return prev->mem;
}
/* Tests that we can walk up and down the allocated blocks and that they
* are properly aligned. */
int debug_halloc_assert_consistency(char* error, size_t len)
{
halloc_node_t* cur = halloc_node_at_off(0);
size_t total_size = 0;
size_t loop_check = 0;
while(1) {
total_size += cur->size + 1;
halloc_node_t* next = halloc_node_next(cur);
if ((uint8_t*) next == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_SIZE) {
break;
} else if ((uint8_t*) next > (uint8_t*)DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE){
snprintf(
error, len, "Next node points is out of bounds. %p vs max of %p\n",
next,
(void*)(DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE));
return 1;
}
cur = next;
}
if (total_size * 4 != MAX_HEAP_SIZE) {
snprintf(
error, len, "Total recorded size is inconsistent. %lu vs %lu\n",
total_size * 4, MAX_HEAP_SIZE);
return 1;
}
while (loop_check < 10000) {
halloc_node_t* prev = halloc_node_prev(cur);
if (prev == halloc_start) {
return 0;
}
cur = prev;
++ loop_check;
}
snprintf(error, len, "Loop check failed.\n");
return 1;
}
#endif
|