source src/allocators/win32_crtdbg.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
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 "win32_crtdbg.h" | ||
9 | - | |||
10 | - | #if defined(GIT_MSVC_CRTDBG) | ||
11 | - | |||
12 | - | #include "win32/w32_crtdbg_stacktrace.h" | ||
13 | - | |||
14 | - | static void *crtdbg__malloc(size_t len, const char *file, int line) | ||
15 | - | { | ||
16 | - | void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line); | ||
17 | - | if (!ptr) git_error_set_oom(); | ||
18 | - | return ptr; | ||
19 | - | } | ||
20 | - | |||
21 | - | static void *crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line) | ||
22 | - | { | ||
23 | - | void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line); | ||
24 | - | if (!ptr) git_error_set_oom(); | ||
25 | - | return ptr; | ||
26 | - | } | ||
27 | - | |||
28 | - | static char *crtdbg__strdup(const char *str, const char *file, int line) | ||
29 | - | { | ||
30 | - | char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line); | ||
31 | - | if (!ptr) git_error_set_oom(); | ||
32 | - | return ptr; | ||
33 | - | } | ||
34 | - | |||
35 | - | static char *crtdbg__strndup(const char *str, size_t n, const char *file, int line) | ||
36 | - | { | ||
37 | - | size_t length = 0, alloclength; | ||
38 | - | char *ptr; | ||
39 | - | |||
40 | - | length = p_strnlen(str, n); | ||
41 | - | |||
42 | - | if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || | ||
43 | - | !(ptr = crtdbg__malloc(alloclength, file, line))) | ||
44 | - | return NULL; | ||
45 | - | |||
46 | - | if (length) | ||
47 | - | memcpy(ptr, str, length); | ||
48 | - | |||
49 | - | ptr[length] = '\0'; | ||
50 | - | |||
51 | - | return ptr; | ||
52 | - | } | ||
53 | - | |||
54 | - | static char *crtdbg__substrdup(const char *start, size_t n, const char *file, int line) | ||
55 | - | { | ||
56 | - | char *ptr; | ||
57 | - | size_t alloclen; | ||
58 | - | |||
59 | - | if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) || | ||
60 | - | !(ptr = crtdbg__malloc(alloclen, file, line))) | ||
61 | - | return NULL; | ||
62 | - | |||
63 | - | memcpy(ptr, start, n); | ||
64 | - | ptr[n] = '\0'; | ||
65 | - | return ptr; | ||
66 | - | } | ||
67 | - | |||
68 | - | static void *crtdbg__realloc(void *ptr, size_t size, const char *file, int line) | ||
69 | - | { | ||
70 | - | void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line); | ||
71 | - | if (!new_ptr) git_error_set_oom(); | ||
72 | - | return new_ptr; | ||
73 | - | } | ||
74 | - | |||
75 | - | static void *crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) | ||
76 | - | { | ||
77 | - | size_t newsize; | ||
78 | - | |||
79 | - | if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) | ||
80 | - | return NULL; | ||
81 | - | |||
82 | - | return crtdbg__realloc(ptr, newsize, file, line); | ||
83 | - | } | ||
84 | - | |||
85 | - | static void *crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line) | ||
86 | - | { | ||
87 | - | return crtdbg__reallocarray(NULL, nelem, elsize, file, line); | ||
88 | - | } | ||
89 | - | |||
90 | - | static void crtdbg__free(void *ptr) | ||
91 | - | { | ||
92 | - | free(ptr); | ||
93 | - | } | ||
94 | - | |||
95 | - | int git_win32_crtdbg_init_allocator(git_allocator *allocator) | ||
96 | - | { | ||
97 | - | allocator->gmalloc = crtdbg__malloc; | ||
98 | - | allocator->gcalloc = crtdbg__calloc; | ||
99 | - | allocator->gstrdup = crtdbg__strdup; | ||
100 | - | allocator->gstrndup = crtdbg__strndup; | ||
101 | - | allocator->gsubstrdup = crtdbg__substrdup; | ||
102 | - | allocator->grealloc = crtdbg__realloc; | ||
103 | - | allocator->greallocarray = crtdbg__reallocarray; | ||
104 | - | allocator->gmallocarray = crtdbg__mallocarray; | ||
105 | - | allocator->gfree = crtdbg__free; | ||
106 | - | return 0; | ||
107 | - | } | ||
108 | - | |||
109 | - | #else | ||
110 | - | |||
111 | ##### | 2 | int git_win32_crtdbg_init_allocator(git_allocator *allocator) | |
112 | - | { | ||
113 | - | GIT_UNUSED(allocator); | ||
114 | ##### | 2 | git_error_set(GIT_EINVALID, "crtdbg memory allocator not available"); | |
115 | ##### | 3 | return -1; | |
116 | - | } | ||
117 | - | |||
118 | - | #endif |