[前][次][番号順一覧][スレッド一覧]

ruby-changes:32579

From: nobu <ko1@a...>
Date: Sun, 19 Jan 2014 23:56:09 +0900 (JST)
Subject: [ruby-changes:32579] nobu:r44658 (trunk): socket/option.c: helper functions

nobu	2014-01-19 23:56:05 +0900 (Sun, 19 Jan 2014)

  New Revision: 44658

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44658

  Log:
    socket/option.c: helper functions
    
    * ext/socket/option.c (sockopt_pack_byte, sockopt_pack_int):
      extract helper functions to pack integers in a String.

  Modified files:
    trunk/ext/socket/option.c
Index: ext/socket/option.c
===================================================================
--- ext/socket/option.c	(revision 44657)
+++ ext/socket/option.c	(revision 44658)
@@ -2,6 +2,22 @@ https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L2
 
 VALUE rb_cSockOpt;
 
+#define pack_var(v) rb_str_new((const char *)&(v), sizeof(v))
+
+static VALUE
+sockopt_pack_byte(VALUE value)
+{
+    char i = NUM2CHR(rb_to_int(value));
+    return pack_var(i);
+}
+
+static VALUE
+sockopt_pack_int(VALUE value)
+{
+    int i = NUM2INT(rb_to_int(value));
+    return pack_var(i);
+}
+
 static VALUE
 constant_to_sym(int constant, ID (*intern_const)(int))
 {
@@ -157,8 +173,7 @@ sockopt_s_byte(VALUE klass, VALUE vfamil https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L173
     int family = rsock_family_arg(vfamily);
     int level = rsock_level_arg(family, vlevel);
     int optname = rsock_optname_arg(family, level, voptname);
-    unsigned char i = (unsigned char)NUM2CHR(vint);
-    return rsock_sockopt_new(family, level, optname, rb_str_new((char*)&i, sizeof(i)));
+    return rsock_sockopt_new(family, level, optname, sockopt_pack_byte(vint));
 }
 
 /*
@@ -199,8 +214,7 @@ sockopt_s_int(VALUE klass, VALUE vfamily https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L214
     int family = rsock_family_arg(vfamily);
     int level = rsock_level_arg(family, vlevel);
     int optname = rsock_optname_arg(family, level, voptname);
-    int i = NUM2INT(vint);
-    return rsock_sockopt_new(family, level, optname, rb_str_new((char*)&i, sizeof(i)));
+    return rsock_sockopt_new(family, level, optname, sockopt_pack_int(vint));
 }
 
 /*
@@ -248,7 +262,7 @@ sockopt_s_bool(VALUE klass, VALUE vfamil https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L262
     int level = rsock_level_arg(family, vlevel);
     int optname = rsock_optname_arg(family, level, voptname);
     int i = RTEST(vbool) ? 1 : 0;
-    return rsock_sockopt_new(family, level, optname, rb_str_new((char*)&i, sizeof(i)));
+    return rsock_sockopt_new(family, level, optname, pack_var(i));
 }
 
 /*
@@ -298,7 +312,7 @@ sockopt_s_linger(VALUE klass, VALUE vono https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L312
     else
         l.l_onoff = RTEST(vonoff) ? 1 : 0;
     l.l_linger = NUM2INT(vsecs);
-    return rsock_sockopt_new(AF_UNSPEC, SOL_SOCKET, SO_LINGER, rb_str_new((char*)&l, sizeof(l)));
+    return rsock_sockopt_new(AF_UNSPEC, SOL_SOCKET, SO_LINGER, pack_var(l));
 }
 
 /*
@@ -354,12 +368,12 @@ sockopt_s_ipv4_multicast_loop(VALUE klas https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L368
 {
 #if defined(IPPROTO_IP) && defined(IP_MULTICAST_LOOP)
 # if defined(__NetBSD__) || defined(__OpenBSD__)
-    unsigned char i = NUM2CHR(rb_to_int(value));
+    VALUE o = sockopt_pack_byte(value);
 # else
-    int i = NUM2INT(rb_to_int(value));
+    VALUE o = sockopt_pack_int(value);
 # endif
     return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_LOOP,
-	    rb_str_new((char*)&i, sizeof(i)));
+			     o);
 #else
 # error IPPROTO_IP or IP_MULTICAST_LOOP is not implemented
 #endif
@@ -417,12 +431,12 @@ sockopt_s_ipv4_multicast_ttl(VALUE klass https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L431
 {
 #if defined(IPPROTO_IP) && defined(IP_MULTICAST_TTL)
 # if defined(__NetBSD__) || defined(__OpenBSD__)
-    unsigned char i = NUM2CHR(rb_to_int(value));
+    VALUE o = sockopt_pack_byte(value);
 # else
-    int i = NUM2INT(rb_to_int(value));
+    VALUE o = sockopt_pack_int(value);
 # endif
     return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_TTL,
-	    rb_str_new((char*)&i, sizeof(i)));
+			     o);
 #else
 # error IPPROTO_IP or IP_MULTICAST_TTL is not implemented
 #endif

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]