Hash :
9865cd16
Author :
Date :
2018-03-20T14:23:49
alloc: make memory allocators use function pointers Currently, our memory allocators are being redirected to the correct implementation at compile time by simply using macros. In order to make them swappable at runtime, this commit reshuffles that by instead making use of a global "git_allocator" structure, whose pointers are set up to reference the allocator functions. Like this, it becomes easy to swap out allocators by simply setting these function pointers. In order to initialize a "git_allocator", our provided allocators "stdalloc" and "crtdbg" both provide an init function. This is being called to initialize a passed in allocator struct and set up its members correctly. No support is yet included to enable users of libgit2 to switch out the memory allocator at a global level.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
* 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_alloc_h__
#define INCLUDE_alloc_h__
#include "git2/sys/alloc.h"
extern git_allocator git__allocator;
#define git__malloc(len) git__allocator.gmalloc(len, __FILE__, __LINE__)
#define git__calloc(nelem, elsize) git__allocator.gcalloc(nelem, elsize, __FILE__, __LINE__)
#define git__strdup(str) git__allocator.gstrdup(str, __FILE__, __LINE__)
#define git__strndup(str, n) git__allocator.gstrndup(str, n, __FILE__, __LINE__)
#define git__substrdup(str, n) git__allocator.gsubstrdup(str, n, __FILE__, __LINE__)
#define git__realloc(ptr, size) git__allocator.grealloc(ptr, size, __FILE__, __LINE__)
#define git__reallocarray(ptr, nelem, elsize) git__allocator.greallocarray(ptr, nelem, elsize, __FILE__, __LINE__)
#define git__mallocarray(nelem, elsize) git__allocator.gmallocarray(nelem, elsize, __FILE__, __LINE__)
#define git__free git__allocator.gfree
/**
* This function is being called by our global setup routines to
* initialize the standard allocator.
*/
int git_allocator_global_init(void);
/**
* Switch out libgit2's global memory allocator
*
* @param allocator The new allocator that should be used. All function pointers
* of it need to be set correctly.
* @return An error code or 0.
*/
int git_allocator_setup(git_allocator *allocator);
#endif