Initial garbage colletor implementation

This commit is contained in:
2025-08-30 21:21:02 -07:00
parent 80e3f1a916
commit d78cf29765
11 changed files with 709 additions and 214 deletions

View File

@ -9,14 +9,46 @@
/**
* Initial value of #refcount_global_allocator.
*/
static const RefcountAllocator default_allocator = {
static const RefcountAllocator DEFAULT_ALLOCATOR = {
.malloc = malloc,
.realloc = realloc,
.free = free,
.pass_data = false,
.user_data = NULL,
};
/**
* The global #RefcountAllocator used by other parts of RefCount. The default
* value just calls the C standard functions malloc, realloc, and free.
* value just calls the C standard functions malloc and free.
*/
const RefcountAllocator *refcount_global_allocator = &default_allocator;
const RefcountAllocator *refcount_global_allocator = &DEFAULT_ALLOCATOR;
/**
* HTAllocator malloc function that delegates to #refcount_malloc.
* @param size The number of bytes to allocate
* @parma alloc The #RefcountAllocator to use
* @return The result of #refcount_malloc
*/
static void *refcount_ht_malloc(size_t size, void *alloc) {
return refcount_malloc(alloc, size);
}
/**
* HTAllocator free function that delegates to #refcount_free.
* @param ptr The pointer to free
* @parma alloc The #RefcountAllocator to use
*/
static void refcount_ht_free(void *ptr, void *alloc) {
refcount_free(alloc, ptr);
}
/**
* Create a new HTAllocator that delegates to src.
* @param src The #RefcountAllocator to delegate to
* @param dest A pointer to a #HTAllocator to initialize
*/
void refcount_allocator_to_ht_allocator(const RefcountAllocator *src,
HTAllocator *dest) {
dest->malloc = refcount_ht_malloc;
dest->free = refcount_ht_free;
dest->user_data = (void *) src;
}