memory.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // Created by 李晓明 on 2023/8/16.
  3. //
  4. #ifndef CLOX_MEMORY_H
  5. #define CLOX_MEMORY_H
  6. #include "common.h"
  7. #include "object.h"
  8. #define GROW_CAPACITY(capacity) \
  9. ((capacity) < 8 ? 8 : (capacity) *2)
  10. #define GROW_ARRAY(type, pointer, oldCount, newCount) \
  11. (type *) reallocate(pointer, sizeof(type) * (oldCount), sizeof(type) * (newCount))
  12. #define FREE_ARRAY(type, pointer, oldCount) \
  13. reallocate(pointer, sizeof(type) * (oldCount), 0)
  14. #define FREE(type, pointer) reallocate(pointer, sizeof(type), 0)
  15. #define ALLOCATE(type, count) \
  16. (type *) reallocate(NULL, 0, sizeof(type) * (count))
  17. // oldSize newSize Operation
  18. // 0 Non‑zero Allocate new block.
  19. // Non‑zero 0 Free allocation.
  20. // Non‑zero Smaller than oldSize Shrink existing allocation.
  21. // Non‑zero Larger than oldSize Grow existing allocation.
  22. void *reallocate(void *pointer, size_t oldSize, size_t newSize);
  23. void markObject(Obj* object);
  24. void markValue(Value value);
  25. void collectGarbage();
  26. void freeObjects();
  27. #endif//CLOX_MEMORY_H