On Mon, Oct 12, 2020 at 08:25:05AM -0700, Jonathan Nieder wrote: > (+cc: Patrick Steinhardt from libgit2) > Hi, > > Han-Wen Nienhuys wrote[1]: > > On Fri, Oct 2, 2020 at 6:12 AM Jonathan Nieder wrote: > >> Han-Wen Nienhuys wrote: > > >>> + reftable_free(a); > >>> +} > >> > >> Are there other callers that need custom free? > > > > The libgit2 folks requested the ability to set memory allocation > > routines, hence reftable_free(). > > Thanks. Patrick or Han-Wen, can you say a little more about this use > case? That would help with making sure we are making an API that > meets its needs. > > For example, is a custom allocator something that would be set > globally or something attached to a handle? If the former, would code > that uses xmalloc and free and gets #define-d away when used in > libgit2 work? If the latter, what does the handle look like? We have global pluggable allocators in libgit2 which can be set up before calling `git_libgit2_init()`. The user of libgit2 can fill a `git_allocator` structure with a set of funtcion pointers, most importantly with implementations of `free` and `malloc`. Those then get used across all of libgit2 for all subsequent allocations. In order to be as thorough as possible, we thus also need to replace these function pointers for libgit2's dependencies. As registration of the allocator happens at runtime, we need to also be able to replace function pointers of dependencies at runtime. E.g. for OpenSSL, it provides an API `CRYPTO_set_mem_functions(malloc, realloc, free)` which we call on global initialization of the libgit2 library. So, to answer your questions: - The allocator is global and cannot be changed after initialization of libgit2. - It is pluggable, users can set up their own allocators by filling a structure with function pointers for `free`, `malloc`, `realloc` etc. - Due to the pluggable nature, we need to be able to set up those pointers at runtime. We can provide a set of static wrappers though which then call into the pluggable functions, so defines would probably work for us, too. I hope that answers all of your questions. Patrick