diff options
Diffstat (limited to 'inc/gc.h')
-rw-r--r-- | inc/gc.h | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/inc/gc.h b/inc/gc.h new file mode 100644 index 0000000..0649559 --- /dev/null +++ b/inc/gc.h @@ -0,0 +1,84 @@ +/* + * gc - A simple mark and sweep garbage collector for C. + */ + +#ifndef __GC_H__ +#define __GC_H__ + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> + +#define LOGLEVEL LOGLEVEL_DEBUG + +enum { + LOGLEVEL_CRITICAL, + LOGLEVEL_WARNING, + LOGLEVEL_INFO, + LOGLEVEL_DEBUG, + LOGLEVEL_NONE +}; + +extern const char *log_level_strings[]; + +#define log(level, fmt, ...) \ + do { \ + if (level <= LOGLEVEL) \ + fprintf(stderr, "[%s] %s:%s:%d: " fmt "\n", \ + log_level_strings[level], __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define LOG_CRITICAL(fmt, ...) log(LOGLEVEL_CRITICAL, fmt, __VA_ARGS__) +#define LOG_WARNING(fmt, ...) log(LOGLEVEL_WARNING, fmt, __VA_ARGS__) +#define LOG_INFO(fmt, ...) log(LOGLEVEL_INFO, fmt, __VA_ARGS__) +#define LOG_DEBUG(fmt, ...) log(LOGLEVEL_DEBUG, fmt, __VA_ARGS__) + +struct AllocationMap; + +typedef struct GarbageCollector { + struct AllocationMap *allocs; // allocation map + bool paused; // (temporarily) switch gc on/off + void *bos; // bottom of stack + size_t min_size; +} GarbageCollector; + +extern GarbageCollector gc; // Global garbage collector for all + // single-threaded applications + +/* + * Starting, stopping, pausing, resuming and running the GC. + */ +void gc_start(GarbageCollector *gc, void *bos); +void gc_start_ext(GarbageCollector *gc, void *bos, size_t initial_size, + size_t min_size, double downsize_load_factor, + double upsize_load_factor, double sweep_factor); +size_t gc_stop(GarbageCollector *gc); +void gc_pause(GarbageCollector *gc); +void gc_resume(GarbageCollector *gc); +size_t gc_run(GarbageCollector *gc); + +/* + * Allocating and deallocating memory. + */ +void *gc_malloc(GarbageCollector *gc, size_t size); +void *gc_malloc_static(GarbageCollector *gc, size_t size, void (*dtor)(void *)); +void *gc_malloc_ext(GarbageCollector *gc, size_t size, void (*dtor)(void *)); +void *gc_calloc(GarbageCollector *gc, size_t count, size_t size); +void *gc_calloc_ext(GarbageCollector *gc, size_t count, size_t size, + void (*dtor)(void *)); +void *gc_realloc(GarbageCollector *gc, void *ptr, size_t size); +void gc_free(GarbageCollector *gc, void *ptr); + +/* + * Lifecycle management + */ +void *gc_make_static(GarbageCollector *gc, void *ptr); + +/* + * Helper functions and stdlib replacements. + */ +char *gc_strdup(GarbageCollector *gc, const char *s); + +#endif /* !__GC_H__ */ |