diff options
author | Eric Wong <e@80x24.org> | 2017-01-05 00:49:02 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2017-01-05 08:39:07 +0000 |
commit | 56c4eb8a98957da88ef8efd42c6e39eaf0a1b69d (patch) | |
tree | 087aebe4bd96b3b997b07f78cafc6d430ffd9eae /ext/sleepy_penguin | |
parent | 1b355863e42164110a074a9313966ffebf880fb7 (diff) | |
download | sleepy_penguin-56c4eb8a98957da88ef8efd42c6e39eaf0a1b69d.tar.gz |
Keyword args allows for a smaller interface for common use, while retaining the capability to use offsets for both input and output. The current (2.4) Ruby C API for keyword args is slow and creates too many garbage objects. As with our splice wrapper, use a pure Ruby wrapper around an internal C function.
Diffstat (limited to 'ext/sleepy_penguin')
-rw-r--r-- | ext/sleepy_penguin/cfr.c | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/ext/sleepy_penguin/cfr.c b/ext/sleepy_penguin/cfr.c index e90d959..acece4f 100644 --- a/ext/sleepy_penguin/cfr.c +++ b/ext/sleepy_penguin/cfr.c @@ -34,38 +34,37 @@ static void *nogvl_cfr(void *ptr) a->fd_out, a->off_out, a->len, a->flags); } -static VALUE rb_cfr(int argc, VALUE *argv, VALUE mod) +/* :nodoc: */ +static VALUE rb_sp_cfr(VALUE mod, VALUE io_in, VALUE off_in, + VALUE io_out, VALUE off_out, + VALUE len, VALUE flags) { - off_t i, o; - VALUE io_in, off_in, io_out, off_out, len, flags; - ssize_t bytes; + off_t i = 0, o = 0; struct copy_args a; - - rb_scan_args(argc, argv, "51", - &io_in, &off_in, &io_out, &off_out, &len, &flags); + ssize_t bytes; a.off_in = NIL_P(off_in) ? NULL : (i = NUM2OFFT(off_in), &i); a.off_out = NIL_P(off_out) ? NULL : (o = NUM2OFFT(off_out), &o); a.len = NUM2SIZET(len); - a.flags = NIL_P(flags) ? 0 : NUM2UINT(flags); + a.flags = NUM2UINT(flags); -again: - a.fd_in = rb_sp_fileno(io_in); - a.fd_out = rb_sp_fileno(io_out); - bytes = (ssize_t)IO_RUN(nogvl_cfr, &a); - if (bytes < 0) { - if (errno == EINTR) - goto again; - rb_sys_fail("copy_file_range"); - } else if (bytes == 0) { - rb_eof_error(); + for (;;) { + a.fd_in = rb_sp_fileno(io_in); + a.fd_out = rb_sp_fileno(io_out); + bytes = (ssize_t)IO_RUN(nogvl_cfr, &a); + if (bytes < 0) { + switch (errno) { + case EINTR: continue; + default: rb_sys_fail("copy_file_range"); + } + } + return SSIZET2NUM(bytes); } - return SSIZET2NUM(bytes); } void sleepy_penguin_init_cfr(void) { VALUE mod = rb_define_module("SleepyPenguin"); - rb_define_singleton_method(mod, "copy_file_range", rb_cfr, -1); + rb_define_singleton_method(mod, "__cfr", rb_sp_cfr, 6); } |