Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created August 13, 2010 22:13
Show Gist options
  • Save methodmissing/523633 to your computer and use it in GitHub Desktop.
Save methodmissing/523633 to your computer and use it in GitHub Desktop.
diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c
index 258f3fb..57f49de 100644
--- a/ext/mysql2/client.c
+++ b/ext/mysql2/client.c
@@ -17,8 +17,10 @@ static ID intern_merge, intern_error_number_eql, intern_sql_state_eql;
rb_iv_set(conn, "@active", Qfalse);
#define GET_CLIENT(self) \
+ mysql_client_wrapper * wrapper; \
MYSQL * client; \
- Data_Get_Struct(self, MYSQL, client);
+ Data_Get_Struct(self, mysql_client_wrapper, wrapper); \
+ client = &wrapper->client;
/*
* used to pass all arguments to mysql_real_connect while inside
@@ -66,6 +68,13 @@ struct nogvl_send_query_args {
* - mysql_ssl_set()
*/
+static void rb_mysql_client_mark(void * wrapper) {
+ mysql_client_wrapper * w = wrapper;
+ if (w) {
+ rb_gc_mark(w->encoding);
+ }
+}
+
static VALUE rb_raise_mysql2_error(MYSQL *client) {
VALUE e = rb_exc_new2(cMysql2Error, mysql_error(client));
rb_funcall(e, intern_error_number_eql, 1, INT2NUM(mysql_errno(client)));
@@ -96,7 +105,8 @@ static VALUE nogvl_connect(void *ptr) {
}
static void rb_mysql_client_free(void * ptr) {
- MYSQL * client = (MYSQL *)ptr;
+ mysql_client_wrapper * wrapper = (mysql_client_wrapper *)ptr;
+ MYSQL * client = &wrapper->client;
/*
* we'll send a QUIT message to the server, but that message is more of a
@@ -129,15 +139,11 @@ static VALUE nogvl_close(void * ptr) {
}
static VALUE allocate(VALUE klass) {
- MYSQL * client;
-
- return Data_Make_Struct(
- klass,
- MYSQL,
- NULL,
- rb_mysql_client_free,
- client
- );
+ VALUE obj;
+ mysql_client_wrapper * wrapper;
+ obj = Data_Make_Struct(klass, mysql_client_wrapper, rb_mysql_client_mark, rb_mysql_client_free, wrapper);
+ wrapper->encoding = Qnil;
+ return obj;
}
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket) {
diff --git a/ext/mysql2/client.h b/ext/mysql2/client.h
index a962303..a520b2e 100644
--- a/ext/mysql2/client.h
+++ b/ext/mysql2/client.h
@@ -31,4 +31,9 @@ rb_thread_blocking_region(
void init_mysql2_client();
+typedef struct {
+ VALUE encoding;
+ MYSQL client;
+} mysql_client_wrapper;
+
#endif
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment