fix more GC bugs

This commit is contained in:
2023-08-29 23:21:52 -05:00
committed by cpunch
parent d41126e75f
commit 6a47c82179
10 changed files with 57 additions and 47 deletions

View File

@@ -12,28 +12,37 @@
#define ARRAY_START 8
#ifdef GC_DEBUG
# define cosmoM_freearray(state, type, buf, capacity) \
# define cosmoM_freeArray(state, type, buf, capacity) \
printf("freeing array %p [size %lu] at %s:%d\n", buf, sizeof(type) * capacity, __FILE__, \
__LINE__); \
cosmoM_reallocate(state, buf, sizeof(type) * capacity, 0)
cosmoM_reallocate(state, buf, sizeof(type) * capacity, 0, true)
#else
# define cosmoM_freearray(state, type, buf, capacity) \
cosmoM_reallocate(state, buf, sizeof(type) * capacity, 0)
# define cosmoM_freeArray(state, type, buf, capacity) \
cosmoM_reallocate(state, buf, sizeof(type) * capacity, 0, true)
#endif
#define cosmoM_growarray(state, type, buf, count, capacity) \
#define cosmoM_growArray(state, type, buf, count, capacity) \
if (count >= capacity || buf == NULL) { \
int old = capacity; \
capacity = old * GROW_FACTOR; \
buf = (type *)cosmoM_reallocate(state, buf, sizeof(type) * old, sizeof(type) * capacity); \
buf = (type *)cosmoM_reallocate(state, buf, sizeof(type) * old, sizeof(type) * capacity, \
true); \
}
#define cosmoM_growArrayNonGC(state, type, buf, count, capacity) \
if (count >= capacity || buf == NULL) { \
int old = capacity; \
capacity = old * GROW_FACTOR; \
buf = (type *)cosmoM_reallocate(state, buf, sizeof(type) * old, sizeof(type) * capacity, \
false); \
}
#ifdef GC_DEBUG
# define cosmoM_free(state, type, x) \
printf("freeing %p [size %lu] at %s:%d\n", x, sizeof(type), __FILE__, __LINE__); \
cosmoM_reallocate(state, x, sizeof(type), 0)
cosmoM_reallocate(state, x, sizeof(type), 0, true)
#else
# define cosmoM_free(state, type, x) cosmoM_reallocate(state, x, sizeof(type), 0)
# define cosmoM_free(state, type, x) cosmoM_reallocate(state, x, sizeof(type), 0, true)
#endif
#define cosmoM_isFrozen(state) (state->freezeGC > 0)
@@ -60,7 +69,8 @@
#endif
COSMO_API void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize);
COSMO_API void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize,
bool isGC);
COSMO_API bool cosmoM_checkGarbage(CState *state,
size_t needed); // returns true if GC event was triggered
COSMO_API void cosmoM_collectGarbage(CState *state);
@@ -76,10 +86,7 @@ COSMO_API void cosmoM_removeRoot(CState *state, CObj *oldRoot);
// wrapper for cosmoM_reallocate so we can track our memory usage
static inline void *cosmoM_xmalloc(CState *state, size_t sz)
{
return cosmoM_reallocate(state, NULL, 0, sz);
return cosmoM_reallocate(state, NULL, 0, sz, true);
}
// #define cosmoM_xmalloc(state, sz) \
// (printf("allocating new buffer at %s:%d of size %ld\n", __FILE__, __LINE__, sz), cosmoM_reallocate(state, NULL, 0, sz))
#endif