alloc.h 1.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * 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__

11 12
#include "git2/sys/alloc.h"

13 14
#include "git2_util.h"

15 16
extern git_allocator git__allocator;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
GIT_INLINE(void *) git__malloc(size_t len)
{
	void *p = git__allocator.gmalloc(len, __FILE__, __LINE__);

	if (!p)
		git_error_set_oom();

	return p;
}

GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
{
	void *p = git__allocator.grealloc(ptr, size, __FILE__, __LINE__);

	if (!p)
		git_error_set_oom();

	return p;
}

GIT_INLINE(void) git__free(void *ptr)
{
	git__allocator.gfree(ptr);
}

extern void *git__calloc(size_t nelem, size_t elsize);
extern void *git__mallocarray(size_t nelem, size_t elsize);
extern void *git__reallocarray(void *ptr, size_t nelem, size_t elsize);

extern char *git__strdup(const char *str);
extern char *git__strndup(const char *str, size_t n);
extern char *git__substrdup(const char *str, size_t n);
49 50 51 52 53 54

/**
 * This function is being called by our global setup routines to
 * initialize the standard allocator.
 */
int git_allocator_global_init(void);
55

56 57
/**
 * Switch out libgit2's global memory allocator
58
 *
59 60 61
 * @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.
62
 */
63
int git_allocator_setup(git_allocator *allocator);
64 65

#endif