|
4 | 4 | #include "fastpow.h" |
5 | 5 | #include "fastlog.h" |
6 | 6 |
|
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); |
8 | 31 | vPortFree(ptr); |
9 | | - ptr = pvPortMalloc(size); |
10 | | - if(ptr != NULL) |
11 | | - memset(ptr, 0, size); |
12 | | - return ptr; |
| 32 | + return p; |
13 | 33 | } |
14 | 34 |
|
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){ |
16 | 39 | size_t xWantedSize = nmemb*size; |
17 | 40 | void* ptr = pvPortMalloc(xWantedSize); |
18 | 41 | if(ptr != NULL) |
|
0 commit comments