From 2335108162854619be9407703302fc3a4f2c8c81 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Mon, 14 Feb 2011 13:08:29 +0100 Subject: [PATCH] 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. --- ext/cbson/cbson.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index 9791d7a..f71ed48 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -162,6 +162,7 @@ static void write_utf8(buffer_t buffer, VALUE string, char check_null) { *buffer = malloc(vslength); \ _snprintf(*buffer, vslength, "%d", i); \ } +#define FREE_INTSTRING(buffer) free(buffer) #else #define INT2STRING(buffer, i) \ { \ @@ -169,9 +170,15 @@ static void write_utf8(buffer_t buffer, VALUE string, char check_null) { *buffer = malloc(vslength); \ snprintf(*buffer, vslength, "%d", i); \ } +#define FREE_INTSTRING(buffer) free(buffer) #endif #else #define INT2STRING(buffer, i) asprintf(buffer, "%d", i); +#ifdef USING_SYSTEM_ALLOCATOR_LIBRARY /* Ruby Enterprise Edition with tcmalloc */ +#define FREE_INTSTRING(buffer) system_free(buffer) +#else +#define FREE_INTSTRING(buffer) free(buffer) +#endif #endif #ifndef RREGEXP_SRC @@ -320,7 +327,7 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) { INT2STRING(&name, i); key = rb_str_new2(name); write_element_with_id(key, rb_ary_entry(value, i), pack_extra(buffer, check_keys)); - free(name); + FREE_INTSTRING(name); } // write null byte and fill in length