Avoid Boxing/Unboxing of file descriptors; ref
MODIFY
mrbgems/mruby-io/include/mruby/ext/io.h
mrbgems/mruby-io/include/mruby/ext/io.h
@@ -30,7 +30,7 @@ struct mrb_io {
#define E_IO_ERROR (mrb_class_get(mrb, "IOError"))
#define E_EOF_ERROR (mrb_class_get(mrb, "EOFError"))
-mrb_value mrb_io_fileno(mrb_state *mrb, mrb_value io);
+int mrb_io_fileno(mrb_state *mrb, mrb_value io);
#if defined(__cplusplus)
} /* extern "C" { */

MODIFY
mrbgems/mruby-io/src/file.c
mrbgems/mruby-io/src/file.c
@@ -347,7 +347,7 @@ mrb_file_mtime(mrb_state *mrb, mrb_value self)
int fd;
obj = mrb_obj_value(mrb_class_get(mrb, "Time"));
- fd = (int)mrb_fixnum(mrb_io_fileno(mrb, self));
+ fd = mrb_io_fileno(mrb, self);
if (fstat(fd, &st) == -1)
return mrb_false_value();
return mrb_funcall(mrb, obj, "at", 1, mrb_fixnum_value(st.st_mtime));
@@ -363,7 +363,7 @@ mrb_file_flock(mrb_state *mrb, mrb_value self)
int fd;
mrb_get_args(mrb, "i", &operation);
- fd = (int)mrb_fixnum(mrb_io_fileno(mrb, self));
+ fd = mrb_io_fileno(mrb, self);
while (flock(fd, (int)operation) == -1) {
switch (errno) {
@@ -393,7 +393,7 @@ mrb_file_size(mrb_state *mrb, mrb_value self)
mrb_stat st;
int fd;
- fd = (int)mrb_fixnum(mrb_io_fileno(mrb, self));
+ fd = mrb_io_fileno(mrb, self);
if (mrb_fstat(fd, &st) == -1) {
mrb_raise(mrb, E_RUNTIME_ERROR, "fstat failed");
}
@@ -446,7 +446,7 @@ mrb_file_truncate(mrb_state *mrb, mrb_value self)
int64_t length;
mrb_value lenv;
- fd = (int)mrb_fixnum(mrb_io_fileno(mrb, self));
+ fd = mrb_io_fileno(mrb, self);
mrb_get_args(mrb, "o", &lenv);
switch (mrb_type(lenv)) {
#ifndef MRB_WITHOUT_FLOAT

MODIFY
mrbgems/mruby-io/src/io.c
mrbgems/mruby-io/src/io.c
@@ -281,7 +281,7 @@ option_to_fd(mrb_state *mrb, mrb_value hash, const char *key)
switch (mrb_type(opt)) {
case MRB_TT_DATA: /* IO */
- return (int)mrb_fixnum(mrb_io_fileno(mrb, opt));
+ return mrb_io_fileno(mrb, opt);
case MRB_TT_FIXNUM:
return (int)mrb_fixnum(opt);
default:
@@ -1190,12 +1190,19 @@ retry:
return result;
}
-mrb_value
+int
mrb_io_fileno(mrb_state *mrb, mrb_value io)
{
struct mrb_io *fptr;
fptr = io_get_open_fptr(mrb, io);
- return mrb_fixnum_value(fptr->fd);
+ return fptr->fd;
+}
+
+static mrb_value
+mrb_io_fileno_m(mrb_state *mrb, mrb_value io)
+{
+ int fd = mrb_io_fileno(mrb, io);
+ return mrb_fixnum_value(fd);
}
mrb_value
@@ -1332,7 +1339,7 @@ mrb_init_io(mrb_state *mrb)
mrb_define_method(mrb, io, "close_on_exec?", mrb_io_close_on_exec_p, MRB_ARGS_NONE());
mrb_define_method(mrb, io, "closed?", mrb_io_closed, MRB_ARGS_NONE()); /* 15.2.20.5.2 */
mrb_define_method(mrb, io, "pid", mrb_io_pid, MRB_ARGS_NONE()); /* 15.2.20.5.2 */
- mrb_define_method(mrb, io, "fileno", mrb_io_fileno, MRB_ARGS_NONE());
+ mrb_define_method(mrb, io, "fileno", mrb_io_fileno_m, MRB_ARGS_NONE());
mrb_define_class_method(mrb, io, "_bufread", io_bufread, MRB_ARGS_REQ(2));
}

Avoid Boxing/Unboxing of file descriptors; ref #4872