dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] ext/socket/ancdata.c (bsock_sendmsg_internal): use rb_scan_args
@ 2015-06-02  2:25 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-06-02  2:25 UTC (permalink / raw)
  To: spew

This should make the code more maintainable and make it easier to
add keyword argument support for "exception: false" in the future.
---
 ext/socket/ancdata.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c
index ef6a870..277a1e8 100644
--- a/ext/socket/ancdata.c
+++ b/ext/socket/ancdata.c
@@ -1131,12 +1131,11 @@ bsock_sendmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
 {
     rb_io_t *fptr;
     VALUE data, vflags, dest_sockaddr;
-    int controls_num;
     struct msghdr mh;
     struct iovec iov;
 #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+    VALUE controls = Qnil;
     VALUE controls_str = 0;
-    VALUE *controls_ptr = NULL;
     int family;
 #endif
     int flags;
@@ -1151,20 +1150,20 @@ bsock_sendmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
 
     if (argc == 0)
         rb_raise(rb_eArgError, "mesg argument required");
-    data = argv[0];
-    if (1 < argc) vflags = argv[1];
-    if (2 < argc) dest_sockaddr = argv[2];
-    controls_num = 3 < argc ? argc - 3 : 0;
 #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
-    if (3 < argc) { controls_ptr = &argv[3]; }
+    rb_scan_args(argc, argv, "12*", &data, &vflags, &dest_sockaddr, &controls);
+#else
+    rb_scan_args(argc, argv, "12", &data, &vflags, &dest_sockaddr);
 #endif
 
     StringValue(data);
 
-    if (controls_num) {
+    if (!NIL_P(controls)) {
 #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
 	int i;
+	int controls_num = RARRAY_LENINT(controls);
 	size_t last_pad = 0;
+	const VALUE *controls_ptr = RARRAY_CONST_PTR(controls);
 #if defined(__NetBSD__)
         int last_level = 0;
         int last_type = 0;
@@ -1239,6 +1238,7 @@ bsock_sendmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
                 rb_str_set_len(controls_str, RSTRING_LEN(controls_str)-last_pad);
 #endif
 	}
+	RB_GC_GUARD(controls);
 #else
 	rb_raise(rb_eNotImpError, "control message for sendmsg is unimplemented");
 #endif
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-06-02  2:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-02  2:25 [PATCH] ext/socket/ancdata.c (bsock_sendmsg_internal): use rb_scan_args Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).