In FreeRTOS, there is only malloc and free , but there is no calloc and realloc . I need realloc and I tried to implement it myself. Is everything right here? The memory management module itself can be found here .
void *pvPortRealloc(void *ptr, size_t s) { uint8_t *puc = ( uint8_t * ) ptr; BlockLink_t *pxLink; void *newBlock; if (ptr != NULL) { puc -= heapSTRUCT_SIZE; pxLink = (void*) puc; vTaskSuspendAll(); { newBlock = pvPortMalloc(s); memcpy(newBlock, ptr, pxLink->xBlockSize); vPortFree(ptr); } xTaskResumeAll(); return newBlock; } return NULL; } Here is the new version created after the AnT comments.
void *pvPortRealloc(void *ptr, size_t s) { uint8_t *puc = ( uint8_t * ) ptr; BlockLink_t *pxLink; void *newBlock; extern void memcpy( void*, void*, size_t ); size_t blockSize; if (ptr == NULL) { newBlock = pvPortMalloc(s); } else { puc -= heapSTRUCT_SIZE; pxLink = (void*) puc; blockSize = pxLink->xBlockSize - heapSTRUCT_SIZE; if (s == 0) { newBlock = NULL; } else if (s > blockSize) { vTaskSuspendAll(); { newBlock = pvPortMalloc(s); if (newBlock != NULL) { memcpy(newBlock, ptr, blockSize); vPortFree(ptr); } } xTaskResumeAll(); } else //s < blockSize { vTaskSuspendAll(); { size_t newBlockSize = s + heapSTRUCT_SIZE + ( portBYTE_ALIGNMENT - ( s & portBYTE_ALIGNMENT_MASK ) ); //if right segment is to few, do nothing if (pxLink->xBlockSize - newBlockSize > heapSTRUCT_SIZE) { //split current block pxLink->xBlockSize = newBlockSize; BlockLink_t *blockToFree = (void*)((uint8_t*)pxLink + pxLink->xBlockSize); blockToFree->xBlockSize = blockSize + heapSTRUCT_SIZE - pxLink->xBlockSize; //free second part prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) blockToFree ) ); xFreeBytesRemaining += blockToFree->xBlockSize; } newBlock = ptr; } xTaskResumeAll(); } } return newBlock; }
s = 0? What will happen if the size is equal to the previous one? - 0andriypvPortMallocandvPortFreethis is done. - maestro