Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74b7ddb

Browse files
committed
settings: allow swapping out memory allocator
Tie in the newly created infrastructure for swapping out memory allocators into our settings code. A user can now simply use the new option "GIT_OPT_SET_ALLOCATOR" with `git_libgit2_opts`, passing in an already initialized allocator structure as vararg.
1 parent 9865cd1 commit 74b7ddb

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

include/git2/common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ typedef enum {
183183
GIT_OPT_GET_WINDOWS_SHAREMODE,
184184
GIT_OPT_SET_WINDOWS_SHAREMODE,
185185
GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION,
186+
GIT_OPT_SET_ALLOCATOR
186187
} git_libgit2_opt_t;
187188

188189
/**
@@ -345,6 +346,12 @@ typedef enum {
345346
* > additional checksum calculation on each object. This defaults
346347
* > to enabled.
347348
*
349+
* opts(GIT_OPT_SET_ALLOCATOR, git_allocator *allocator)
350+
*
351+
* > Set the memory allocator to a different memory allocator. This
352+
* > allocator will then be used to make all memory allocations for
353+
* > libgit2 operations.
354+
*
348355
* @param option Option key
349356
* @param ... value to set the option
350357
* @return 0 on success, <0 on failure

include/git2/sys/alloc.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ typedef struct {
7272
void (*gfree)(void *ptr);
7373
} git_allocator;
7474

75+
/**
76+
* Initialize the allocator structure to use the `stdalloc` pointer.
77+
*
78+
* Set up the structure so that all of its members are using the standard
79+
* "stdalloc" allocator functions. The structure can then be used with
80+
* `git_allocator_setup`.
81+
*
82+
* @param allocator The allocator that is to be initialized.
83+
* @return An error code or 0.
84+
*/
85+
int git_stdalloc_init_allocator(git_allocator *allocator);
86+
87+
/**
88+
* Initialize the allocator structure to use the `crtdbg` pointer.
89+
*
90+
* Set up the structure so that all of its members are using the "crtdbg"
91+
* allocator functions. Note that this allocator is only available on Windows
92+
* platforms and only if libgit2 is being compiled with "-DMSVC_CRTDBG".
93+
*
94+
* @param allocator The allocator that is to be initialized.
95+
* @return An error code or 0.
96+
*/
97+
int git_win32_crtdbg_init_allocator(git_allocator *allocator);
98+
7599
GIT_END_DECL
76100

77101
#endif

src/alloc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ int git_allocator_setup(git_allocator *allocator)
2929
memcpy(&git__allocator, allocator, sizeof(*allocator));
3030
return 0;
3131
}
32+
33+
#if !defined(GIT_MSVC_CRTDBG)
34+
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
35+
{
36+
GIT_UNUSED(allocator);
37+
giterr_set(GIT_EINVALID, "crtdbg memory allocator not available");
38+
return -1;
39+
}
40+
#endif

src/settings.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#endif
1717

1818
#include <git2.h>
19+
#include "alloc.h"
1920
#include "sysdir.h"
2021
#include "cache.h"
2122
#include "global.h"
@@ -260,6 +261,10 @@ int git_libgit2_opts(int key, ...)
260261
git_odb__strict_hash_verification = (va_arg(ap, int) != 0);
261262
break;
262263

264+
case GIT_OPT_SET_ALLOCATOR:
265+
error = git_allocator_setup(va_arg(ap, git_allocator *));
266+
break;
267+
263268
default:
264269
giterr_set(GITERR_INVALID, "invalid option key");
265270
error = -1;

0 commit comments

Comments
 (0)