Skip to content

Commit 2335108

Browse files
FooBarWidgetbanker
authored andcommitted
When possible, use system_free() to free asprintf() results.
On OS X, Ruby Enterprise Edition allocates memory with tcmalloc which is not compatible with the system malloc. asprintf() allocates memory with the system malloc so we need to free its result with the system free() function instead of tcmalloc's free() function. REE's system_free() API call does that.
1 parent 735a93d commit 2335108

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ext/cbson/cbson.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,23 @@ static void write_utf8(buffer_t buffer, VALUE string, char check_null) {
162162
*buffer = malloc(vslength); \
163163
_snprintf(*buffer, vslength, "%d", i); \
164164
}
165+
#define FREE_INTSTRING(buffer) free(buffer)
165166
#else
166167
#define INT2STRING(buffer, i) \
167168
{ \
168169
int vslength = snprintf(NULL, 0, "%d", i) + 1; \
169170
*buffer = malloc(vslength); \
170171
snprintf(*buffer, vslength, "%d", i); \
171172
}
173+
#define FREE_INTSTRING(buffer) free(buffer)
172174
#endif
173175
#else
174176
#define INT2STRING(buffer, i) asprintf(buffer, "%d", i);
177+
#ifdef USING_SYSTEM_ALLOCATOR_LIBRARY /* Ruby Enterprise Edition with tcmalloc */
178+
#define FREE_INTSTRING(buffer) system_free(buffer)
179+
#else
180+
#define FREE_INTSTRING(buffer) free(buffer)
181+
#endif
175182
#endif
176183

177184
#ifndef RREGEXP_SRC
@@ -320,7 +327,7 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
320327
INT2STRING(&name, i);
321328
key = rb_str_new2(name);
322329
write_element_with_id(key, rb_ary_entry(value, i), pack_extra(buffer, check_keys));
323-
free(name);
330+
FREE_INTSTRING(name);
324331
}
325332

326333
// write null byte and fill in length

0 commit comments

Comments
 (0)