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
36
37
38
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
63
64
65
66
67
69
70
74
75
76
77
83
84
85
89
90
91
92
93
95
96
97
98
99
100
102
103
104
105
106
107
111
112
113
114
115
116
117
118
119
120
121
122
124
128
132
133
137
138
139
141
142
143
144
145
147
153
157
158
162
163
164
165
167
168
169
174
175
177
178
179
181
182
183
185
186
187
189
190
191
195
196
197
198
200
201
202
203
204
205
206
210
211
212
213
217
221
222
224
225
226
227
228
232
233
237
238
242
243
244
245
246
247
248
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
275
276
277
278
279
280
281
282
283
284
285
286
287
288
290
291
292
293
300
301
302
306
307
311
312
313
314
315
319
320
321
325
326
327
331
332
333
334
335
336
337
338
339
340
341
342
343
349
350
351
352
354
355
356
357
359
360
361
362
363
364
365
366
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
388
392
393
395
396
401
405
406
408
409
422
426
427
431
435
439
440
441
442
443
444
445
446
447
448
449
450
451
453
454
455
456
457
459
460
461
465
466
470
471
473
474
475
476
477
478
479
480
481
482
483
484
490
491
492
/* ... */
/* ... */
#include <stdlib.h>
/* ... */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#include "FreeRTOS.h"
#include "task.h"
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
#error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
#endif
#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
#define heapBITS_PER_BYTE ( ( size_t ) 8 )
#if( configAPPLICATION_ALLOCATED_HEAP == 1 )
/* ... */
extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];/* ... */
#else
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
#endif
/* ... */
typedef struct A_BLOCK_LINK
{
struct A_BLOCK_LINK *pxNextFreeBlock;
size_t xBlockSize;
...} BlockLink_t;
/* ... */
static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );
/* ... */
static void prvHeapInit( void );
/* ... */
static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
static BlockLink_t xStart, *pxEnd = NULL;
/* ... */
static size_t xFreeBytesRemaining = 0U;
static size_t xMinimumEverFreeBytesRemaining = 0U;
static size_t xNumberOfSuccessfulAllocations = 0;
static size_t xNumberOfSuccessfulFrees = 0;
/* ... */
static size_t xBlockAllocatedBit = 0;
void *pvPortMalloc( size_t xWantedSize )
{
BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
void *pvReturn = NULL;
vTaskSuspendAll();
{
/* ... */
if( pxEnd == NULL )
{
prvHeapInit();
}if (pxEnd == NULL) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
/* ... */
if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
{
/* ... */
if( xWantedSize > 0 )
{
xWantedSize += xHeapStructSize;
/* ... */
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
{
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
}if (( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (xWantedSize > 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
{
/* ... */
pxPreviousBlock = &xStart;
pxBlock = xStart.pxNextFreeBlock;
while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
{
pxPreviousBlock = pxBlock;
pxBlock = pxBlock->pxNextFreeBlock;
}while (( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL )) { ... }
/* ... */
if( pxBlock != pxEnd )
{
/* ... */
pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
/* ... */
pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
/* ... */
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
{
/* ... */
pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
configASSERT( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );
/* ... */
pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
pxBlock->xBlockSize = xWantedSize;
prvInsertBlockIntoFreeList( pxNewBlockLink );
}if (( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
xFreeBytesRemaining -= pxBlock->xBlockSize;
if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
{
xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
}if (xFreeBytesRemaining < xMinimumEverFreeBytesRemaining) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
/* ... */
pxBlock->xBlockSize |= xBlockAllocatedBit;
pxBlock->pxNextFreeBlock = NULL;
xNumberOfSuccessfulAllocations++;
}if (pxBlock != pxEnd) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining )) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (( xWantedSize & xBlockAllocatedBit ) == 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
traceMALLOC( pvReturn, xWantedSize );
...}
( void ) xTaskResumeAll();
#if( configUSE_MALLOC_FAILED_HOOK == 1 )
{
if( pvReturn == NULL )
{
extern void vApplicationMallocFailedHook( void );
vApplicationMallocFailedHook();
}if (pvReturn == NULL) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
...}/* ... */
#endif
configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) portBYTE_ALIGNMENT_MASK ) == 0 );
return pvReturn;
}{ ... }
void vPortFree( void *pv )
{
uint8_t *puc = ( uint8_t * ) pv;
BlockLink_t *pxLink;
if( pv != NULL )
{
/* ... */
puc -= xHeapStructSize;
pxLink = ( void * ) puc;
configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
configASSERT( pxLink->pxNextFreeBlock == NULL );
if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
{
if( pxLink->pxNextFreeBlock == NULL )
{
/* ... */
pxLink->xBlockSize &= ~xBlockAllocatedBit;
vTaskSuspendAll();
{
xFreeBytesRemaining += pxLink->xBlockSize;
traceFREE( pv, pxLink->xBlockSize );
prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
xNumberOfSuccessfulFrees++;
...}
( void ) xTaskResumeAll();
}if (pxLink->pxNextFreeBlock == NULL) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (( pxLink->xBlockSize & xBlockAllocatedBit ) != 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (pv != NULL) { ... }
}{ ... }
size_t xPortGetFreeHeapSize( void )
{
return xFreeBytesRemaining;
}{ ... }
size_t xPortGetMinimumEverFreeHeapSize( void )
{
return xMinimumEverFreeBytesRemaining;
}{ ... }
void vPortInitialiseBlocks( void )
{
}{ ... }
static void prvHeapInit( void )
{
BlockLink_t *pxFirstFreeBlock;
uint8_t *pucAlignedHeap;
size_t uxAddress;
size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
uxAddress = ( size_t ) ucHeap;
if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
{
uxAddress += ( portBYTE_ALIGNMENT - 1 );
uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
}if (( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0) { ... }
pucAlignedHeap = ( uint8_t * ) uxAddress;
/* ... */
xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
xStart.xBlockSize = ( size_t ) 0;
/* ... */
uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;
uxAddress -= xHeapStructSize;
uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
pxEnd = ( void * ) uxAddress;
pxEnd->xBlockSize = 0;
pxEnd->pxNextFreeBlock = NULL;
/* ... */
pxFirstFreeBlock = ( void * ) pucAlignedHeap;
pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
}{ ... }
static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )
{
BlockLink_t *pxIterator;
uint8_t *puc;
/* ... */
for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
{
}for (pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock) { ... }
/* ... */
puc = ( uint8_t * ) pxIterator;
if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
{
pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
pxBlockToInsert = pxIterator;
}if (( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
/* ... */
puc = ( uint8_t * ) pxBlockToInsert;
if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
{
if( pxIterator->pxNextFreeBlock != pxEnd )
{
pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
}if (pxIterator->pxNextFreeBlock != pxEnd) { ... }
else
{
pxBlockToInsert->pxNextFreeBlock = pxEnd;
}else { ... }
}if (( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock) { ... }
else
{
pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
}else { ... }
/* ... */
if( pxIterator != pxBlockToInsert )
{
pxIterator->pxNextFreeBlock = pxBlockToInsert;
}if (pxIterator != pxBlockToInsert) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}{ ... }
void vPortGetHeapStats( HeapStats_t *pxHeapStats )
{
BlockLink_t *pxBlock;
size_t xBlocks = 0, xMaxSize = 0, xMinSize = portMAX_DELAY;
vTaskSuspendAll();
{
pxBlock = xStart.pxNextFreeBlock;
/* ... */
if( pxBlock != NULL )
{
do
{
/* ... */
xBlocks++;
if( pxBlock->xBlockSize > xMaxSize )
{
xMaxSize = pxBlock->xBlockSize;
}if (pxBlock->xBlockSize > xMaxSize) { ... }
if( pxBlock->xBlockSize < xMinSize )
{
xMinSize = pxBlock->xBlockSize;
}if (pxBlock->xBlockSize < xMinSize) { ... }
/* ... */
pxBlock = pxBlock->pxNextFreeBlock;
...} while( pxBlock != pxEnd );
}if (pxBlock != NULL) { ... }
...}
xTaskResumeAll();
pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;
pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;
pxHeapStats->xNumberOfFreeBlocks = xBlocks;
taskENTER_CRITICAL();
{
pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;
pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;
pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;
pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;
...}
taskEXIT_CRITICAL();
}{ ... }