-
Notifications
You must be signed in to change notification settings - Fork 28
/
CodalHeapAllocator.cpp
429 lines (354 loc) · 13.1 KB
/
CodalHeapAllocator.cpp
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
The MIT License (MIT)
Copyright (c) 2017 Lancaster University.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/**
* A simple 32 bit block based memory allocator. This allows one or more memory segments to
* be designated as heap storage, and is designed to run in a static memory area or inside the standard C
* heap for use by the codal device runtime. This is required for several reasons:
*
* 1) It reduces memory fragmentation due to the high churn sometime placed on the heap
* by ManagedTypes, fibers and user code. Underlying heap implentations are often have very simplistic
* allocation pilicies and suffer from fragmentation in prolonged use - which can cause programs to
* stop working after a period of time. The algorithm implemented here is simple, but highly tolerant to
* large amounts of churn.
*
* 2) It allows us to reuse the 8K of SRAM set aside for SoftDevice as additional heap storage
* when BLE is not in use.
*
* 3) It gives a simple example of how memory allocation works! :-)
*
* P.S. This is a very simple allocator, therefore not without its weaknesses. Why don't you consider
* what these are, and consider the tradeoffs against simplicity...
*
* @note The need for this should be reviewed in the future, if a different memory allocator is
* made available in the mbed platform.
*
* TODO: Consider caching recently freed blocks to improve allocation time.
*/
#include "CodalConfig.h"
#include "CodalHeapAllocator.h"
#include "platform_includes.h"
#include "CodalDevice.h"
#include "CodalCompat.h"
#include "CodalDmesg.h"
#include "ErrorNo.h"
using namespace codal;
#if CONFIG_ENABLED(DEVICE_HEAP_ALLOCATOR)
// A list of all active heap regions, and their dimensions in memory.
HeapDefinition heap[DEVICE_MAXIMUM_HEAPS] = { };
uint8_t heap_count = 0;
#if (CODAL_DEBUG >= CODAL_DEBUG_HEAP)
// Diplays a usage summary about a given heap...
void device_heap_print(HeapDefinition &heap)
{
PROCESSOR_WORD_TYPE blockSize;
PROCESSOR_WORD_TYPE *block;
int totalFreeBlock = 0;
int totalUsedBlock = 0;
if (heap.heap_start == NULL)
{
DMESG("--- HEAP NOT INITIALISED ---");
return;
}
DMESG("heap_start : %p", heap.heap_start);
DMESG("heap_end : %p", heap.heap_end);
DMESG("heap_size : %d", (int)heap.heap_end - (int)heap.heap_start);
// Disable IRQ temporarily to ensure no race conditions!
target_disable_irq();
block = heap.heap_start;
while (block < heap.heap_end)
{
blockSize = *block & ~DEVICE_HEAP_BLOCK_FREE;
if (*block & DEVICE_HEAP_BLOCK_FREE)
DMESGN("[F:%d] ", blockSize*DEVICE_HEAP_BLOCK_SIZE);
else
DMESGN("[U:%d] ", blockSize*DEVICE_HEAP_BLOCK_SIZE);
if (*block & DEVICE_HEAP_BLOCK_FREE)
totalFreeBlock += blockSize;
else
totalUsedBlock += blockSize;
block += blockSize;
}
// Enable Interrupts
target_enable_irq();
DMESG("\n");
DMESG("mb_total_free : %d", totalFreeBlock*DEVICE_HEAP_BLOCK_SIZE);
DMESG("mb_total_used : %d", totalUsedBlock*DEVICE_HEAP_BLOCK_SIZE);
}
// Diagnostics function. Displays a usage summary about all initialised heaps.
void device_heap_print()
{
for (int i=0; i < heap_count; i++)
{
DMESG("\nHEAP %d: ", i);
device_heap_print(heap[i]);
}
}
#endif
/**
* Create and initialise a given memory region as for heap storage.
* After this is called, any future calls to malloc, new, free or delete may use the new heap.
* The heap allocator will attempt to allocate memory from heaps in the order that they are created.
* i.e. memory will be allocated from first heap created until it is full, then the second heap, and so on.
*
* @param start The start address of memory to use as a heap region.
*
* @param end The end address of memory to use as a heap region.
*
* @return DEVICE_OK on success, or DEVICE_NO_RESOURCES if the heap could not be allocated.
*
* @note Only code that #includes DeviceHeapAllocator.h will use this heap. This includes all codal device runtime
* code, and user code targetting the runtime. External code can choose to include this file, or
* simply use the standard heap.
*/
int device_create_heap(PROCESSOR_WORD_TYPE start, PROCESSOR_WORD_TYPE end)
{
HeapDefinition *h = &heap[heap_count];
#if CONFIG_ENABLED(CODAL_LOW_LEVEL_VALIDATION)
// Ensure we don't exceed the maximum number of heap segments.
if (heap_count == DEVICE_MAXIMUM_HEAPS)
return DEVICE_NO_RESOURCES;
// Sanity check. Ensure range is valid, large enough and word aligned.
if (end <= start || end - start < DEVICE_HEAP_BLOCK_SIZE*2 || end % DEVICE_HEAP_BLOCK_SIZE != 0 || start % DEVICE_HEAP_BLOCK_SIZE != 0)
return DEVICE_INVALID_PARAMETER;
#endif
// Disable IRQ temporarily to ensure no race conditions!
target_disable_irq();
// Record the dimensions of this new heap
h->heap_start = (PROCESSOR_WORD_TYPE *)start;
h->heap_end = (PROCESSOR_WORD_TYPE *)end;
// Initialise the heap as being completely empty and available for use.
*h->heap_start = DEVICE_HEAP_BLOCK_FREE | (((PROCESSOR_WORD_TYPE) h->heap_end - (PROCESSOR_WORD_TYPE) h->heap_start) / DEVICE_HEAP_BLOCK_SIZE);
heap_count++;
// Enable Interrupts
target_enable_irq();
#if (CODAL_DEBUG >= CODAL_DEBUG_HEAP)
device_heap_print();
#endif
return DEVICE_OK;
}
uint32_t device_heap_size(uint8_t heap_index)
{
if (heap_index >= heap_count)
return 0;
HeapDefinition *h = &heap[heap_index];
return (uint8_t*)h->heap_end - (uint8_t*)h->heap_start;
}
/**
* Attempt to allocate a given amount of memory from a given heap area.
*
* @param size The amount of memory, in bytes, to allocate.
* @param heap The heap to allocate memory from.
*
* @return A pointer to the allocated memory, or NULL if insufficient memory is available.
*/
REAL_TIME_FUNC
void *device_malloc_in(size_t size, HeapDefinition &heap)
{
PROCESSOR_WORD_TYPE blockSize = 0;
PROCESSOR_WORD_TYPE blocksNeeded = size % DEVICE_HEAP_BLOCK_SIZE == 0 ? size / DEVICE_HEAP_BLOCK_SIZE : size / DEVICE_HEAP_BLOCK_SIZE + 1;
PROCESSOR_WORD_TYPE *block;
PROCESSOR_WORD_TYPE *next;
if (size <= 0)
return NULL;
// Account for the index block;
blocksNeeded++;
// Disable IRQ temporarily to ensure no race conditions!
target_disable_irq();
// We implement a first fit algorithm with cache to handle rapid churn...
// We also defragment free blocks as we search, to optimise this and future searches.
block = heap.heap_start;
while (block < heap.heap_end)
{
// If the block is used, then keep looking.
if(!(*block & DEVICE_HEAP_BLOCK_FREE))
{
block += *block;
continue;
}
blockSize = *block & ~DEVICE_HEAP_BLOCK_FREE;
// We have a free block. Let's see if the subsequent ones are too. If so, we can merge...
next = block + blockSize;
while (*next & DEVICE_HEAP_BLOCK_FREE)
{
if (next >= heap.heap_end)
break;
// We can merge!
blockSize += (*next & ~DEVICE_HEAP_BLOCK_FREE);
*block = blockSize | DEVICE_HEAP_BLOCK_FREE;
next = block + blockSize;
}
// We have a free block. Let's see if it's big enough.
// If so, we have a winner.
if (blockSize >= blocksNeeded)
break;
// Otherwise, keep looking...
block += blockSize;
}
// We're full!
if (block >= heap.heap_end)
{
target_enable_irq();
return NULL;
}
// If we're at the end of memory or have very near match then mark the whole segment as in use.
if (blockSize <= blocksNeeded+1 || block+blocksNeeded+1 >= heap.heap_end)
{
// Just mark the whole block as used.
*block &= ~DEVICE_HEAP_BLOCK_FREE;
}
else
{
// We need to split the block.
PROCESSOR_WORD_TYPE *splitBlock = block + blocksNeeded;
*splitBlock = blockSize - blocksNeeded;
*splitBlock |= DEVICE_HEAP_BLOCK_FREE;
*block = blocksNeeded;
}
// Enable Interrupts
target_enable_irq();
return block+1;
}
/**
* Attempt to allocate a given amount of memory from any of our configured heap areas.
*
* @param size The amount of memory, in bytes, to allocate.
*
* @return A pointer to the allocated memory, or NULL if insufficient memory is available.
*/
REAL_TIME_FUNC
void* device_malloc (size_t size)
{
static uint8_t initialised = 0;
void *p;
if (size <= 0)
return NULL;
if (!initialised)
{
heap_count = 0;
#if CONFIG_ENABLED(CODAL_LOW_LEVEL_VALIDATION)
if(device_create_heap((PROCESSOR_WORD_TYPE)(codal_heap_start), (PROCESSOR_WORD_TYPE)(DEVICE_STACK_BASE) - (PROCESSOR_WORD_TYPE)(DEVICE_STACK_SIZE)) == DEVICE_INVALID_PARAMETER)
target_panic(DEVICE_HEAP_ERROR);
#else
device_create_heap((PROCESSOR_WORD_TYPE)(codal_heap_start), (PROCESSOR_WORD_TYPE)(DEVICE_STACK_BASE) - (PROCESSOR_WORD_TYPE)(DEVICE_STACK_SIZE));
#endif
initialised = 1;
}
#if (DEVICE_MAXIMUM_HEAPS == 1)
p = device_malloc_in(size, heap[0]);
#else
// Assign the memory from the first heap created that has space.
for (int i=0; i < heap_count; i++)
{
p = device_malloc_in(size, heap[i]);
if (p != NULL)
break;
}
#endif
if (p != NULL)
{
#if (CODAL_DEBUG >= CODAL_DEBUG_HEAP)
DMESG("device_malloc: ALLOCATED: %d [%p]", size, p);
#endif
return p;
}
// We're totally out of options (and memory!).
#if (CODAL_DEBUG >= CODAL_DEBUG_HEAP)
// Keep everything transparent if we've not been initialised yet
DMESG("device_malloc: OUT OF MEMORY [%d]", size);
#endif
#if CONFIG_ENABLED(DEVICE_PANIC_HEAP_FULL)
target_panic(DEVICE_OOM);
#endif
return NULL;
}
/**
* Release a given area of memory from the heap.
*
* @param mem The memory area to release.
*/
REAL_TIME_FUNC
void device_free (void *mem)
{
PROCESSOR_WORD_TYPE *memory = (PROCESSOR_WORD_TYPE *)mem;
PROCESSOR_WORD_TYPE *cb = memory-1;
int i=0;
#if (CODAL_DEBUG >= CODAL_DEBUG_HEAP)
if (heap_count > 0)
DMESG("device_free: %p", mem);
#endif
// Sanity check.
if (memory == NULL)
return;
// If this memory was created from a heap registered with us, free it.
#if (DEVICE_MAXIMUM_HEAPS > 1)
for (i=0; i < heap_count; i++)
#endif
{
if(memory > heap[i].heap_start && memory < heap[i].heap_end)
{
// The memory block given is part of this heap, so we can simply
// flag that this memory area is now free, and we're done.
if (*cb == 0 || *cb & DEVICE_HEAP_BLOCK_FREE)
target_panic(DEVICE_HEAP_ERROR);
*cb |= DEVICE_HEAP_BLOCK_FREE;
return;
}
}
// If we reach here, then the memory is not part of any registered heap.
target_panic(DEVICE_HEAP_ERROR);
}
void* calloc (size_t num, size_t size)
{
void *mem = malloc(num*size);
if (mem) {
// without this write, GCC will happily optimize malloc() above into calloc()
// and remove the memset
((uint32_t*)mem)[0] = 1;
memset(mem, 0, num*size);
}
return mem;
}
extern "C" void* device_realloc (void* ptr, size_t size)
{
void *mem = malloc(size);
// handle the simplest case - no previous memory allocted.
if (ptr != NULL && mem != NULL)
{
// Otherwise we need to copy and free up the old data.
PROCESSOR_WORD_TYPE *cb = ((PROCESSOR_WORD_TYPE *)ptr) - 1;
PROCESSOR_WORD_TYPE blockSize = *cb & ~DEVICE_HEAP_BLOCK_FREE;
memcpy(mem, ptr, min(blockSize * sizeof(PROCESSOR_WORD_TYPE), size));
free(ptr);
}
return mem;
}
void *malloc(size_t sz) __attribute__ ((weak, alias ("device_malloc")));
void free(void *mem) __attribute__ ((weak, alias ("device_free")));
void* realloc (void* ptr, size_t size) __attribute__ ((weak, alias ("device_realloc")));
// make sure the libc allocator is not pulled in
void *_malloc_r(struct _reent *, size_t len)
{
return malloc(len);
}
void _free_r(struct _reent *, void *addr)
{
free(addr);
}
#endif