-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdalloc.c
364 lines (325 loc) · 13.1 KB
/
dalloc.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright 2021 Alexey Vasilenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dalloc.h"
#define LOG_MODULE "dalloc"
#include "log.h"
#if ALLOC_INFO_U16
#define _OFFSET 15
#else
#define _OFFSET 31
#endif
#define FREEFLAG_GET(arr) ((arr.alloc_info) >> _OFFSET & 0x01)
#define FREEFLAG_SET(arr) ((arr.alloc_info) |= (1 << _OFFSET))
#define FREEFLAG_CLR(arr) ((arr.alloc_info) &= ~(1 << _OFFSET))
#define ALLOCSIZE_GET(arr) ((arr.alloc_info) & ~(1 << _OFFSET))
#define ALLOCSIZE_SET(arr, size) \
(arr.alloc_info) = ((size) | ((arr.alloc_info) & (1 << _OFFSET)))
#if USE_SINGLE_HEAP_MEMORY
/* define single_heap array somewhere in your code, like on the example below:
uint8_t single_heap[SINGLE_HEAP_SIZE] = {0};
*/
dl_heap_t default_heap;
#if !HEAP_LOCATION
uint8_t single_heap[SINGLE_HEAP_SIZE] = {0};
#else
#define HEAP_SET_ADDR(addr) __attribute__((section(".ARM.__at_" #addr)))
#define _HEAP_SET_ADDR(addr) HEAP_SET_ADDR(addr)
uint8_t single_heap[SINGLE_HEAP_SIZE] _HEAP_SET_ADDR(HEAP_LOCATION) = {0};
#endif
bool memory_init_flag = false;
#endif
void heap_init(dl_heap_t* heap_struct_ptr, void* mem_ptr,
uint32_t mem_size) { // Init here mem structures
heap_struct_ptr->offset = 0;
heap_struct_ptr->mem = (uint8_t*)mem_ptr;
heap_struct_ptr->total_size = mem_size;
heap_struct_ptr->alloc_info.allocations_num = 0;
heap_struct_ptr->alloc_info.max_memory_amount = 0;
for (uint32_t i = 0; i < MAX_NUM_OF_ALLOCATIONS; i++) {
heap_struct_ptr->alloc_info.ptr_info_arr[i].ptr = NULL;
heap_struct_ptr->alloc_info.ptr_info_arr[i].alloc_info = 0;
FREEFLAG_SET(heap_struct_ptr->alloc_info.ptr_info_arr[i]);
}
for (uint32_t i = 0; i < heap_struct_ptr->total_size; i++) {
heap_struct_ptr->mem[i] = 0;
}
}
void dalloc(dl_heap_t* heap_struct_ptr, uint32_t size, void** ptr) {
#if USE_SINGLE_HEAP_MEMORY
if (memory_init_flag == false) {
heap_init(&default_heap, single_heap, SINGLE_HEAP_SIZE);
memory_init_flag = true;
}
#endif
if (!heap_struct_ptr || !size) {
*ptr = NULL;
}
uint32_t new_offset = heap_struct_ptr->offset + size;
/* Correct offset if use alignment */
#if USE_ALIGNMENT
while (new_offset % ALLOCATION_ALIGNMENT_BYTES != 0) {
new_offset += 1;
}
#endif
/* Check if there is enough memory for new allocation, and if number of
* allocations is exceeded */
if ((new_offset <= heap_struct_ptr->total_size) &&
(heap_struct_ptr->alloc_info.allocations_num <
MAX_NUM_OF_ALLOCATIONS)) {
*ptr = heap_struct_ptr->mem + heap_struct_ptr->offset;
heap_struct_ptr->offset = new_offset;
/* Save info about allocated memory */
heap_struct_ptr->alloc_info
.ptr_info_arr[heap_struct_ptr->alloc_info.allocations_num]
.ptr = (uint8_t**)ptr;
ALLOCSIZE_SET(
heap_struct_ptr->alloc_info
.ptr_info_arr[heap_struct_ptr->alloc_info.allocations_num],
size);
FREEFLAG_CLR(
heap_struct_ptr->alloc_info
.ptr_info_arr[heap_struct_ptr->alloc_info.allocations_num]);
heap_struct_ptr->alloc_info.allocations_num =
heap_struct_ptr->alloc_info.allocations_num + 1;
if (heap_struct_ptr->offset >
heap_struct_ptr->alloc_info.max_memory_amount) {
heap_struct_ptr->alloc_info.max_memory_amount =
heap_struct_ptr->offset;
}
if (heap_struct_ptr->alloc_info.allocations_num >
heap_struct_ptr->alloc_info.max_allocations_amount) {
heap_struct_ptr->alloc_info.max_allocations_amount =
heap_struct_ptr->alloc_info.allocations_num;
}
} else {
LOG_ERROR("dalloc: Allocation failed");
print_dalloc_info(heap_struct_ptr);
*ptr = NULL;
if (new_offset > heap_struct_ptr->total_size) {
LOG_ERROR("dalloc: Heap size exceeded");
}
if (heap_struct_ptr->alloc_info.allocations_num >
MAX_NUM_OF_ALLOCATIONS) {
LOG_ERROR(
"dalloc: Max number of allocations exceeded: %lu",
(long unsigned int)heap_struct_ptr->alloc_info.allocations_num);
}
}
}
bool validate_ptr(dl_heap_t* heap_struct_ptr, void** ptr,
validate_ptr_condition_t condition, uint32_t* ptr_index) {
for (uint32_t i = 0; i < heap_struct_ptr->alloc_info.allocations_num; i++) {
if (condition == USING_PTR_ADDRESS) {
if (heap_struct_ptr->alloc_info.ptr_info_arr[i].ptr ==
(uint8_t**)ptr) {
if (ptr_index != NULL) {
*ptr_index = i;
}
return true;
}
} else {
if (*(heap_struct_ptr->alloc_info.ptr_info_arr[i].ptr) == *ptr) {
if (ptr_index != NULL) {
*ptr_index = i;
}
return true;
}
}
}
return false;
}
bool is_ptr_address_in_heap_area(dl_heap_t* heap_struct_ptr, void** ptr) {
size_t heap_start_area = (size_t)(heap_struct_ptr->mem);
size_t heap_stop_area =
(size_t)(heap_struct_ptr->mem) + heap_struct_ptr->total_size;
if (((size_t)ptr >= heap_start_area) && ((size_t)ptr <= heap_stop_area)) {
return true;
}
return false;
}
void defrag_memory(dl_heap_t* heap_struct_ptr) {
for (uint32_t i = 0; i < heap_struct_ptr->alloc_info.allocations_num; i++) {
if (FREEFLAG_GET(heap_struct_ptr->alloc_info.ptr_info_arr[i])) {
/* Optimize memory */
uint8_t* start_mem_ptr =
*(heap_struct_ptr->alloc_info.ptr_info_arr[i].ptr);
uint32_t start_ind =
(uint32_t)(start_mem_ptr - heap_struct_ptr->mem);
/* Set given ptr to NULL */
*(heap_struct_ptr->alloc_info.ptr_info_arr[i].ptr) = NULL;
uint32_t alloc_size =
ALLOCSIZE_GET(heap_struct_ptr->alloc_info.ptr_info_arr[i]);
#if USE_ALIGNMENT
while (alloc_size % ALLOCATION_ALIGNMENT_BYTES != 0) {
alloc_size += 1;
}
#endif
/* Check if ptrs adresses of defragmentated memory are in heap region */
for (uint32_t k = i + 1;
k < heap_struct_ptr->alloc_info.allocations_num; k++) {
if (is_ptr_address_in_heap_area(
heap_struct_ptr,
(void**)heap_struct_ptr->alloc_info.ptr_info_arr[k]
.ptr)) {
if ((size_t)heap_struct_ptr->alloc_info.ptr_info_arr[k]
.ptr > (size_t)(start_mem_ptr)) {
heap_struct_ptr->alloc_info.ptr_info_arr[k].ptr =
(uint8_t**)((size_t)(heap_struct_ptr->alloc_info
.ptr_info_arr[k]
.ptr) -
alloc_size);
}
}
}
/* Defragmentate memory */
uint32_t stop_ind = heap_struct_ptr->offset - alloc_size;
for (uint32_t k = start_ind; k <= stop_ind; k++) {
*(heap_struct_ptr->mem + k) =
*(heap_struct_ptr->mem + k + alloc_size);
}
/* Reassign pointers */
for (uint32_t k = i + 1;
k < heap_struct_ptr->alloc_info.allocations_num; k++) {
*(heap_struct_ptr->alloc_info.ptr_info_arr[k].ptr) -=
alloc_size;
}
/* Actualize ptr info array */
for (uint32_t k = i;
k < heap_struct_ptr->alloc_info.allocations_num - 1; k++) {
heap_struct_ptr->alloc_info.ptr_info_arr[k] =
heap_struct_ptr->alloc_info.ptr_info_arr[k + 1];
}
/* Decrement allocations number */
heap_struct_ptr->alloc_info.allocations_num--;
/* Refresh offset */
heap_struct_ptr->offset = heap_struct_ptr->offset - alloc_size;
/* Fill by 0 all freed memory */
#if FILL_FREED_MEMORY_BY_NULLS
for (uint32_t k = 0; k < alloc_size; k++) {
heap_struct_ptr->mem[heap_struct_ptr->offset + k] = 0;
}
#endif
}
}
}
void dfree(dl_heap_t* heap_struct_ptr, void** ptr,
validate_ptr_condition_t condition) {
/* Check if heap_ptr is not assigned */
if (heap_struct_ptr == NULL) {
LOG_ERROR("Heap pointer is not assigned");
return;
}
uint32_t ptr_index = 0;
/* Try to find given ptr in ptr_info array */
if (validate_ptr(heap_struct_ptr, ptr, condition, &ptr_index) != true) {
LOG_ERROR("Try to free unexisting pointer");
return;
}
uint32_t alloc_size =
ALLOCSIZE_GET(heap_struct_ptr->alloc_info.ptr_info_arr[ptr_index]);
#if USE_ALIGNMENT
while (alloc_size % ALLOCATION_ALIGNMENT_BYTES != 0) {
alloc_size += 1;
}
#endif
/* Edit ptr info array */
FREEFLAG_SET(heap_struct_ptr->alloc_info.ptr_info_arr[ptr_index]);
#if FILL_FREED_MEMORY_BY_NULLS
for (uint32_t i = 0; i < alloc_size; i++) {
*(*(heap_struct_ptr->alloc_info.ptr_info_arr[ptr_index].ptr) + i) = 0;
}
#endif
defrag_memory(heap_struct_ptr);
}
void replace_pointers(dl_heap_t* heap_struct_ptr, void** ptr_to_replace,
void** ptr_new) {
uint32_t ptr_ind = 0;
if (validate_ptr(heap_struct_ptr, ptr_to_replace, USING_PTR_ADDRESS,
&ptr_ind) != true) {
LOG_ERROR("Can't replace pointers. No pointer found in buffer");
return;
}
*ptr_new = *ptr_to_replace;
heap_struct_ptr->alloc_info.ptr_info_arr[ptr_ind].ptr = (uint8_t**)ptr_new;
*ptr_to_replace = NULL;
}
bool drealloc(dl_heap_t* heap_struct_ptr, uint32_t size, void** ptr) {
uint32_t size_of_old_block = 0;
uint32_t old_ptr_ind = 0;
if (validate_ptr(heap_struct_ptr, ptr, USING_PTR_ADDRESS, &old_ptr_ind) ==
true) {
size_of_old_block = ALLOCSIZE_GET(
heap_struct_ptr->alloc_info.ptr_info_arr[old_ptr_ind]);
} else {
return false;
}
uint8_t* new_ptr = NULL;
dalloc(heap_struct_ptr, size, (void**)&new_ptr);
if (new_ptr == NULL) {
LOG_ERROR("drealloc failed due to dalloc failed");
return false;
}
uint8_t* old_ptr = (uint8_t*)(*ptr);
for (uint32_t i = 0; i < size_of_old_block; i++) {
new_ptr[i] = old_ptr[i];
}
dfree(heap_struct_ptr, ptr, USING_PTR_ADDRESS);
replace_pointers(heap_struct_ptr, (void**)&new_ptr, ptr);
return true;
}
void print_dalloc_info(dl_heap_t* heap_struct_ptr) {
PRINTLN("************ Mem Info ************PRINTLN$1");
PRINTLN("Total memory, bytes: %luPRINTLN$1",
(long unsigned int)heap_struct_ptr->total_size);
PRINTLN("Memory in use, bytes: %luPRINTLN$1",
(long unsigned int)heap_struct_ptr->offset);
PRINTLN("Number of allocations: %luPRINTLN$1",
(long unsigned int)heap_struct_ptr->alloc_info.allocations_num);
PRINTLN("The biggest memory was in use: %luPRINTLN$1",
(long unsigned int)heap_struct_ptr->alloc_info.max_memory_amount);
PRINTLN(
"Max allocations number: %luPRINTLN$1",
(long unsigned int)heap_struct_ptr->alloc_info.max_allocations_amount);
PRINTLN("**********************************PRINTLN$1");
}
void dump_heap(dl_heap_t* heap_struct_ptr) {
PRINTLN("************ Dump Heap ***********PRINTLN$1");
for (uint32_t i = 0; i < heap_struct_ptr->total_size; i++) {
PRINT("%02X ", heap_struct_ptr->mem[i]);
}
PRINTLN("**********************************PRINTLN$1");
}
void dump_dalloc_ptr_info(dl_heap_t* heap_struct_ptr) {
PRINTLN("************ Ptr Info ************PRINTLN$1");
for (uint32_t i = 0; i < heap_struct_ptr->alloc_info.allocations_num; i++) {
PRINTLN(
"Ptr address: 0x%08X, ptr first val: 0x%02X, alloc size: "
"%luPRINTLN$1",
(size_t)(heap_struct_ptr->alloc_info.ptr_info_arr[i].ptr),
(uint8_t)(**heap_struct_ptr->alloc_info.ptr_info_arr[i].ptr),
(long unsigned int)ALLOCSIZE_GET(
heap_struct_ptr->alloc_info.ptr_info_arr[i]));
}
PRINTLN("**********************************PRINTLN$1");
}
float get_heap_usage(dl_heap_t* heap_struct_ptr) {
uint32_t alloc_size = 0;
for (uint32_t i = 0; i < heap_struct_ptr->alloc_info.allocations_num; i++) {
alloc_size +=
ALLOCSIZE_GET(heap_struct_ptr->alloc_info.ptr_info_arr[i]);
}
return (float)alloc_size / (float)heap_struct_ptr->total_size;
}