odb: provide a free function for custom backends #5005
Merged
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
git_odb_backend_malloc
name is a system function that is provided for custom ODB backends and allows them to allocate memory for an ODB object in the read callback. This is important so that libgit2 can later free the memory used by an ODB object that was read from the custom backend.(Simply taking data from the backend that it allocated means that we don't know how to free it, we could be using two different allocators if the library was built "normally" and the app in question has an instrumented allocator for debugging then libgit2 could not
free
the data allocated by the custom backend.)However, if an error occurs in the custom backend after the memory has been allocated for the custom object but before it's returned to libgit2, the custom backend has no way to free that memory and it must be leaked.
Provide a free function that corresponds to the alloc function so that custom backends have an opportunity to free memory before they return an error.
While creating this
free
function and searching for a name for it, I tweaked the name of the allocation function to avoid sounding like we allocated a backend instead of a bit of backend data. I created a proxy for safe deprecation, but kept the definition insys/odb_backend.h
to avoid clutteringdeprecated.h
with data about a function that is only included explicitly (by includingsys/odb_backend.h
).