Type

judo_memfunc

Memory manager.

Since v1.0
typedef void *(*judo_memfunc)(void *user_data, void *ptr, size_t size)

Discussion 🔗

This typedef defines the function signature for a custom memory management implementation. The memory manager is responsible for allocating and freeing memory. The implementation must behave like malloc or free depending upon its arguments.

When ptr is NULL, then the allocator must behave like malloc. When ptr is not NULL, then the allocator must behave like free. The value of size is either the number of bytes to allocate or the number of bytes being freed, depending on if the allocator is behaving like malloc or free.

The ud argument is a user data pointer passed through as-is from judo_parse and judo_free.

Examples 🔗

A minimal implementation that uses the C standard memory management routines might resemble the following code snippet.

void *memfunc(void *ud, void *ptr, size_t size) {
    if (ptr == NULL) {
        return malloc(size);
    } else {
        free(ptr);
        return NULL;
    }
}