Skip to content

Commit aebbf2f

Browse files
committed
Set a custom allocator to redirect via the ruby runtime
This lets us tell the ruby runtime how much memory rugged/libgit2 is allocating to give it a more realistic view of how much memory the process is using. The expectation/hope is that long-lived processes will give back memory to the system more readily than is currently the case.
1 parent 075f5d6 commit aebbf2f

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,6 @@ typedef struct _rugged_backend {
173173
int (* refdb_backend)(git_refdb_backend **backend_out, struct _rugged_backend *backend, const char* path);
174174
} rugged_backend;
175175

176+
extern void rugged_set_allocator(void);
177+
176178
#endif

ext/rugged/rugged_allocator.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
char *newstr;
24+
size_t n;
25+
26+
n = strlen(str);
27+
newstr = xmalloc(n+1);
28+
memcpy(newstr, str, n);
29+
newstr[n] = '\0';
30+
31+
return newstr;
32+
}
33+
34+
static char *rugged_gstrndup(const char *str, size_t n, const char *file, int line)
35+
{
36+
size_t len;
37+
char *newstr;
38+
39+
len = strlen(str);
40+
if (len < n)
41+
n = len;
42+
43+
newstr = xmalloc(n+1);
44+
memcpy(newstr, str, n);
45+
newstr[n] = '\0';
46+
47+
return newstr;
48+
}
49+
50+
static char *rugged_gsubstrdup(const char *str, size_t n, const char *file, int line)
51+
{
52+
char *newstr;
53+
54+
newstr = xmalloc(n+1);
55+
memcpy(newstr, str, n);
56+
newstr[n] = '\0';
57+
58+
return newstr;
59+
}
60+
61+
static void *rugged_grealloc(void *ptr, size_t size, const char *file, int line)
62+
{
63+
return xrealloc(ptr, size);
64+
}
65+
66+
static void *rugged_greallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
67+
{
68+
return xrealloc2(ptr, nelem, elsize);
69+
}
70+
71+
static void *rugged_gmallocarray(size_t nelem, size_t elsize, const char *file, int line)
72+
{
73+
return xmalloc2(nelem, elsize);
74+
}
75+
76+
static void rugged_gfree(void *ptr)
77+
{
78+
xfree(ptr);
79+
}
80+
81+
void rugged_set_allocator(void)
82+
{
83+
git_allocator allocator;
84+
85+
allocator.gmalloc = rugged_gmalloc;
86+
allocator.gcalloc = rugged_gcalloc;
87+
allocator.gstrdup = rugged_gstrdup;
88+
allocator.gstrndup = rugged_gstrndup;
89+
allocator.gstrndup = rugged_gstrndup;
90+
allocator.gsubstrdup = rugged_gsubstrdup;
91+
allocator.grealloc = rugged_grealloc;
92+
allocator.greallocarray = rugged_greallocarray;
93+
allocator.gmallocarray = rugged_gmallocarray;
94+
allocator.gfree = rugged_gfree;
95+
96+
git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &allocator);
97+
}

0 commit comments

Comments
 (0)