void free(void *) {}
-void *realloc(void *ptr, size_t s) {
- free(ptr);
- return malloc(s);
+void *realloc(void *mem, size_t s) {
+ if (mem == nullptr)
+ return malloc(s);
+ uint8_t *newmem = reinterpret_cast<uint8_t *>(malloc(s));
+ if (newmem == nullptr)
+ return nullptr;
+ uint8_t *oldmem = reinterpret_cast<uint8_t *>(mem);
+ // We use a simple for loop to copy the data over.
+ // If |s| is less the previous alloc size, the copy works as expected.
+ // If |s| is greater than the previous alloc size, then garbage is copied
+ // over to the additional part in the new memory block.
+ for (size_t i = 0; i < s; ++i)
+ newmem[i] = oldmem[i];
+ return newmem;
}
// The unit test framework uses pure virtual functions. Since hermetic tests