1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "alloc.h"
9 #include "runtime.h"
10
11 #include "allocators/failalloc.h"
12 #include "allocators/stdalloc.h"
13 #include "allocators/win32_leakcheck.h"
14
15 /* Fail any allocation until git_libgit2_init is called. */
16 git_allocator git__allocator = {
17 git_failalloc_malloc,
18 git_failalloc_calloc,
19 git_failalloc_strdup,
20 git_failalloc_strndup,
21 git_failalloc_substrdup,
22 git_failalloc_realloc,
23 git_failalloc_reallocarray,
24 git_failalloc_mallocarray,
25 git_failalloc_free
26 };
27
setup_default_allocator(void)28 static int setup_default_allocator(void)
29 {
30 #if defined(GIT_WIN32_LEAKCHECK)
31 return git_win32_leakcheck_init_allocator(&git__allocator);
32 #else
33 return git_stdalloc_init_allocator(&git__allocator);
34 #endif
35 }
36
git_allocator_global_init(void)37 int git_allocator_global_init(void)
38 {
39 /*
40 * We don't want to overwrite any allocator which has been set
41 * before the init function is called.
42 */
43 if (git__allocator.gmalloc != git_failalloc_malloc)
44 return 0;
45
46 return setup_default_allocator();
47 }
48
git_allocator_setup(git_allocator * allocator)49 int git_allocator_setup(git_allocator *allocator)
50 {
51 if (!allocator)
52 return setup_default_allocator();
53
54 memcpy(&git__allocator, allocator, sizeof(*allocator));
55 return 0;
56 }
57