memory.c 559 B

123456789101112131415161718
  1. #include "memory.h"
  2. #include <stdlib.h>
  3. // oldSize newSize Operation
  4. // 0 Non‑zero Allocate new block.
  5. // Non‑zero 0 Free allocation.
  6. // Non‑zero Smaller than oldSize Shrink existing allocation.
  7. // Non‑zero Larger than oldSize Grow existing allocation.
  8. void *reallocate(void *pointer, size_t oldSize, size_t newSize) {
  9. if (newSize == 0) {
  10. free(pointer);
  11. return NULL;
  12. }
  13. void *result = realloc(pointer, newSize);
  14. if (result == NULL) exit(1);
  15. return result;
  16. }