From 79583aaa426ac2f0967cb84524b6e17d72499cd5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 24 Dec 2010 23:24:29 -0800 Subject: refactor type checks and remove unnecessary guards StringValuePtr should already be a sufficient guard. There are more tests while we're at it, too, and we'll now raise TypeError instead of ArgumentError when a POSIX_MQ::Attr is not passed properly. --- ext/posix_mq/posix_mq.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'ext/posix_mq/posix_mq.c') diff --git a/ext/posix_mq/posix_mq.c b/ext/posix_mq/posix_mq.c index b634a91..26c4514 100644 --- a/ext/posix_mq/posix_mq.c +++ b/ext/posix_mq/posix_mq.c @@ -14,10 +14,6 @@ # define NUM2TIMET NUM2INT #endif -#ifndef RB_GC_GUARD -# define RB_GC_GUARD(v) (*(volatile VALUE *)&(v)) -#endif - #include #include #include @@ -303,17 +299,21 @@ static struct posix_mq *get(VALUE self, int need_valid) return mq; } +static void check_struct_type(VALUE astruct) +{ + if (CLASS_OF(astruct) == cAttr) + return; + astruct = rb_inspect(astruct); + rb_raise(rb_eTypeError, "not a POSIX_MQ::Attr: %s", + StringValuePtr(astruct)); +} + /* converts the POSIX_MQ::Attr astruct into a struct mq_attr attr */ static void attr_from_struct(struct mq_attr *attr, VALUE astruct, int all) { VALUE *ptr; - if (CLASS_OF(astruct) != cAttr) { - RB_GC_GUARD(astruct) = rb_inspect(astruct); - rb_raise(rb_eArgError, "not a POSIX_MQ::Attr: %s", - RSTRING_PTR(astruct)); - } - + check_struct_type(astruct); ptr = RSTRUCT_PTR(astruct); attr->mq_flags = NUM2LONG(ptr[0]); @@ -369,10 +369,10 @@ static VALUE init(int argc, VALUE *argv, VALUE self) else if (oflags == sym_rw) x.oflags = O_CREAT|O_RDWR; else { - RB_GC_GUARD(oflags) = oflags; + oflags = rb_inspect(oflags); rb_raise(rb_eArgError, "symbol must be :r, :w, or :rw: %s", - RSTRING_PTR(oflags)); + StringValuePtr(oflags)); } break; case T_BIGNUM: @@ -413,9 +413,7 @@ static VALUE init(int argc, VALUE *argv, VALUE self) case T_NIL: break; default: - RB_GC_GUARD(attr) = rb_inspect(attr); - rb_raise(rb_eArgError, "attr must be a POSIX_MQ::Attr: %s", - RSTRING_PTR(attr)); + check_struct_type(attr); } mq->des = (mqd_t)xopen(&x); -- cgit v1.2.3-24-ge0c7