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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
diff --git a/include/git2/common.h b/include/git2/common.h
index f65cfdd..f095e59 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -183,6 +183,7 @@ typedef enum {
GIT_OPT_GET_WINDOWS_SHAREMODE,
GIT_OPT_SET_WINDOWS_SHAREMODE,
GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION,
+ GIT_OPT_SET_ALLOCATOR
} git_libgit2_opt_t;
/**
@@ -345,6 +346,12 @@ typedef enum {
* > additional checksum calculation on each object. This defaults
* > to enabled.
*
+ * opts(GIT_OPT_SET_ALLOCATOR, git_allocator *allocator)
+ *
+ * > Set the memory allocator to a different memory allocator. This
+ * > allocator will then be used to make all memory allocations for
+ * > libgit2 operations.
+ *
* @param option Option key
* @param ... value to set the option
* @return 0 on success, <0 on failure
diff --git a/include/git2/sys/alloc.h b/include/git2/sys/alloc.h
index a0955ce..4bc5323 100644
--- a/include/git2/sys/alloc.h
+++ b/include/git2/sys/alloc.h
@@ -72,6 +72,30 @@ typedef struct {
void (*gfree)(void *ptr);
} git_allocator;
+/**
+ * Initialize the allocator structure to use the `stdalloc` pointer.
+ *
+ * Set up the structure so that all of its members are using the standard
+ * "stdalloc" allocator functions. The structure can then be used with
+ * `git_allocator_setup`.
+ *
+ * @param allocator The allocator that is to be initialized.
+ * @return An error code or 0.
+ */
+int git_stdalloc_init_allocator(git_allocator *allocator);
+
+/**
+ * Initialize the allocator structure to use the `crtdbg` pointer.
+ *
+ * Set up the structure so that all of its members are using the "crtdbg"
+ * allocator functions. Note that this allocator is only available on Windows
+ * platforms and only if libgit2 is being compiled with "-DMSVC_CRTDBG".
+ *
+ * @param allocator The allocator that is to be initialized.
+ * @return An error code or 0.
+ */
+int git_win32_crtdbg_init_allocator(git_allocator *allocator);
+
GIT_END_DECL
#endif
diff --git a/src/alloc.c b/src/alloc.c
index 79ed1dd..35fdd00 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -29,3 +29,12 @@ int git_allocator_setup(git_allocator *allocator)
memcpy(&git__allocator, allocator, sizeof(*allocator));
return 0;
}
+
+#if !defined(GIT_MSVC_CRTDBG)
+int git_win32_crtdbg_init_allocator(git_allocator *allocator)
+{
+ GIT_UNUSED(allocator);
+ giterr_set(GIT_EINVALID, "crtdbg memory allocator not available");
+ return -1;
+}
+#endif
diff --git a/src/settings.c b/src/settings.c
index f6bc5b2..14280f8 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -16,6 +16,7 @@
#endif
#include <git2.h>
+#include "alloc.h"
#include "sysdir.h"
#include "cache.h"
#include "global.h"
@@ -260,6 +261,10 @@ int git_libgit2_opts(int key, ...)
git_odb__strict_hash_verification = (va_arg(ap, int) != 0);
break;
+ case GIT_OPT_SET_ALLOCATOR:
+ error = git_allocator_setup(va_arg(ap, git_allocator *));
+ break;
+
default:
giterr_set(GITERR_INVALID, "invalid option key");
error = -1;