Expose a malloc function to 3rd party ODB backends #900
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The ODB backend interface has functions in it for doing reads. These functions allocate buffers and return them (and ownership of them) to the caller. It is expected right now that the ODB backend allocates these buffers using
git__malloc
. Since this function is not exported, it's not possible to write a third-party ODB backend for libgit2. All backends must be in the git2 library so that they can callgit__malloc
.This change asks that backends use a new exported function called
git_odb_backend_malloc
to allocate buffers that will change ownership from the backend to libgit2. The fact that this entry point just redirects togit__malloc
right now is an implementation detail.This implementation detail means that the existing backends don't have to be switched over to using
git_odb_backend_malloc
(although they could be). It also means that the caller (who takes ownership of the buffer) can still assume thatgit__free
is the function to be used to free any buffer that comes back from an ODB backend.The implementation could be changed to have a separate heap for
git_odb_backend_malloc
allocations. Doing so would mean that libgit2 would need to keep track of thefree
function for each buffer so that it can call the right function at free time.This implementation detail does expose
git__malloc
to any outside caller. But anyone using it for a purpose other than allocating a buffer to return from read/read_prefix in libgit2 is in violation of the contract and subject to being broken by the implementation details described above changing.There are certainly other ways to do this. If this isn't what the team feels like is the best approach then I would be happy to revisit it.
Thanks,
Philip
Microsoft Corporation