From 6304fa33b34dc3c9514920bce49ea7121d6bbf3d Mon Sep 17 00:00:00 2001 From: "Erik S. Chang" Date: Thu, 3 Mar 2011 15:02:12 -0800 Subject: [PATCH] cleanups --- ext/lwes_ext/emitter.c | 54 ++++++++++++++++++++++++++++++++---------------- ext/lwes_ext/extconf.rb | 4 ++-- ext/lwes_ext/lwes_ruby.h | 5 ----- lib/lwes/struct.rb | 2 +- test/test_helper.rb | 2 +- 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/ext/lwes_ext/emitter.c b/ext/lwes_ext/emitter.c index 68f43ad..dca24f6 100644 --- a/ext/lwes_ext/emitter.c +++ b/ext/lwes_ext/emitter.c @@ -93,6 +93,11 @@ static VALUE rle_alloc(VALUE klass) NULL, rle_free, rle); } +struct hash_memo { + size_t off; + LWES_BYTE_P buf; +}; + /* * kv - Array: * key => String, @@ -104,13 +109,14 @@ static VALUE rle_alloc(VALUE klass) static VALUE event_hash_iter_i(VALUE kv, VALUE memo) { volatile VALUE raise_inspect; - VALUE *tmp = (VALUE *)memo; + struct hash_memo *hash_memo = (struct hash_memo *)NUM2ULONG(memo); VALUE val; VALUE name; char *attr_name; int rv = 0; - LWES_BYTE_P buf = (LWES_BYTE_P)tmp[0]; - size_t *off = (size_t *)tmp[1]; + LWES_BYTE_P buf = hash_memo->buf; + size_t *off = &hash_memo->off; + VALUE *tmp; if (TYPE(kv) != T_ARRAY || RARRAY_LEN(kv) != 2) rb_raise(rb_eTypeError, @@ -153,25 +159,24 @@ static VALUE event_hash_iter_i(VALUE kv, VALUE memo) static VALUE emit_hash(VALUE self, VALUE name, VALUE event) { struct _rb_lwes_emitter *rle = _rle(self); - LWES_BYTE_P buf = rle->emitter->buffer; - VALUE tmp[2]; - size_t off = 0; + struct hash_memo hash_memo; + LWES_BYTE_P buf; + size_t *off; + VALUE memo = ULONG2NUM((unsigned long)&hash_memo); VALUE enc; - int size = NUM2INT(rb_funcall(event, id_size, 0, 0)); + LWES_U_INT_16 size = lwesrb_uint16(rb_funcall(event, id_size, 0, 0)); int rv; char *event_name = StringValueCStr(name); - tmp[0] = (VALUE)buf; - tmp[1] = (VALUE)&off; - - if (size < 0 || size > UINT16_MAX) - rb_raise(rb_eRangeError, "hash size out of uint16 range"); + buf = hash_memo.buf = rle->emitter->buffer; + hash_memo.off = 0; + off = &hash_memo.off; /* event name first */ - dump_name(event_name, buf, &off); + dump_name(event_name, buf, off); /* number of attributes second */ - rv = marshall_U_INT_16((LWES_U_INT_16)size, buf, MAX_MSG_SIZE, &off); + rv = marshall_U_INT_16(size, buf, MAX_MSG_SIZE, off); if (rv <= 0) rb_raise(rb_eRuntimeError, "failed to dump num_attrs"); @@ -180,12 +185,12 @@ static VALUE emit_hash(VALUE self, VALUE name, VALUE event) if (NIL_P(enc)) enc = rb_hash_aref(event, ENC); if (! NIL_P(enc)) - dump_enc(enc, buf, &off); + dump_enc(enc, buf, off); /* the rest of the fields */ - rb_iterate(rb_each, event, event_hash_iter_i, (VALUE)&tmp); + rb_iterate(rb_each, event, event_hash_iter_i, memo); - if (lwes_emitter_emit_bytes(rle->emitter, buf, off) < 0) + if (lwes_emitter_emit_bytes(rle->emitter, buf, *off) < 0) rb_raise(rb_eRuntimeError, "failed to emit event"); return event; @@ -243,6 +248,19 @@ static void lwes_struct_class( *have_enc = rb_const_get(*event_class, id_HAVE_ENCODING); } +#if !defined(RSTRUCT_PTR) && defined(RSTRUCT) +# define RSTRUCT_PTR(s) (RSTRUCT(s)->ptr) +#endif +static VALUE * rstruct_ptr(VALUE *ary, VALUE rstruct) +{ +#ifdef RSTRUCT_PTR + return RSTRUCT_PTR(*ary = rstruct); +#else + *ary = rb_funcall(rstruct, rb_intern("to_a"), 0, 0); + return RARRAY_PTR(*ary); +#endif +} + static VALUE emit_struct(VALUE self, VALUE event) { VALUE event_class, name, type_list, have_enc; @@ -278,7 +296,7 @@ static VALUE emit_struct(VALUE self, VALUE event) } i = RARRAY_LEN(type_list); - flds = RSTRUCT_PTR(event); + flds = rstruct_ptr(&name, event); tmp = RARRAY_PTR(type_list); for (; --i >= 0; tmp++, flds++) { /* inner: [ :field_sym, "field_name", type ] */ diff --git a/ext/lwes_ext/extconf.rb b/ext/lwes_ext/extconf.rb index fccf34d..25efd71 100644 --- a/ext/lwes_ext/extconf.rb +++ b/ext/lwes_ext/extconf.rb @@ -41,14 +41,14 @@ end unless have_library('lwes') && have_header('lwes.h') warn "LWES library not found, building locally" sub_cd(pwd) do - unless test ?r, tgz + unless File.readable?(tgz) response = fetch(url) File.open("#{tgz}.#{$$}.#{rand}.tmp", "wb") do |fp| fp.write(response.body) File.rename(fp.path, tgz) end end - unless test ?r, "#{inst}/.ok" + unless File.readable?("#{inst}/.ok") FileUtils.rm_rf(dir) system('tar', 'zxf', tgz) or abort "tar failed with #{$?}" sub_cd(dir) do diff --git a/ext/lwes_ext/lwes_ruby.h b/ext/lwes_ext/lwes_ruby.h index 8881ee4..2f1f37d 100644 --- a/ext/lwes_ext/lwes_ruby.h +++ b/ext/lwes_ext/lwes_ruby.h @@ -37,11 +37,6 @@ void lwesrb_dump_num_ary(VALUE array, LWES_BYTE_P buf, size_t *off); # define RARRAY_LEN(s) (RARRAY(s)->len) #endif -#ifndef RSTRUCT_PTR -# define RSTRUCT_PTR(s) (RSTRUCT(s)->ptr) -# define RSTRUCT_LEN(s) (RSTRUCT(s)->len) -#endif - #define RAISE_INSPECT(v) RSTRING_PTR(raise_inspect = rb_inspect(v)) extern VALUE cLWES_Event; diff --git a/lib/lwes/struct.rb b/lib/lwes/struct.rb index d6b5505..05d5447 100644 --- a/lib/lwes/struct.rb +++ b/lib/lwes/struct.rb @@ -30,7 +30,7 @@ module LWES db ||= begin file = options[:file] or raise ArgumentError, "TypeDB :db or ESF :file missing" - test ?r, file or + File.readable?(file) or raise ArgumentError, "file #{file.inspect} not readable" TypeDB.new(file) end diff --git a/test/test_helper.rb b/test/test_helper.rb index b9b3191..02720e4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,7 +16,7 @@ unless defined?(LISTENER_DEFAULTS) end private_bin = "ext/lwes_ext/.inst/bin" -if test ?d, private_bin +if File.directory? private_bin ENV['PATH'] = "#{private_bin}:#{ENV['PATH']}" end -- 2.11.4.GIT