Commit b63396b75fe26afa3b27259c0d13dd7bc13b53ba

Patrick Steinhardt 2019-02-21T12:13:59

allocators: move standard allocator into subdirectory Right now, our two allocator implementations are scattered around the tree in "stdalloc.h" and "win32/w32_crtdbg_stacktrace.h". Start grouping them together in a single directory "allocators/", similar to how e.g. our streams are organized.

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 5ad8b14..b056c9a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -428,6 +428,7 @@ ELSE()
 	FILE(GLOB SRC_OS unix/*.c unix/*.h)
 ENDIF()
 FILE(GLOB SRC_GIT2 *.c *.h
+	allocators/*.c allocators/*.h
 	streams/*.c streams/*.h
 	transports/*.c transports/*.h
 	xdiff/*.c xdiff/*.h)
diff --git a/src/alloc.c b/src/alloc.c
index 8d4a194..5ff4151 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -10,7 +10,7 @@
 #if defined(GIT_MSVC_CRTDBG)
 # include "win32/w32_crtdbg_stacktrace.h"
 #else
-# include "stdalloc.h"
+# include "allocators/stdalloc.h"
 #endif
 
 git_allocator git__allocator;
diff --git a/src/allocators/stdalloc.c b/src/allocators/stdalloc.c
new file mode 100644
index 0000000..c4938e3
--- /dev/null
+++ b/src/allocators/stdalloc.c
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "stdalloc.h"
+
+static void *stdalloc__malloc(size_t len, const char *file, int line)
+{
+	void *ptr = malloc(len);
+
+	GIT_UNUSED(file);
+	GIT_UNUSED(line);
+
+	if (!ptr) git_error_set_oom();
+	return ptr;
+}
+
+static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line)
+{
+	void *ptr = calloc(nelem, elsize);
+
+	GIT_UNUSED(file);
+	GIT_UNUSED(line);
+
+	if (!ptr) git_error_set_oom();
+	return ptr;
+}
+
+static char *stdalloc__strdup(const char *str, const char *file, int line)
+{
+	char *ptr = strdup(str);
+
+	GIT_UNUSED(file);
+	GIT_UNUSED(line);
+
+	if (!ptr) git_error_set_oom();
+	return ptr;
+}
+
+static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line)
+{
+	size_t length = 0, alloclength;
+	char *ptr;
+
+	length = p_strnlen(str, n);
+
+	if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
+		!(ptr = stdalloc__malloc(alloclength, file, line)))
+		return NULL;
+
+	if (length)
+		memcpy(ptr, str, length);
+
+	ptr[length] = '\0';
+
+	return ptr;
+}
+
+static char *stdalloc__substrdup(const char *start, size_t n, const char *file, int line)
+{
+	char *ptr;
+	size_t alloclen;
+
+	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
+		!(ptr = stdalloc__malloc(alloclen, file, line)))
+		return NULL;
+
+	memcpy(ptr, start, n);
+	ptr[n] = '\0';
+	return ptr;
+}
+
+static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
+{
+	void *new_ptr = realloc(ptr, size);
+
+	GIT_UNUSED(file);
+	GIT_UNUSED(line);
+
+	if (!new_ptr) git_error_set_oom();
+	return new_ptr;
+}
+
+static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
+{
+	size_t newsize;
+
+	if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize))
+		return NULL;
+
+	return stdalloc__realloc(ptr, newsize, file, line);
+}
+
+static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
+{
+	return stdalloc__reallocarray(NULL, nelem, elsize, file, line);
+}
+
+static void stdalloc__free(void *ptr)
+{
+	free(ptr);
+}
+
+int git_stdalloc_init_allocator(git_allocator *allocator)
+{
+	allocator->gmalloc = stdalloc__malloc;
+	allocator->gcalloc = stdalloc__calloc;
+	allocator->gstrdup = stdalloc__strdup;
+	allocator->gstrndup = stdalloc__strndup;
+	allocator->gsubstrdup = stdalloc__substrdup;
+	allocator->grealloc = stdalloc__realloc;
+	allocator->greallocarray = stdalloc__reallocarray;
+	allocator->gmallocarray = stdalloc__mallocarray;
+	allocator->gfree = stdalloc__free;
+	return 0;
+}
diff --git a/src/allocators/stdalloc.h b/src/allocators/stdalloc.h
new file mode 100644
index 0000000..fa23fe6
--- /dev/null
+++ b/src/allocators/stdalloc.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_allocators_stdalloc_h__
+#define INCLUDE_allocators_stdalloc_h__
+
+#include "common.h"
+
+#include "alloc.h"
+
+int git_stdalloc_init_allocator(git_allocator *allocator);
+
+#endif
diff --git a/src/stdalloc.c b/src/stdalloc.c
deleted file mode 100644
index c4938e3..0000000
--- a/src/stdalloc.c
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-
-#include "stdalloc.h"
-
-static void *stdalloc__malloc(size_t len, const char *file, int line)
-{
-	void *ptr = malloc(len);
-
-	GIT_UNUSED(file);
-	GIT_UNUSED(line);
-
-	if (!ptr) git_error_set_oom();
-	return ptr;
-}
-
-static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line)
-{
-	void *ptr = calloc(nelem, elsize);
-
-	GIT_UNUSED(file);
-	GIT_UNUSED(line);
-
-	if (!ptr) git_error_set_oom();
-	return ptr;
-}
-
-static char *stdalloc__strdup(const char *str, const char *file, int line)
-{
-	char *ptr = strdup(str);
-
-	GIT_UNUSED(file);
-	GIT_UNUSED(line);
-
-	if (!ptr) git_error_set_oom();
-	return ptr;
-}
-
-static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line)
-{
-	size_t length = 0, alloclength;
-	char *ptr;
-
-	length = p_strnlen(str, n);
-
-	if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
-		!(ptr = stdalloc__malloc(alloclength, file, line)))
-		return NULL;
-
-	if (length)
-		memcpy(ptr, str, length);
-
-	ptr[length] = '\0';
-
-	return ptr;
-}
-
-static char *stdalloc__substrdup(const char *start, size_t n, const char *file, int line)
-{
-	char *ptr;
-	size_t alloclen;
-
-	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
-		!(ptr = stdalloc__malloc(alloclen, file, line)))
-		return NULL;
-
-	memcpy(ptr, start, n);
-	ptr[n] = '\0';
-	return ptr;
-}
-
-static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
-{
-	void *new_ptr = realloc(ptr, size);
-
-	GIT_UNUSED(file);
-	GIT_UNUSED(line);
-
-	if (!new_ptr) git_error_set_oom();
-	return new_ptr;
-}
-
-static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
-{
-	size_t newsize;
-
-	if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize))
-		return NULL;
-
-	return stdalloc__realloc(ptr, newsize, file, line);
-}
-
-static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
-{
-	return stdalloc__reallocarray(NULL, nelem, elsize, file, line);
-}
-
-static void stdalloc__free(void *ptr)
-{
-	free(ptr);
-}
-
-int git_stdalloc_init_allocator(git_allocator *allocator)
-{
-	allocator->gmalloc = stdalloc__malloc;
-	allocator->gcalloc = stdalloc__calloc;
-	allocator->gstrdup = stdalloc__strdup;
-	allocator->gstrndup = stdalloc__strndup;
-	allocator->gsubstrdup = stdalloc__substrdup;
-	allocator->grealloc = stdalloc__realloc;
-	allocator->greallocarray = stdalloc__reallocarray;
-	allocator->gmallocarray = stdalloc__mallocarray;
-	allocator->gfree = stdalloc__free;
-	return 0;
-}
diff --git a/src/stdalloc.h b/src/stdalloc.h
deleted file mode 100644
index a8927a5..0000000
--- a/src/stdalloc.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-
-#ifndef INCLUDE_stdalloc_h__
-#define INCLUDE_stdalloc_h__
-
-#include "alloc.h"
-
-#include "common.h"
-
-int git_stdalloc_init_allocator(git_allocator *allocator);
-
-#endif