Skip to content

Commit 48727e5

Browse files
committed
allocators: extract crtdbg allocator into its own file
The Windows-specific crtdbg allocator is currently mixed into the crtdbg stacktracing compilation unit, making it harder to find than necessary. Extract it and move it into the new "allocators/" subdirectory to improve discoverability. This change means that the crtdbg compilation unit is now compiled unconditionally, whereas it has previously only been compiled on Windows platforms. Thus we now have additional guards around the code so that it will only be compiled if GIT_MSVC_CRTDBG is defined. This also allows us to move over the fallback-implementation of `git_win32_crtdbg_init_allocator` into the same compilation unit.
1 parent b63396b commit 48727e5

File tree

5 files changed

+135
-109
lines changed

5 files changed

+135
-109
lines changed

src/alloc.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77

88
#include "alloc.h"
99

10-
#if defined(GIT_MSVC_CRTDBG)
11-
# include "win32/w32_crtdbg_stacktrace.h"
12-
#else
13-
# include "allocators/stdalloc.h"
14-
#endif
10+
#include "allocators/stdalloc.h"
11+
#include "allocators/win32_crtdbg.h"
1512

1613
git_allocator git__allocator;
1714

@@ -44,12 +41,3 @@ int git_allocator_setup(git_allocator *allocator)
4441
memcpy(&git__allocator, allocator, sizeof(*allocator));
4542
return 0;
4643
}
47-
48-
#if !defined(GIT_MSVC_CRTDBG)
49-
int 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

src/allocators/win32_crtdbg.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
80+
NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
81+
}
82+
83+
static void *crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
84+
{
85+
return crtdbg__reallocarray(NULL, nelem, elsize, file, line);
86+
}
87+
88+
static void crtdbg__free(void *ptr)
89+
{
90+
free(ptr);
91+
}
92+
93+
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
94+
{
95+
allocator->gmalloc = crtdbg__malloc;
96+
allocator->gcalloc = crtdbg__calloc;
97+
allocator->gstrdup = crtdbg__strdup;
98+
allocator->gstrndup = crtdbg__strndup;
99+
allocator->gsubstrdup = crtdbg__substrdup;
100+
allocator->grealloc = crtdbg__realloc;
101+
allocator->greallocarray = crtdbg__reallocarray;
102+
allocator->gmallocarray = crtdbg__mallocarray;
103+
allocator->gfree = crtdbg__free;
104+
return 0;
105+
}
106+
107+
#else
108+
109+
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
110+
{
111+
GIT_UNUSED(allocator);
112+
git_error_set(GIT_EINVALID, "crtdbg memory allocator not available");
113+
return -1;
114+
}
115+
116+
#endif

src/allocators/win32_crtdbg.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
#ifndef INCLUDE_allocators_crtdbg_h
9+
#define INCLUDE_allocators_crtdbg_h
10+
11+
#include "common.h"
12+
13+
#include "alloc.h"
14+
15+
int git_win32_crtdbg_init_allocator(git_allocator *allocator);
16+
17+
#endif

src/win32/w32_crtdbg_stacktrace.c

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -71,99 +71,6 @@ static bool g_limit_reached = false; /* had allocs after we filled row table */
7171
static unsigned int g_checkpoint_id = 0; /* to better label leak checkpoints */
7272
static bool g_transient_leaks_since_mark = false; /* payload for hook */
7373

74-
static void *crtdbg__malloc(size_t len, const char *file, int line)
75-
{
76-
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
77-
if (!ptr) git_error_set_oom();
78-
return ptr;
79-
}
80-
81-
static void *crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
82-
{
83-
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
84-
if (!ptr) git_error_set_oom();
85-
return ptr;
86-
}
87-
88-
static char *crtdbg__strdup(const char *str, const char *file, int line)
89-
{
90-
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
91-
if (!ptr) git_error_set_oom();
92-
return ptr;
93-
}
94-
95-
static char *crtdbg__strndup(const char *str, size_t n, const char *file, int line)
96-
{
97-
size_t length = 0, alloclength;
98-
char *ptr;
99-
100-
length = p_strnlen(str, n);
101-
102-
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
103-
!(ptr = crtdbg__malloc(alloclength, file, line)))
104-
return NULL;
105-
106-
if (length)
107-
memcpy(ptr, str, length);
108-
109-
ptr[length] = '\0';
110-
111-
return ptr;
112-
}
113-
114-
static char *crtdbg__substrdup(const char *start, size_t n, const char *file, int line)
115-
{
116-
char *ptr;
117-
size_t alloclen;
118-
119-
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
120-
!(ptr = crtdbg__malloc(alloclen, file, line)))
121-
return NULL;
122-
123-
memcpy(ptr, start, n);
124-
ptr[n] = '\0';
125-
return ptr;
126-
}
127-
128-
static void *crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
129-
{
130-
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
131-
if (!new_ptr) git_error_set_oom();
132-
return new_ptr;
133-
}
134-
135-
static void *crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
136-
{
137-
size_t newsize;
138-
139-
return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
140-
NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
141-
}
142-
143-
static void *crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
144-
{
145-
return crtdbg__reallocarray(NULL, nelem, elsize, file, line);
146-
}
147-
148-
static void crtdbg__free(void *ptr)
149-
{
150-
free(ptr);
151-
}
152-
153-
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
154-
{
155-
allocator->gmalloc = crtdbg__malloc;
156-
allocator->gcalloc = crtdbg__calloc;
157-
allocator->gstrdup = crtdbg__strdup;
158-
allocator->gstrndup = crtdbg__strndup;
159-
allocator->gsubstrdup = crtdbg__substrdup;
160-
allocator->grealloc = crtdbg__realloc;
161-
allocator->greallocarray = crtdbg__reallocarray;
162-
allocator->gmallocarray = crtdbg__mallocarray;
163-
allocator->gfree = crtdbg__free;
164-
return 0;
165-
}
166-
16774
/**
16875
* Compare function for bsearch on g_cs_index table.
16976
*/

src/win32/w32_crtdbg_stacktrace.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
* startup. See tests/main.c for an example.
4444
*/
4545

46-
int git_win32_crtdbg_init_allocator(git_allocator *allocator);
47-
4846
/**
4947
* Initialize our memory leak tracking and de-dup data structures.
5048
* This should ONLY be called by git_libgit2_init().

0 commit comments

Comments
 (0)