Custom allocator
yrashk opened this issue · comments
Are you open to adding support for custom allocators? In my particular case, I am integrating libh2o with Postgres in the Omnigres project (https://omnigr.es) and while I do so far get away with partial reliance on the fact that libh2o will, in places, use standard allocator, this is generally becoming more difficult over time.
I wonder if you're open to adding something like this to memory.h:
#ifndef h2o_malloc
#define h2o_malloc(sz) malloc(sz)
#endif
#ifndef h2o_free
#define h2o_free(ptr) free(ptr)
#endif
#ifndef h2o_realloc
#define h2o_realloc(ptr, sz) realloc(ptr, sz)
#endif
and changing instances of malloc
/free
/realloc
to these helpers in the code?
This way, the default behavior remains the same but allows one to override the allocator when necessary.
I am happy to do the PR to facilitate this.
P.S. I am aware that some aspects of this may be a bit more hairy than this. There may be some interaction with OpenSSL that needs to be fixed for this to work, at the very least.
We already have h2o_mem_alloc
and h2o_mem_realloc
. Therefore, the question is if we should replace invocations of free
with that of a wrapper.
I am neutral to doing that, though, as you point out, we cannot modify our dependencies and there could be cases where we free memory allocated by a dependency (or vice versa). So I am afraid if we can provide code that would work with memory allocation functions that are more than simple wrappers around malloc / realloc / free.