Skip to content

Commit 95ed80b

Browse files
author
Martin Klang
committed
fixed realloc
1 parent acdbffa commit 95ed80b

3 files changed

Lines changed: 45 additions & 6 deletions

File tree

LibSource/basicmaths.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,38 @@
44
#include "fastpow.h"
55
#include "fastlog.h"
66

7-
void* pvPortRealloc(void *ptr, size_t size ){
7+
/* The realloc() function changes the size of the memory block pointed to */
8+
/* by ptr to size bytes. The contents will be unchanged in the range from */
9+
/* the start of the region up to the minimum of the old and new sizes. If */
10+
/* the new size is larger than the old size, the added memory will not be */
11+
/* initialized. If ptr is NULL, then the call is equivalent to mal‐ */
12+
/* loc(size), for all values of size; if size is equal to zero, and ptr is */
13+
/* not NULL, then the call is equivalent to free(ptr). Unless ptr is */
14+
/* NULL, it must have been returned by an earlier call to malloc(), cal‐ */
15+
/* loc(), or realloc(). If the area pointed to was moved, a free(ptr) is */
16+
/* done. */
17+
void* pvPortRealloc(void *ptr, size_t new_size) {
18+
if(ptr == NULL)
19+
return pvPortMalloc(new_size);
20+
size_t old_size = vPortGetSizeBlock(ptr);
21+
if(new_size == 0){
22+
vPortFree(ptr);
23+
return NULL;
24+
}
25+
if(new_size <= old_size)
26+
return ptr;
27+
void* p = pvPortMalloc(new_size);
28+
if(p == NULL)
29+
return p;
30+
memcpy(p, ptr, old_size);
831
vPortFree(ptr);
9-
ptr = pvPortMalloc(size);
10-
if(ptr != NULL)
11-
memset(ptr, 0, size);
12-
return ptr;
32+
return p;
1333
}
1434

15-
void *pvPortCalloc(size_t nmemb, size_t size){
35+
/* The calloc() function allocates memory for an array of nmemb elements */
36+
/* of size bytes each and returns a pointer to the allocated memory. */
37+
/* The memory is set to zero. */
38+
void *pvPortCalloc(size_t nmemb, size_t size){
1639
size_t xWantedSize = nmemb*size;
1740
void* ptr = pvPortMalloc(xWantedSize);
1841
if(ptr != NULL)

Source/heap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
size_t xPortGetFreeHeapSize( void );
2828
size_t xPortGetMinimumEverFreeHeapSize( void );
2929
void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions );
30+
int vPortGetSizeBlock (void *pv);
3031

3132
#ifdef __cplusplus
3233
}

Source/heap_5.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,18 @@ const HeapRegion_t *pxHeapRegion;
537537
xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );
538538
}
539539

540+
/*
541+
* ref: https://sudonull.com/post/25551-We-embed-the-Lua-interpreter-in-the-project-for-the-microcontroller-stm32
542+
*/
543+
int vPortGetSizeBlock (void *pv) {
544+
uint8_t *puc = (uint8_t *)pv;
545+
BlockLink_t *pxLink;
546+
if (pv != NULL) {
547+
puc -= uxHeapStructSize;
548+
pxLink = (BlockLink_t *)puc;
549+
configASSERT((pxLink->xBlockSize & xBlockAllocatedBit) != 0);
550+
configASSERT(pxLink->pxNextFreeBlock == NULL);
551+
return pxLink->xBlockSize & ~xBlockAllocatedBit;
552+
}
553+
return 0;
554+
}

0 commit comments

Comments
 (0)