File: rugged_allocator.c

package info (click to toggle)
ruby-rugged 1.5.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,748 kB
  • sloc: ansic: 8,746; ruby: 7,464; sh: 94; makefile: 5
file content (89 lines) | stat: -rw-r--r-- 1,963 bytes parent folder | download | duplicates (2)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) the Rugged contributors.  All rights reserved.
 *
 * This file is part of Rugged, distributed under the MIT license.
 * For full terms see the included LICENSE file.
 */

#include "rugged.h"
#include <git2/sys/alloc.h>

static void *rugged_gmalloc(size_t n, const char *file, int line)
{
	return xmalloc(n);
}

static void *rugged_gcalloc(size_t nelem, size_t elsize, const char *file, int line)
{
	return xcalloc(nelem, elsize);
}

static char *rugged_gstrdup(const char *str, const char *file, int line)
{
	return ruby_strdup(str);
}

static char *rugged_gstrndup(const char *str, size_t n, const char *file, int line)
{
	size_t len;
	char *newstr;

	len = strnlen(str, n);
	if (len < n)
		n = len;

	newstr = xmalloc(n+1);
	memcpy(newstr, str, n);
	newstr[n] = '\0';

	return newstr;
}

static char *rugged_gsubstrdup(const char *str, size_t n, const char *file, int line)
{
	char *newstr;

	newstr = xmalloc(n+1);
	memcpy(newstr, str, n);
	newstr[n] = '\0';

	return newstr;
}

static void *rugged_grealloc(void *ptr, size_t size, const char *file, int line)
{
	return xrealloc(ptr, size);
}

static void *rugged_greallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
{
	return xrealloc2(ptr, nelem, elsize);
}

static void *rugged_gmallocarray(size_t nelem, size_t elsize, const char *file, int line)
{
	return xmalloc2(nelem, elsize);
}

static void rugged_gfree(void *ptr)
{
	xfree(ptr);
}

void rugged_set_allocator(void)
{
	git_allocator allocator;

	allocator.gmalloc = rugged_gmalloc;
	allocator.gcalloc = rugged_gcalloc;
	allocator.gstrdup = rugged_gstrdup;
	allocator.gstrndup = rugged_gstrndup;
	allocator.gstrndup = rugged_gstrndup;
	allocator.gsubstrdup = rugged_gsubstrdup;
	allocator.grealloc = rugged_grealloc;
	allocator.greallocarray = rugged_greallocarray;
	allocator.gmallocarray = rugged_gmallocarray;
	allocator.gfree = rugged_gfree;

	git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &allocator);
}