-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
31 lines (22 loc) · 1.04 KB
/
memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// Created by JP Ramassini on 10/23/19.
//
#ifndef CLOX_MEMORY_H
#define CLOX_MEMORY_H
#include "object.h"
#define ALLOCATE(type, count) \
(type*)reallocate(NULL, 0, sizeof(type) * (count))
// Just reuses the reallocate logic to "resize" to zero bytes.
// Sending this through reallocate allows us to keep track of how much memory is actively being used.
#define FREE(type, pointer) \
reallocate(pointer, sizeof(type), 0)
#define GROW_CAPACITY(capacity) \
((capacity) < 8 ? 8 : (capacity) * 2) // Note: Double capacity each time we need to increase. (Except for initial grow, which should be 8.)
// A convenience macro to grab the type size and cast the resulting void* to the right type.
#define GROW_ARRAY(previous, type, oldCount, count) \
(type*)reallocate(previous, sizeof(type) * (oldCount), sizeof(type) * (count))
#define FREE_ARRAY(type, pointer, oldCount) \
reallocate(pointer, sizeof(type) * (oldCount), 0)
void* reallocate(void* previous, size_t oldSize, size_t newSize);
void freeObjects();
#endif //CLOX_MEMORY_H