Skip to content

Commit ac6a82a

Browse files
authored
Merge pull request #756 from libgit2/cmn/memory-allocator
Set a custom allocator to redirect via the ruby runtime
2 parents 734ae11 + f049f6d commit ac6a82a

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

ext/rugged/rugged.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ void Init_rugged(void)
604604
*/
605605
rb_define_const(rb_mRugged, "SORT_REVERSE", INT2FIX(GIT_SORT_REVERSE));
606606

607-
/* Initialize libgit2 */
607+
/* Set the allocator and initialize libgit2 */
608+
rugged_set_allocator();
608609
git_libgit2_init();
609610

610611
/* Hook a global object to cleanup the library

ext/rugged/rugged.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <ruby/encoding.h>
2121
#endif
2222

23+
#include <ruby/util.h>
24+
2325
#include <assert.h>
2426
#include <git2.h>
2527
#include <git2/odb_backend.h>
@@ -173,4 +175,6 @@ typedef struct _rugged_backend {
173175
int (* refdb_backend)(git_refdb_backend **backend_out, struct _rugged_backend *backend, const char* path);
174176
} rugged_backend;
175177

178+
extern void rugged_set_allocator(void);
179+
176180
#endif

ext/rugged/rugged_allocator.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (C) the Rugged contributors. All rights reserved.
3+
*
4+
* This file is part of Rugged, distributed under the MIT license.
5+
* For full terms see the included LICENSE file.
6+
*/
7+
8+
#include "rugged.h"
9+
#include <git2/sys/alloc.h>
10+
11+
static void *rugged_gmalloc(size_t n, const char *file, int line)
12+
{
13+
return xmalloc(n);
14+
}
15+
16+
static void *rugged_gcalloc(size_t nelem, size_t elsize, const char *file, int line)
17+
{
18+
return xcalloc(nelem, elsize);
19+
}
20+
21+
static char *rugged_gstrdup(const char *str, const char *file, int line)
22+
{
23+
return ruby_strdup(str);
24+
}
25+
26+
static char *rugged_gstrndup(const char *str, size_t n, const char *file, int line)
27+
{
28+
size_t len;
29+
char *newstr;
30+
31+
len = strlen(str);
32+
if (len < n)
33+
n = len;
34+
35+
newstr = xmalloc(n+1);
36+
memcpy(newstr, str, n);
37+
newstr[n] = '\0';
38+
39+
return newstr;
40+
}
41+
42+
static char *rugged_gsubstrdup(const char *str, size_t n, const char *file, int line)
43+
{
44+
char *newstr;
45+
46+
newstr = xmalloc(n+1);
47+
memcpy(newstr, str, n);
48+
newstr[n] = '\0';
49+
50+
return newstr;
51+
}
52+
53+
static void *rugged_grealloc(void *ptr, size_t size, const char *file, int line)
54+
{
55+
return xrealloc(ptr, size);
56+
}
57+
58+
static void *rugged_greallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
59+
{
60+
return xrealloc2(ptr, nelem, elsize);
61+
}
62+
63+
static void *rugged_gmallocarray(size_t nelem, size_t elsize, const char *file, int line)
64+
{
65+
return xmalloc2(nelem, elsize);
66+
}
67+
68+
static void rugged_gfree(void *ptr)
69+
{
70+
xfree(ptr);
71+
}
72+
73+
void rugged_set_allocator(void)
74+
{
75+
git_allocator allocator;
76+
77+
allocator.gmalloc = rugged_gmalloc;
78+
allocator.gcalloc = rugged_gcalloc;
79+
allocator.gstrdup = rugged_gstrdup;
80+
allocator.gstrndup = rugged_gstrndup;
81+
allocator.gstrndup = rugged_gstrndup;
82+
allocator.gsubstrdup = rugged_gsubstrdup;
83+
allocator.grealloc = rugged_grealloc;
84+
allocator.greallocarray = rugged_greallocarray;
85+
allocator.gmallocarray = rugged_gmallocarray;
86+
allocator.gfree = rugged_gfree;
87+
88+
git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &allocator);
89+
}

vendor/libgit2

Submodule libgit2 updated 1 file

0 commit comments

Comments
 (0)