Skip to content

Commit e36f680

Browse files
Eric Wongbrianmario
authored andcommitted
revert to using xmalloc/xfree for allocations
I've (re)read our usage of them and observed no bad interactions with the Ruby GC and no need to use RB_GC_GUARD anywhere. xmalloc() does error-checking for us, so we'll raise NoMemoryError instead of a segfaulting when/if malloc() fails to release memory. xmalloc() may increase the frequency of GC invocation in MRI because it bumps the malloc counter (and xfree() cannot decrement it), but MRI 1.9.3+ users can set a higher RUBY_GC_MALLOC_LIMIT to reduce GC frequency (or improve GC :P).
1 parent ebc7dd7 commit e36f680

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

ext/mysql2/client.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static VALUE nogvl_close(void *ptr) {
147147
#endif
148148

149149
mysql_close(wrapper->client);
150-
free(wrapper->client);
150+
xfree(wrapper->client);
151151
}
152152

153153
return Qnil;
@@ -158,7 +158,7 @@ static void rb_mysql_client_free(void * ptr) {
158158

159159
nogvl_close(wrapper);
160160

161-
free(ptr);
161+
xfree(ptr);
162162
}
163163

164164
static VALUE allocate(VALUE klass) {
@@ -169,7 +169,7 @@ static VALUE allocate(VALUE klass) {
169169
wrapper->active = 0;
170170
wrapper->reconnect_enabled = 0;
171171
wrapper->closed = 1;
172-
wrapper->client = (MYSQL*)malloc(sizeof(MYSQL));
172+
wrapper->client = (MYSQL*)xmalloc(sizeof(MYSQL));
173173
return obj;
174174
}
175175

@@ -181,19 +181,19 @@ static VALUE rb_mysql_client_escape(RB_MYSQL_UNUSED VALUE klass, VALUE str) {
181181
Check_Type(str, T_STRING);
182182

183183
oldLen = RSTRING_LEN(str);
184-
newStr = malloc(oldLen*2+1);
184+
newStr = xmalloc(oldLen*2+1);
185185

186186
newLen = mysql_escape_string((char *)newStr, StringValuePtr(str), oldLen);
187187
if (newLen == oldLen) {
188188
// no need to return a new ruby string if nothing changed
189-
free(newStr);
189+
xfree(newStr);
190190
return str;
191191
} else {
192192
rb_str = rb_str_new((const char*)newStr, newLen);
193193
#ifdef HAVE_RUBY_ENCODING_H
194194
rb_enc_copy(rb_str, str);
195195
#endif
196-
free(newStr);
196+
xfree(newStr);
197197
return rb_str;
198198
}
199199
}
@@ -504,12 +504,12 @@ static VALUE rb_mysql_client_real_escape(VALUE self, VALUE str) {
504504
#endif
505505

506506
oldLen = RSTRING_LEN(str);
507-
newStr = malloc(oldLen*2+1);
507+
newStr = xmalloc(oldLen*2+1);
508508

509509
newLen = mysql_real_escape_string(wrapper->client, (char *)newStr, StringValuePtr(str), oldLen);
510510
if (newLen == oldLen) {
511511
// no need to return a new ruby string if nothing changed
512-
free(newStr);
512+
xfree(newStr);
513513
return str;
514514
} else {
515515
rb_str = rb_str_new((const char*)newStr, newLen);
@@ -519,7 +519,7 @@ static VALUE rb_mysql_client_real_escape(VALUE self, VALUE str) {
519519
rb_str = rb_str_export_to_enc(rb_str, default_internal_enc);
520520
}
521521
#endif
522-
free(newStr);
522+
xfree(newStr);
523523
return rb_str;
524524
}
525525
}

0 commit comments

Comments
 (0)