55 lines
1.5 KiB
C
55 lines
1.5 KiB
C
/**
|
|
* @file
|
|
* Replaceable memory allocator.
|
|
*/
|
|
#include "refcount/allocator.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
/**
|
|
* Initial value of #refcount_global_allocator.
|
|
*/
|
|
static const RefcountAllocator DEFAULT_ALLOCATOR = {
|
|
.malloc.no_data = malloc,
|
|
.free.no_data = 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 and free.
|
|
*/
|
|
const RefcountAllocator *refcount_global_allocator = &DEFAULT_ALLOCATOR;
|
|
|
|
/**
|
|
* HTAllocator malloc function that delegates to #refcount_malloc.
|
|
* @param size The number of bytes to allocate
|
|
* @param 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
|
|
* @param 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;
|
|
}
|