From 2ef1e413c6d7f876bd9595bd218e7d0235501efb Mon Sep 17 00:00:00 2001 From: "Erik S. Chang" Date: Wed, 2 Mar 2011 23:40:13 +0000 Subject: [PATCH] cleanups --- ext/lwes_ext/emitter.c | 1 - ext/lwes_ext/event.c | 7 +++---- ext/lwes_ext/type_db.c | 41 ++++++++++++++++------------------------- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/ext/lwes_ext/emitter.c b/ext/lwes_ext/emitter.c index 713ea97..a7e5883 100644 --- a/ext/lwes_ext/emitter.c +++ b/ext/lwes_ext/emitter.c @@ -465,7 +465,6 @@ static VALUE _create(VALUE self, VALUE options) { struct _rb_lwes_emitter *rle = _rle(self); VALUE address, iface, port, heartbeat, ttl; - const char *str; rle->emit_heartbeat = FALSE; rle->freq = 0; diff --git a/ext/lwes_ext/event.c b/ext/lwes_ext/event.c index 03c09f7..614460e 100644 --- a/ext/lwes_ext/event.c +++ b/ext/lwes_ext/event.c @@ -20,7 +20,6 @@ static void event_free(void *ptr) lwes_event_destroy(event); } - static VALUE event_alloc(VALUE klass) { struct lwes_event *e; @@ -91,11 +90,11 @@ static VALUE event_aset(VALUE self, VALUE key, VALUE val) LWES_BYTE *attr_type; if (ehash == NULL) - rb_raise(rb_eArgError, "invalid event: %s\n", e->eventName); + rb_raise(rb_eArgError, "invalid event: %s", e->eventName); attr_type = lwes_hash_get(ehash, attr); if (attr_type == NULL) - rb_raise(rb_eArgError, "invalid attribute: %s\n", attr); + rb_raise(rb_eArgError, "invalid attribute: %s", attr); switch (*attr_type) { } @@ -176,7 +175,7 @@ static VALUE parse(VALUE self, VALUE buf) rc = lwes_event_from_bytes(e, bytes, num_bytes, 0, &dtmp); if (rc < 0) rb_raise(rb_eRuntimeError, - "failed to parse LWES event (code: %d)\n", rc); + "failed to parse LWES event (code: %d)", rc); return event; } diff --git a/ext/lwes_ext/type_db.c b/ext/lwes_ext/type_db.c index 52fa0c4..df95f85 100644 --- a/ext/lwes_ext/type_db.c +++ b/ext/lwes_ext/type_db.c @@ -2,38 +2,30 @@ VALUE cLWES_TypeDB; -struct _tdb { - struct lwes_event_type_db *db; -}; - static void tdb_free(void *ptr) { - struct _tdb *tdb = ptr; + struct lwes_event_type_db *db = ptr; - if (tdb->db) - lwes_event_type_db_destroy(tdb->db); - xfree(ptr); + if (db) + lwes_event_type_db_destroy(db); } static VALUE tdb_alloc(VALUE klass) { - struct _tdb *tdb; - - return Data_Make_Struct(klass, struct _tdb, NULL, tdb_free, tdb); + return Data_Wrap_Struct(klass, NULL, tdb_free, NULL); } static VALUE tdb_init(VALUE self, VALUE path) { - struct _tdb *tdb; + struct lwes_event_type_db *db = DATA_PTR(self); int gc_retry = 1; char *cpath = StringValueCStr(path); - Data_Get_Struct(self, struct _tdb, tdb); - if (tdb->db) + if (db) rb_raise(rb_eRuntimeError, "ESF already initialized"); retry: - tdb->db = lwes_event_type_db_create(cpath); - if (!tdb->db) { + db = lwes_event_type_db_create(cpath); + if (!db) { if (--gc_retry == 0) { rb_gc(); goto retry; @@ -41,6 +33,7 @@ retry: rb_raise(rb_eRuntimeError, "failed to create type DB for LWES file %s", cpath); } + DATA_PTR(self) = db; return Qnil; } @@ -97,28 +90,26 @@ static VALUE event_def_ary(struct lwes_hash *hash) struct lwes_event_type_db * lwesrb_get_type_db(VALUE self) { - struct _tdb *tdb; + struct lwes_event_type_db *db = DATA_PTR(self); - Data_Get_Struct(self, struct _tdb, tdb); - if (! tdb->db) { + if (!db) { volatile VALUE raise_inspect; rb_raise(rb_eRuntimeError, "couldn't get lwes_type_db from %s", RAISE_INSPECT(self)); } - return tdb->db; + return db; } static VALUE tdb_to_hash(VALUE self) { - struct _tdb *tdb; + struct lwes_event_type_db *db = DATA_PTR(self); VALUE rv = rb_hash_new(); struct lwes_hash *events; struct lwes_hash_enumeration e; - Data_Get_Struct(self, struct _tdb, tdb); - assert(tdb->db && tdb->db->events && "tdb not initialized"); - events = tdb->db->events; + assert(db && db->events && "tdb not initialized"); + events = db->events; if (!lwes_hash_keys(events, &e)) rb_raise(rb_eRuntimeError, "couldn't get type_db events"); @@ -148,7 +139,7 @@ void lwesrb_init_type_db(void) { VALUE mLWES = rb_define_module("LWES"); cLWES_TypeDB = rb_define_class_under(mLWES, "TypeDB", rb_cObject); - rb_define_method(cLWES_TypeDB, "initialize", tdb_init, 1); + rb_define_private_method(cLWES_TypeDB, "initialize", tdb_init, 1); rb_define_method(cLWES_TypeDB, "to_hash", tdb_to_hash, 0); rb_define_alloc_func(cLWES_TypeDB, tdb_alloc); } -- 2.11.4.GIT