From 7c4b13ddc88e25194436fb8b4304a02aee6c610d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 9 Feb 2014 09:52:11 +0000 Subject: use a normal Ruby hash to store exceptions st.h should probably not be part of the public Ruby API, and it may well be removed one day (I would support it). --- ext/tdb/tdb.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'ext') diff --git a/ext/tdb/tdb.c b/ext/tdb/tdb.c index 58e16bf..c108c38 100644 --- a/ext/tdb/tdb.c +++ b/ext/tdb/tdb.c @@ -3,17 +3,12 @@ #include #include #include -#ifdef HAVE_RUBY_ST_H -# include -#else -# include -#endif #include /* this protects the global list of tdb objects maintained by libtdb */ static pthread_mutex_t big_lock = PTHREAD_MUTEX_INITIALIZER; static VALUE cTDB, cERR; -static st_table *exc_hash; +static VALUE exc_hash; static VALUE hashes; /* must be a macro to prevent GC from killing converted 'val's */ @@ -26,13 +21,14 @@ static VALUE hashes; static void init_exc(enum TDB_ERROR ecode, const char *name) { VALUE exc = rb_define_class_under(cERR, name, cERR); - st_insert(exc_hash, (st_data_t)ecode, (st_data_t)exc); + rb_hash_aset(exc_hash, INT2NUM(ecode), exc); } static void init_errors(void) { cERR = rb_define_class_under(cTDB, "ERR", rb_eStandardError); - exc_hash = st_init_numtable(); + exc_hash = rb_hash_new(); + rb_global_variable(&exc_hash); init_exc(TDB_ERR_CORRUPT, "CORRUPT"); init_exc(TDB_ERR_IO, "IO"); @@ -53,7 +49,7 @@ static void my_raise(struct tdb_context *tdb) { enum TDB_ERROR ecode = tdb_error(tdb); const char *str = tdb_errorstr(tdb); - VALUE exc; + VALUE exc = Qnil; switch (ecode) { case TDB_SUCCESS: @@ -71,9 +67,10 @@ static void my_raise(struct tdb_context *tdb) #ifdef HAVE_CONST_TDB_ERR_NESTING case TDB_ERR_NESTING: #endif /* HAVE_CONST_TDB_ERR_NESTING */ - if (!st_lookup(exc_hash, (st_data_t)ecode, (st_data_t *)&exc)) - rb_bug("no-existent exception: %s\n", str); + exc = rb_hash_aref(exc_hash, INT2NUM(ecode)); } + if (NIL_P(exc)) + rb_bug("no-existent exception: %s\n", str); rb_raise(exc, str); } -- cgit v1.2.3-24-ge0c7