Commit 305e801a by Edward Thomson

util: allow callers to reset custom allocators

Provide a utility to reset custom allocators back to their default.
This is particularly useful for testing.
parent 671b2446
...@@ -364,7 +364,8 @@ typedef enum { ...@@ -364,7 +364,8 @@ typedef enum {
* *
* > Set the memory allocator to a different memory allocator. This * > Set the memory allocator to a different memory allocator. This
* > allocator will then be used to make all memory allocations for * > allocator will then be used to make all memory allocations for
* > libgit2 operations. * > libgit2 operations. If the given `allocator` is NULL, then the
* > system default will be restored.
* *
* opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, int enabled) * opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, int enabled)
* *
......
...@@ -15,6 +15,15 @@ ...@@ -15,6 +15,15 @@
git_allocator git__allocator; git_allocator git__allocator;
static int setup_default_allocator(void)
{
#if defined(GIT_MSVC_CRTDBG)
return git_win32_crtdbg_init_allocator(&git__allocator);
#else
return git_stdalloc_init_allocator(&git__allocator);
#endif
}
int git_allocator_global_init(void) int git_allocator_global_init(void)
{ {
/* /*
...@@ -24,15 +33,14 @@ int git_allocator_global_init(void) ...@@ -24,15 +33,14 @@ int git_allocator_global_init(void)
if (git__allocator.gmalloc != NULL) if (git__allocator.gmalloc != NULL)
return 0; return 0;
#if defined(GIT_MSVC_CRTDBG) return setup_default_allocator();
return git_win32_crtdbg_init_allocator(&git__allocator);
#else
return git_stdalloc_init_allocator(&git__allocator);
#endif
} }
int git_allocator_setup(git_allocator *allocator) int git_allocator_setup(git_allocator *allocator)
{ {
if (!allocator)
return setup_default_allocator();
memcpy(&git__allocator, allocator, sizeof(*allocator)); memcpy(&git__allocator, allocator, sizeof(*allocator));
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment