@@ -72,7 +72,10 @@ typedef struct A_BLOCK_LINK
72
72
#if (configUSER_HEAP_STATS == 1 )
73
73
TaskHandle_t xAllocatingTask ; /*<< The task allocating the memory block. */
74
74
TickType_t xTimeAllocated ; /*<< The timestamp of memory block allocation. */
75
- #endif
75
+ #if (configUSER_HEAP_EXTENDED_STATS == 1 )
76
+ struct A_BLOCK_LINK * pxNextTakenBlock , * pxPrevTakenBlock ;
77
+ #endif // configUSER_HEAP_EXTENDED_STATS
78
+ #endif // configUSER_HEAP_STATS
76
79
} BlockLink_t ;
77
80
78
81
/*-----------------------------------------------------------*/
@@ -100,12 +103,22 @@ static const size_t xHeapStructSize = (sizeof(BlockLink_t)
100
103
& ~((size_t ) usermemBYTE_ALIGNMENT_MASK );
101
104
102
105
/* Create a couple of list links to mark the start and end of the list. */
103
- static BlockLink_t userxStart , * userpxEnd = NULL ;
106
+ static BlockLink_t xUserStart , * pxUserEnd = NULL ;
107
+
108
+ /* Start/end of used blocks list */
109
+ #if (configUSER_HEAP_STATS == 1 && configUSER_HEAP_EXTENDED_STATS == 1 )
110
+ static BlockLink_t xUserTakenEnd ;
111
+ #endif
104
112
105
113
/* Keeps track of the number of free bytes remaining, but says nothing about
106
114
fragmentation. */
107
- static size_t userxFreeBytesRemaining = 0U ;
108
- static size_t userxMinimumEverFreeBytesRemaining = 0U ;
115
+ static size_t xUserFreeBytesRemaining = 0U ;
116
+ static size_t xUserMinimumEverFreeBytesRemaining = 0U ;
117
+
118
+ #if (configUSER_HEAP_STATS == 1 && configUSER_HEAP_EXTENDED_STATS == 1 )
119
+ static size_t xUserNumberOfSuccessfulAllocations = 0U ;
120
+ static size_t xUserNumberOfSuccessfulFrees = 0U ;
121
+ #endif
109
122
110
123
/* Allocation statistics */
111
124
static size_t xAllocationsCount = 0 ;
@@ -134,7 +147,7 @@ void *usermalloc(size_t xWantedSize)
134
147
{
135
148
/* If this is the first call to malloc then the heap will require
136
149
initialisation to setup the list of free blocks. */
137
- if ( userpxEnd == NULL )
150
+ if ( pxUserEnd == NULL )
138
151
{
139
152
prvHeapInit ();
140
153
}
@@ -173,12 +186,12 @@ void *usermalloc(size_t xWantedSize)
173
186
mtCOVERAGE_TEST_MARKER ();
174
187
}
175
188
176
- if ( ( xWantedSize > 0 ) && ( xWantedSize <= userxFreeBytesRemaining ) )
189
+ if ( ( xWantedSize > 0 ) && ( xWantedSize <= xUserFreeBytesRemaining ) )
177
190
{
178
191
/* Traverse the list from the start (lowest address) block until
179
192
one of adequate size is found. */
180
- pxPreviousBlock = & userxStart ;
181
- pxBlock = userxStart .pxNextFreeBlock ;
193
+ pxPreviousBlock = & xUserStart ;
194
+ pxBlock = xUserStart .pxNextFreeBlock ;
182
195
while ( ( pxBlock -> xBlockSize < xWantedSize ) && ( pxBlock -> pxNextFreeBlock != NULL ) )
183
196
{
184
197
pxPreviousBlock = pxBlock ;
@@ -187,7 +200,7 @@ void *usermalloc(size_t xWantedSize)
187
200
188
201
/* If the end marker was reached then a block of adequate size
189
202
was not found. */
190
- if ( pxBlock != userpxEnd )
203
+ if ( pxBlock != pxUserEnd )
191
204
{
192
205
/* Return the memory space pointed to - jumping over the
193
206
BlockLink_t structure at its start. */
@@ -236,11 +249,11 @@ void *usermalloc(size_t xWantedSize)
236
249
xAllocatedSum += pxBlock -> xBlockSize ;
237
250
#endif
238
251
239
- userxFreeBytesRemaining -= pxBlock -> xBlockSize ;
252
+ xUserFreeBytesRemaining -= pxBlock -> xBlockSize ;
240
253
241
- if ( userxFreeBytesRemaining < userxMinimumEverFreeBytesRemaining )
254
+ if ( xUserFreeBytesRemaining < xUserMinimumEverFreeBytesRemaining )
242
255
{
243
- userxMinimumEverFreeBytesRemaining = userxFreeBytesRemaining ;
256
+ xUserMinimumEverFreeBytesRemaining = xUserFreeBytesRemaining ;
244
257
}
245
258
else
246
259
{
@@ -251,9 +264,16 @@ void *usermalloc(size_t xWantedSize)
251
264
by the application and has no "next" block. */
252
265
pxBlock -> xBlockSize |= xBlockAllocatedBit ;
253
266
#if (configUSER_HEAP_STATS == 1 )
267
+ #if (configUSER_HEAP_EXTENDED_STATS == 1 )
268
+ /* Push taken block at the end of the taken list */
269
+ xUserTakenEnd .pxPrevTakenBlock -> pxNextTakenBlock = pxBlock ;
270
+ pxBlock -> pxPrevTakenBlock = xUserTakenEnd .pxPrevTakenBlock ;
271
+ pxBlock -> pxNextTakenBlock = & xUserTakenEnd ;
272
+ xUserTakenEnd .pxPrevTakenBlock = pxBlock ;
273
+ #endif // configUSER_HEAP_EXTENDED_STATS
254
274
pxBlock -> xAllocatingTask = xTaskGetCurrentTaskHandle ();
255
275
pxBlock -> xTimeAllocated = xTaskGetTickCount ();
256
- #endif
276
+ #endif // configUSER_HEAP_STATS
257
277
258
278
#if (PROJECT_CONFIG_HEAP_INTEGRITY_CHECKS != 0 )
259
279
{
@@ -265,6 +285,9 @@ void *usermalloc(size_t xWantedSize)
265
285
#endif
266
286
267
287
pxBlock -> pxNextFreeBlock = NULL ;
288
+ #if (configUSER_HEAP_STATS == 1 && configUSER_HEAP_EXTENDED_STATS == 1 )
289
+ xUserNumberOfSuccessfulAllocations ++ ;
290
+ #endif
268
291
}
269
292
else
270
293
{
@@ -350,9 +373,17 @@ void userfree(void *pv)
350
373
#endif
351
374
352
375
/* Add this block to the list of free blocks. */
353
- userxFreeBytesRemaining += pxLink -> xBlockSize ;
376
+ xUserFreeBytesRemaining += pxLink -> xBlockSize ;
354
377
traceFREE ( pv , pxLink -> xBlockSize );
355
378
prvInsertBlockIntoFreeList ( ( ( BlockLink_t * ) pxLink ) );
379
+ #if (configUSER_HEAP_STATS == 1 && configUSER_HEAP_EXTENDED_STATS == 1 )
380
+ xUserNumberOfSuccessfulFrees ++ ;
381
+ /* Update taken list */
382
+ pxLink -> pxPrevTakenBlock -> pxNextTakenBlock = pxLink -> pxNextTakenBlock ;
383
+ pxLink -> pxNextTakenBlock -> pxPrevTakenBlock = pxLink -> pxPrevTakenBlock ;
384
+ pxLink -> pxPrevTakenBlock = NULL ;
385
+ pxLink -> pxNextTakenBlock = NULL ;
386
+ #endif
356
387
}
357
388
( void ) xTaskResumeAll ();
358
389
}
@@ -417,13 +448,13 @@ void *userrealloc(void *pv, size_t xWantedSize) {
417
448
418
449
size_t usermemGetFreeHeapSize ( void )
419
450
{
420
- return userxFreeBytesRemaining ;
451
+ return xUserFreeBytesRemaining ;
421
452
}
422
453
/*-----------------------------------------------------------*/
423
454
424
455
size_t usermemGetMinimumEverFreeHeapSize ( void )
425
456
{
426
- return userxMinimumEverFreeBytesRemaining ;
457
+ return xUserMinimumEverFreeBytesRemaining ;
427
458
}
428
459
/*-----------------------------------------------------------*/
429
460
@@ -478,42 +509,54 @@ size_t xTotalHeapSize = USERMEM_TOTAL_HEAP_SIZE;
478
509
479
510
pucAlignedHeap = ( uint8_t * ) uxAddress ;
480
511
481
- /* userxStart is used to hold a pointer to the first item in the list of free
512
+ /* xUserStart is used to hold a pointer to the first item in the list of free
482
513
blocks. The void cast is used to prevent compiler warnings. */
483
- userxStart .pxNextFreeBlock = ( void * ) pucAlignedHeap ;
484
- userxStart .xBlockSize = ( size_t ) 0 ;
514
+ xUserStart .pxNextFreeBlock = ( void * ) pucAlignedHeap ;
515
+ xUserStart .xBlockSize = ( size_t ) 0 ;
485
516
#if (configUSER_HEAP_STATS == 1 )
486
- userxStart .xTimeAllocated = 0 ;
487
- userxStart .xAllocatingTask = 0 ;
488
- #endif
489
-
490
- /* userpxEnd is used to mark the end of the list of free blocks and is inserted
517
+ xUserStart .xTimeAllocated = 0 ;
518
+ xUserStart .xAllocatingTask = 0 ;
519
+
520
+ #if (configUSER_HEAP_EXTENDED_STATS == 1 )
521
+ xUserStart .pxNextTakenBlock = & xUserTakenEnd ;
522
+ xUserStart .pxPrevTakenBlock = NULL ;
523
+
524
+ xUserTakenEnd .pxNextFreeBlock = NULL ;
525
+ xUserTakenEnd .xBlockSize = 0 ;
526
+ xUserTakenEnd .pxNextTakenBlock = NULL ;
527
+ xUserTakenEnd .pxPrevTakenBlock = & xUserStart ;
528
+ xUserTakenEnd .xTimeAllocated = 0 ;
529
+ xUserTakenEnd .xAllocatingTask = 0 ;
530
+ #endif // configUSER_HEAP_EXTENDED_STATS
531
+ #endif // configUSER_HEAP_STATS
532
+
533
+ /* pxUserEnd is used to mark the end of the list of free blocks and is inserted
491
534
at the end of the heap space. */
492
535
uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize ;
493
536
uxAddress -= xHeapStructSize ;
494
537
uxAddress &= ~( ( size_t ) usermemBYTE_ALIGNMENT_MASK );
495
- userpxEnd = ( void * ) uxAddress ;
496
- userpxEnd -> xBlockSize = 0 ;
497
- userpxEnd -> pxNextFreeBlock = NULL ;
538
+ pxUserEnd = ( void * ) uxAddress ;
539
+ pxUserEnd -> xBlockSize = 0 ;
540
+ pxUserEnd -> pxNextFreeBlock = NULL ;
498
541
#if (configUSER_HEAP_STATS == 1 )
499
- userpxEnd -> xTimeAllocated = 0 ;
500
- userpxEnd -> xAllocatingTask = 0 ;
542
+ pxUserEnd -> xTimeAllocated = 0 ;
543
+ pxUserEnd -> xAllocatingTask = 0 ;
501
544
#endif
502
545
503
546
/* To start with there is a single free block that is sized to take up the
504
- entire heap space, minus the space taken by userpxEnd . */
547
+ entire heap space, minus the space taken by pxUserEnd . */
505
548
pxFirstFreeBlock = ( void * ) pucAlignedHeap ;
506
549
pxFirstFreeBlock -> xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock ;
507
- pxFirstFreeBlock -> pxNextFreeBlock = userpxEnd ;
550
+ pxFirstFreeBlock -> pxNextFreeBlock = pxUserEnd ;
508
551
#if ( PROJECT_CONFIG_HEAP_INTEGRITY_CHECKS != 0 )
509
552
{
510
553
pxFirstFreeBlock -> ulStamp = INTEGRITY_STAMP_FREE ;
511
554
}
512
555
#endif
513
556
514
557
/* Only one block exists - and it covers the entire usable heap space. */
515
- userxMinimumEverFreeBytesRemaining = pxFirstFreeBlock -> xBlockSize ;
516
- userxFreeBytesRemaining = pxFirstFreeBlock -> xBlockSize ;
558
+ xUserMinimumEverFreeBytesRemaining = pxFirstFreeBlock -> xBlockSize ;
559
+ xUserFreeBytesRemaining = pxFirstFreeBlock -> xBlockSize ;
517
560
518
561
/* Work out the position of the top bit in a size_t variable. */
519
562
xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof ( size_t ) * heapBITS_PER_BYTE ) - 1 );
@@ -527,7 +570,7 @@ uint8_t *puc;
527
570
528
571
/* Iterate through the list until a block is found that has a higher address
529
572
than the block being inserted. */
530
- for ( pxIterator = & userxStart ; pxIterator -> pxNextFreeBlock < pxBlockToInsert ; pxIterator = pxIterator -> pxNextFreeBlock )
573
+ for ( pxIterator = & xUserStart ; pxIterator -> pxNextFreeBlock < pxBlockToInsert ; pxIterator = pxIterator -> pxNextFreeBlock )
531
574
{
532
575
/* Nothing to do here, just iterate to the right position. */
533
576
}
@@ -550,15 +593,15 @@ uint8_t *puc;
550
593
puc = ( uint8_t * ) pxBlockToInsert ;
551
594
if ( ( puc + pxBlockToInsert -> xBlockSize ) == ( uint8_t * ) pxIterator -> pxNextFreeBlock )
552
595
{
553
- if ( pxIterator -> pxNextFreeBlock != userpxEnd )
596
+ if ( pxIterator -> pxNextFreeBlock != pxUserEnd )
554
597
{
555
598
/* Form one big block from the two blocks. */
556
599
pxBlockToInsert -> xBlockSize += pxIterator -> pxNextFreeBlock -> xBlockSize ;
557
600
pxBlockToInsert -> pxNextFreeBlock = pxIterator -> pxNextFreeBlock -> pxNextFreeBlock ;
558
601
}
559
602
else
560
603
{
561
- pxBlockToInsert -> pxNextFreeBlock = userpxEnd ;
604
+ pxBlockToInsert -> pxNextFreeBlock = pxUserEnd ;
562
605
}
563
606
}
564
607
else
0 commit comments