memory.h 809 B

1234567891011121314151617181920212223242526
  1. //
  2. // Created by 李晓明 on 2023/8/16.
  3. //
  4. #ifndef CLOX_MEMORY_H
  5. #define CLOX_MEMORY_H
  6. #include "common.h"
  7. #define GROW_CAPACITY(capacity) \
  8. ((capacity) < 8 ? 8 : (capacity) *2)
  9. #define GROW_ARRAY(type, pointer, oldCount, newCount) \
  10. (type *) reallocate(pointer, sizeof(type) * (oldCount), sizeof(type) * (newCount))
  11. #define FREE_ARRAY(type, pointer, oldCount) \
  12. reallocate(pointer, sizeof(type) * (oldCount), 0)
  13. // oldSize newSize Operation
  14. // 0 Non‑zero Allocate new block.
  15. // Non‑zero 0 Free allocation.
  16. // Non‑zero Smaller than oldSize Shrink existing allocation.
  17. // Non‑zero Larger than oldSize Grow existing allocation.
  18. void *reallocate(void *pointer, size_t oldSize, size_t newSize);
  19. #endif//CLOX_MEMORY_H