]>
git.proxmox.com Git - libgit2.git/blob - src/allocators/win32_leakcheck.c
fe06a14af738023c0a8b7c7fc7b32ab6e36c8a5c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
8 #include "win32_leakcheck.h"
10 #if defined(GIT_WIN32_LEAKCHECK)
12 #include "win32/w32_leakcheck.h"
14 static void *leakcheck_malloc(size_t len
, const char *file
, int line
)
16 void *ptr
= _malloc_dbg(len
, _NORMAL_BLOCK
, git_win32_leakcheck_stacktrace(1,file
), line
);
17 if (!ptr
) git_error_set_oom();
21 static void *leakcheck_calloc(size_t nelem
, size_t elsize
, const char *file
, int line
)
23 void *ptr
= _calloc_dbg(nelem
, elsize
, _NORMAL_BLOCK
, git_win32_leakcheck_stacktrace(1,file
), line
);
24 if (!ptr
) git_error_set_oom();
28 static char *leakcheck_strdup(const char *str
, const char *file
, int line
)
30 char *ptr
= _strdup_dbg(str
, _NORMAL_BLOCK
, git_win32_leakcheck_stacktrace(1,file
), line
);
31 if (!ptr
) git_error_set_oom();
35 static char *leakcheck_strndup(const char *str
, size_t n
, const char *file
, int line
)
37 size_t length
= 0, alloclength
;
40 length
= p_strnlen(str
, n
);
42 if (GIT_ADD_SIZET_OVERFLOW(&alloclength
, length
, 1) ||
43 !(ptr
= leakcheck_malloc(alloclength
, file
, line
)))
47 memcpy(ptr
, str
, length
);
54 static char *leakcheck_substrdup(const char *start
, size_t n
, const char *file
, int line
)
59 if (GIT_ADD_SIZET_OVERFLOW(&alloclen
, n
, 1) ||
60 !(ptr
= leakcheck_malloc(alloclen
, file
, line
)))
63 memcpy(ptr
, start
, n
);
68 static void *leakcheck_realloc(void *ptr
, size_t size
, const char *file
, int line
)
70 void *new_ptr
= _realloc_dbg(ptr
, size
, _NORMAL_BLOCK
, git_win32_leakcheck_stacktrace(1,file
), line
);
71 if (!new_ptr
) git_error_set_oom();
75 static void *leakcheck_reallocarray(void *ptr
, size_t nelem
, size_t elsize
, const char *file
, int line
)
79 if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize
, nelem
, elsize
))
82 return leakcheck_realloc(ptr
, newsize
, file
, line
);
85 static void *leakcheck_mallocarray(size_t nelem
, size_t elsize
, const char *file
, int line
)
87 return leakcheck_reallocarray(NULL
, nelem
, elsize
, file
, line
);
90 static void leakcheck_free(void *ptr
)
95 int git_win32_leakcheck_init_allocator(git_allocator
*allocator
)
97 allocator
->gmalloc
= leakcheck_malloc
;
98 allocator
->gcalloc
= leakcheck_calloc
;
99 allocator
->gstrdup
= leakcheck_strdup
;
100 allocator
->gstrndup
= leakcheck_strndup
;
101 allocator
->gsubstrdup
= leakcheck_substrdup
;
102 allocator
->grealloc
= leakcheck_realloc
;
103 allocator
->greallocarray
= leakcheck_reallocarray
;
104 allocator
->gmallocarray
= leakcheck_mallocarray
;
105 allocator
->gfree
= leakcheck_free
;
111 int git_win32_leakcheck_init_allocator(git_allocator
*allocator
)
113 GIT_UNUSED(allocator
);
114 git_error_set(GIT_EINVALID
, "leakcheck memory allocator not available");