]> git.proxmox.com Git - libgit2.git/blame - src/alloc.c
New upstream version 0.28.1+dfsg.1
[libgit2.git] / src / alloc.c
CommitLineData
ac3d33df
JK
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
10#if defined(GIT_MSVC_CRTDBG)
11# include "win32/w32_crtdbg_stacktrace.h"
12#else
13# include "stdalloc.h"
14#endif
15
16git_allocator git__allocator;
17
18static int setup_default_allocator(void)
19{
20#if defined(GIT_MSVC_CRTDBG)
21 return git_win32_crtdbg_init_allocator(&git__allocator);
22#else
23 return git_stdalloc_init_allocator(&git__allocator);
24#endif
25}
26
27int git_allocator_global_init(void)
28{
29 /*
30 * We don't want to overwrite any allocator which has been set before
31 * the init function is called.
32 */
33 if (git__allocator.gmalloc != NULL)
34 return 0;
35
36 return setup_default_allocator();
37}
38
39int git_allocator_setup(git_allocator *allocator)
40{
41 if (!allocator)
42 return setup_default_allocator();
43
44 memcpy(&git__allocator, allocator, sizeof(*allocator));
45 return 0;
46}
47
48#if !defined(GIT_MSVC_CRTDBG)
49int git_win32_crtdbg_init_allocator(git_allocator *allocator)
50{
51 GIT_UNUSED(allocator);
52 git_error_set(GIT_EINVALID, "crtdbg memory allocator not available");
53 return -1;
54}
55#endif