void* unity_realloc(void * oldMem, size_t size) { Guard* guard = (Guard*)oldMem; // char* memAsChar = (char*)oldMem; void* newMem; if (oldMem == 0) return unity_malloc(size); guard--; if (isOverrun(oldMem)) { release_memory(oldMem); TEST_FAIL_MESSAGE("Buffer overrun detected during realloc()"); } if (size == 0) { release_memory(oldMem); return 0; } if (guard->size >= size) return oldMem; newMem = unity_malloc(size); memcpy(newMem, oldMem, guard->size); unity_free(oldMem); return newMem; }
void* unity_realloc(void * oldMem, size_t size) { Guard* guard = (Guard*)oldMem; // char* memAsChar = (char*)oldMem; void* newMem; if (oldMem == 0) return unity_malloc(size); guard--; if (isOverrun(oldMem)) { release_memory(oldMem); /*DX_PATCH for jumless version*/ UnityPrint("Buffer overrun detected during realloc()", unity_p); return 0; } if (size == 0) { release_memory(oldMem); return 0; } if (guard->size >= size) return oldMem; newMem = unity_malloc(size); memcpy(newMem, oldMem, guard->size); unity_free(oldMem); return newMem; }
void* unity_calloc(size_t num, size_t size) { void* mem = unity_malloc(num * size); if (mem == NULL) return NULL; memset(mem, 0, num * size); return mem; }
void* unity_realloc(void* oldMem, size_t size) { Guard* guard = (Guard*)oldMem; void* newMem; if (oldMem == NULL) return unity_malloc(size); guard--; if (isOverrun(oldMem)) { release_memory(oldMem); UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during realloc()"); } if (size == 0) { release_memory(oldMem); return NULL; } if (guard->size >= size) return oldMem; #ifdef UNITY_EXCLUDE_STDLIB_MALLOC /* Optimization if memory is expandable */ if (oldMem == unity_heap + heap_index - guard->size - sizeof(end) && heap_index + size - guard->size <= UNITY_INTERNAL_HEAP_SIZE_BYTES) { release_memory(oldMem); /* Not thread-safe, like unity_heap generally */ return unity_malloc(size); /* No memcpy since data is in place */ } #endif newMem = unity_malloc(size); if (newMem == NULL) return NULL; /* Do not release old memory */ memcpy(newMem, oldMem, guard->size); release_memory(oldMem); return newMem; }