stdalloc.c 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * 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"

10
static void *stdalloc__malloc(size_t len, const char *file, int line)
11 12
{
	void *ptr = malloc(len);
13 14 15 16

	GIT_UNUSED(file);
	GIT_UNUSED(line);

17 18 19 20
	if (!ptr) giterr_set_oom();
	return ptr;
}

21
static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line)
22 23
{
	void *ptr = calloc(nelem, elsize);
24 25 26 27

	GIT_UNUSED(file);
	GIT_UNUSED(line);

28 29 30 31
	if (!ptr) giterr_set_oom();
	return ptr;
}

32
static char *stdalloc__strdup(const char *str, const char *file, int line)
33 34
{
	char *ptr = strdup(str);
35 36 37 38

	GIT_UNUSED(file);
	GIT_UNUSED(line);

39 40 41 42
	if (!ptr) giterr_set_oom();
	return ptr;
}

43
static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line)
44 45 46 47 48 49 50
{
	size_t length = 0, alloclength;
	char *ptr;

	length = p_strnlen(str, n);

	if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
51
		!(ptr = stdalloc__malloc(alloclength, file, line)))
52 53 54 55 56 57 58 59 60 61
		return NULL;

	if (length)
		memcpy(ptr, str, length);

	ptr[length] = '\0';

	return ptr;
}

62
static char *stdalloc__substrdup(const char *start, size_t n, const char *file, int line)
63 64 65 66 67
{
	char *ptr;
	size_t alloclen;

	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
68
		!(ptr = stdalloc__malloc(alloclen, file, line)))
69 70 71 72 73 74 75
		return NULL;

	memcpy(ptr, start, n);
	ptr[n] = '\0';
	return ptr;
}

76
static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
77 78
{
	void *new_ptr = realloc(ptr, size);
79 80 81 82

	GIT_UNUSED(file);
	GIT_UNUSED(line);

83 84 85 86
	if (!new_ptr) giterr_set_oom();
	return new_ptr;
}

87
static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
88 89
{
	size_t newsize;
90 91 92 93

	GIT_UNUSED(file);
	GIT_UNUSED(line);

94 95 96 97
	return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
		NULL : realloc(ptr, newsize);
}

98
static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
99
{
100
	return stdalloc__reallocarray(NULL, nelem, elsize, file, line);
101 102
}

103
static void stdalloc__free(void *ptr)
104 105 106
{
	free(ptr);
}
107 108 109 110 111 112 113 114 115 116 117 118 119 120

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;
}