| 123456789101112131415161718192021222324252627282930313233343536 |
- //
- // Created by 李晓明 on 2023/8/16.
- //
- #ifndef CLOX_MEMORY_H
- #define CLOX_MEMORY_H
- #include "common.h"
- #include "object.h"
- #define GROW_CAPACITY(capacity) \
- ((capacity) < 8 ? 8 : (capacity) *2)
- #define GROW_ARRAY(type, pointer, oldCount, newCount) \
- (type *) reallocate(pointer, sizeof(type) * (oldCount), sizeof(type) * (newCount))
- #define FREE_ARRAY(type, pointer, oldCount) \
- reallocate(pointer, sizeof(type) * (oldCount), 0)
- #define FREE(type, pointer) reallocate(pointer, sizeof(type), 0)
- #define ALLOCATE(type, count) \
- (type *) reallocate(NULL, 0, sizeof(type) * (count))
- // oldSize newSize Operation
- // 0 Non‑zero Allocate new block.
- // Non‑zero 0 Free allocation.
- // Non‑zero Smaller than oldSize Shrink existing allocation.
- // Non‑zero Larger than oldSize Grow existing allocation.
- void *reallocate(void *pointer, size_t oldSize, size_t newSize);
- void markObject(Obj* object);
- void markValue(Value value);
- void collectGarbage();
- void freeObjects();
- #endif//CLOX_MEMORY_H
|