Skip to content

Commit c559485

Browse files
authored
Merge pull request libgit2#4998 from pks-t/pks/allocator-restructuring
Allocator restructuring
2 parents 3aa8401 + 765ff6e commit c559485

File tree

8 files changed

+142
-113
lines changed

8 files changed

+142
-113
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ ELSE()
428428
FILE(GLOB SRC_OS unix/*.c unix/*.h)
429429
ENDIF()
430430
FILE(GLOB SRC_GIT2 *.c *.h
431+
allocators/*.c allocators/*.h
431432
streams/*.c streams/*.h
432433
transports/*.c transports/*.h
433434
xdiff/*.c xdiff/*.h)

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 "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/stdalloc.h renamed to src/allocators/stdalloc.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
77

8-
#ifndef INCLUDE_stdalloc_h__
9-
#define INCLUDE_stdalloc_h__
10-
11-
#include "alloc.h"
8+
#ifndef INCLUDE_allocators_stdalloc_h__
9+
#define INCLUDE_allocators_stdalloc_h__
1210

1311
#include "common.h"
1412

13+
#include "alloc.h"
14+
1515
int git_stdalloc_init_allocator(git_allocator *allocator);
1616

1717
#endif

src/allocators/win32_crtdbg.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
112+
{
113+
GIT_UNUSED(allocator);
114+
git_error_set(GIT_EINVALID, "crtdbg memory allocator not available");
115+
return -1;
116+
}
117+
118+
#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)