-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Allow swapping out the memory allocation functions. #4574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow swapping out the memory allocation functions. #4574
Conversation
This is mainly useful for bindings like rugged, where we can swap these functions with the ones provided by Ruby. This helps Ruby to better track memory usage through libgit2, and allows it to trigger GC runs more intelligently.
@@ -82,28 +82,34 @@ | |||
|
|||
#else | |||
|
|||
void* (*git__malloc_func)(size_t size) = malloc; | |||
void* (*git__calloc_func)(size_t nitems, size_t size) = calloc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll want to have typedef
s for these, so we don't need to repeat them here and over in src/settings.c
.
Don’t we have a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be enough to figure out the perf impact once other comments are addressed. Before merging we do have to make sure that you can only set these functions once before you initialise the library. Otherwise we'll have allocated memory that we'll try to free with the wrong allocator
void* (*git__calloc_func)(size_t nitems, size_t size) = calloc; | ||
char* (*git__strdup_func)(const char *str1) = strdup; | ||
void* (*git__realloc_func)(void *ptr, size_t size) = realloc; | ||
void* (*git__free_func)(void *ptr) = free; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This returns void, not void pointer
char* (*git__strdup_func)(const char *str1) = strdup; | ||
void* (*git__realloc_func)(void *ptr, size_t size) = realloc; | ||
void* (*git__free_func)(void *ptr) = free; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these assignments need to be in a .c file. The header can then have an extern definition
Haha. Should've looked first if there was another PR already. Well, anyway. see #4576 for another implementation. |
I do like the approach of passing in a structure; it should be less error prone than a bunch of untyped varargs. |
#4576 also brings up that we have things like |
I'm closing this in favor of #4576. Thanks for your work? |
This is mainly useful for bindings like rugged, where we can swap these
functions with the ones provided by Ruby. This helps Ruby to better
track memory usage through libgit2, and allows it to trigger GC runs
more intelligently.
The implementation in this pull request uses the existing
git_libgit2_opts
infrastructure to allow changing the allocator functions.This is obviously missing documentation, as I want to first get some feedback whether
git_libgit2_opts
is the correct place for this or not. 😅I tested this from the rugged bindings, and things were working just fine, without any super obvious performance impact. 👍