Cosmo/src/cmem.h

86 lines
3.9 KiB
C
Raw Normal View History

2020-10-28 05:16:30 +00:00
#ifndef CMEME_C
#define CMEME_C // meme lol
#include "cosmo.h"
#include "cstate.h"
2023-02-09 18:32:48 +00:00
// #define GC_STRESS
// #define GC_DEBUG
// arrays *must* grow by a factor of 2
#define GROW_FACTOR 2
2020-10-28 05:16:30 +00:00
#define HEAP_GROW_FACTOR 2
2023-02-09 18:32:48 +00:00
#define ARRAY_START 8
2020-10-28 05:16:30 +00:00
#ifdef GC_DEBUG
2023-02-09 18:32:48 +00:00
# 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)
#else
2023-02-09 18:32:48 +00:00
# define cosmoM_freearray(state, type, buf, capacity) \
cosmoM_reallocate(state, buf, sizeof(type) * capacity, 0)
#endif
2020-10-28 05:16:30 +00:00
2023-02-09 18:32:48 +00:00
#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); \
2020-10-28 05:16:30 +00:00
}
#ifdef GC_DEBUG
2023-02-09 18:32:48 +00:00
# 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)
#else
2023-02-09 18:32:48 +00:00
# define cosmoM_free(state, type, x) cosmoM_reallocate(state, x, sizeof(type), 0)
#endif
2020-10-28 05:16:30 +00:00
2023-02-09 18:32:48 +00:00
#define cosmoM_isFrozen(state) (state->freezeGC > 0)
2020-10-28 05:16:30 +00:00
// if debugging, print the locations of when the state is frozen/unfrozen
#ifdef GC_DEBUG
2023-02-09 18:32:48 +00:00
# define cosmoM_freezeGC(state) \
state->freezeGC++; \
printf("freezing state at %s:%d [%d]\n", __FILE__, __LINE__, state->freezeGC)
# define cosmoM_unfreezeGC(state) \
state->freezeGC--; \
printf("unfreezing state at %s:%d [%d]\n", __FILE__, __LINE__, state->freezeGC); \
cosmoM_checkGarbage(state, 0)
#else
// freeze's the garbage collector until cosmoM_unfreezeGC is called
2023-02-09 18:32:48 +00:00
# define cosmoM_freezeGC(state) state->freezeGC++
2020-10-28 05:16:30 +00:00
// unfreeze's the garbage collector and tries to run a garbage collection cycle
2023-02-09 18:32:48 +00:00
# define cosmoM_unfreezeGC(state) \
state->freezeGC--; \
cosmoM_checkGarbage(state, 0)
2023-02-09 18:32:48 +00:00
#endif
2020-10-28 05:16:30 +00:00
2020-11-17 09:10:55 +00:00
COSMO_API void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize);
2023-02-09 18:32:48 +00:00
COSMO_API bool cosmoM_checkGarbage(CState *state,
size_t needed); // returns true if GC event was triggered
2020-11-17 09:10:55 +00:00
COSMO_API void cosmoM_collectGarbage(CState *state);
COSMO_API void cosmoM_updateThreshhold(CState *state);
2020-10-28 05:16:30 +00:00
// lets the VM know you are holding a reference to a CObj and to not free it
2023-06-03 06:39:35 +00:00
// NOTE: prefer to use the stack when possible
COSMO_API void cosmoM_addRoot(CState *state, CObj *newRoot);
2020-12-19 19:32:43 +00:00
// lets the VM know this root is no longer held in a reference and is able to be freed
COSMO_API void cosmoM_removeRoot(CState *state, CObj *oldRoot);
2023-08-26 00:57:16 +00:00
// wrapper for cosmoM_reallocate so we can track our memory usage
2023-02-09 18:32:48 +00:00
static inline void *cosmoM_xmalloc(CState *state, size_t sz)
{
2020-10-28 05:16:30 +00:00
return cosmoM_reallocate(state, NULL, 0, sz);
}
2023-08-26 00:57:16 +00:00
// #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))
2021-01-02 05:06:24 +00:00
#endif