Unverified Commit c5594852 by Edward Thomson Committed by GitHub

Merge pull request #4998 from pks-t/pks/allocator-restructuring

Allocator restructuring
parents 3aa8401a 765ff6e0
......@@ -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)
......
......@@ -7,11 +7,8 @@
#include "alloc.h"
#if defined(GIT_MSVC_CRTDBG)
# include "win32/w32_crtdbg_stacktrace.h"
#else
# include "stdalloc.h"
#endif
#include "allocators/stdalloc.h"
#include "allocators/win32_crtdbg.h"
git_allocator git__allocator;
......@@ -44,12 +41,3 @@ int git_allocator_setup(git_allocator *allocator)
memcpy(&git__allocator, allocator, sizeof(*allocator));
return 0;
}
#if !defined(GIT_MSVC_CRTDBG)
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
{
GIT_UNUSED(allocator);
git_error_set(GIT_EINVALID, "crtdbg memory allocator not available");
return -1;
}
#endif
......@@ -5,13 +5,13 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_stdalloc_h__
#define INCLUDE_stdalloc_h__
#include "alloc.h"
#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
/*
* 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 "win32_crtdbg.h"
#if defined(GIT_MSVC_CRTDBG)
#include "win32/w32_crtdbg_stacktrace.h"
static void *crtdbg__malloc(size_t len, const char *file, int line)
{
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static void *crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
{
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static char *crtdbg__strdup(const char *str, const char *file, int line)
{
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static char *crtdbg__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 = crtdbg__malloc(alloclength, file, line)))
return NULL;
if (length)
memcpy(ptr, str, length);
ptr[length] = '\0';
return ptr;
}
static char *crtdbg__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 = crtdbg__malloc(alloclen, file, line)))
return NULL;
memcpy(ptr, start, n);
ptr[n] = '\0';
return ptr;
}
static void *crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
{
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!new_ptr) git_error_set_oom();
return new_ptr;
}
static void *crtdbg__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 crtdbg__realloc(ptr, newsize, file, line);
}
static void *crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
{
return crtdbg__reallocarray(NULL, nelem, elsize, file, line);
}
static void crtdbg__free(void *ptr)
{
free(ptr);
}
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
{
allocator->gmalloc = crtdbg__malloc;
allocator->gcalloc = crtdbg__calloc;
allocator->gstrdup = crtdbg__strdup;
allocator->gstrndup = crtdbg__strndup;
allocator->gsubstrdup = crtdbg__substrdup;
allocator->grealloc = crtdbg__realloc;
allocator->greallocarray = crtdbg__reallocarray;
allocator->gmallocarray = crtdbg__mallocarray;
allocator->gfree = crtdbg__free;
return 0;
}
#else
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
{
GIT_UNUSED(allocator);
git_error_set(GIT_EINVALID, "crtdbg memory allocator not available");
return -1;
}
#endif
/*
* 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_crtdbg_h
#define INCLUDE_allocators_crtdbg_h
#include "common.h"
#include "alloc.h"
int git_win32_crtdbg_init_allocator(git_allocator *allocator);
#endif
......@@ -71,99 +71,6 @@ static bool g_limit_reached = false; /* had allocs after we filled row table */
static unsigned int g_checkpoint_id = 0; /* to better label leak checkpoints */
static bool g_transient_leaks_since_mark = false; /* payload for hook */
static void *crtdbg__malloc(size_t len, const char *file, int line)
{
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static void *crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
{
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static char *crtdbg__strdup(const char *str, const char *file, int line)
{
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static char *crtdbg__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 = crtdbg__malloc(alloclength, file, line)))
return NULL;
if (length)
memcpy(ptr, str, length);
ptr[length] = '\0';
return ptr;
}
static char *crtdbg__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 = crtdbg__malloc(alloclen, file, line)))
return NULL;
memcpy(ptr, start, n);
ptr[n] = '\0';
return ptr;
}
static void *crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
{
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
if (!new_ptr) git_error_set_oom();
return new_ptr;
}
static void *crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
{
size_t newsize;
return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
}
static void *crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
{
return crtdbg__reallocarray(NULL, nelem, elsize, file, line);
}
static void crtdbg__free(void *ptr)
{
free(ptr);
}
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
{
allocator->gmalloc = crtdbg__malloc;
allocator->gcalloc = crtdbg__calloc;
allocator->gstrdup = crtdbg__strdup;
allocator->gstrndup = crtdbg__strndup;
allocator->gsubstrdup = crtdbg__substrdup;
allocator->grealloc = crtdbg__realloc;
allocator->greallocarray = crtdbg__reallocarray;
allocator->gmallocarray = crtdbg__mallocarray;
allocator->gfree = crtdbg__free;
return 0;
}
/**
* Compare function for bsearch on g_cs_index table.
*/
......
......@@ -43,8 +43,6 @@
* startup. See tests/main.c for an example.
*/
int git_win32_crtdbg_init_allocator(git_allocator *allocator);
/**
* Initialize our memory leak tracking and de-dup data structures.
* This should ONLY be called by git_libgit2_init().
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment