ext/io/epoll/epoll.c in io-epoll-0.0.1 vs ext/io/epoll/epoll.c in io-epoll-0.0.2

- old
+ new

@@ -87,28 +87,30 @@ struct Epoll *ptr = get_epoll(self); struct epoll_event ev; VALUE flag; VALUE io; VALUE events; + rb_io_t *fptr; int fd; switch (rb_scan_args(argc, argv, "21", &flag, &io, &events)) { case 2: - if (FIX2INT(flag) != EPOLL_CTL_DEL) { + if (FIX2INT(flag) != EPOLL_CTL_DEL) rb_raise(rb_eArgError, "too few argument for CTL_ADD or CTL_MOD"); - } break; case 3: - if (FIX2INT(flag) != EPOLL_CTL_ADD && FIX2INT(flag) != EPOLL_CTL_MOD) { + if (FIX2INT(flag) != EPOLL_CTL_ADD && FIX2INT(flag) != EPOLL_CTL_MOD) rb_raise(rb_eArgError, "too many argument for CTL_DEL"); - } - ev.events = FIX2INT(events); + if ((FIX2LONG(events) & (EPOLLIN|EPOLLPRI|EPOLLRDHUP|EPOLLOUT|EPOLLET|EPOLLONESHOT)) == 0) + rb_raise(rb_eIOError, "undefined events"); + ev.events = FIX2LONG(events); ev.data.ptr = (void*)io; break; } - fd = RFILE(io)->fptr->fd; + GetOpenFile(rb_io_get_io(io), fptr); + fd = fptr->fd; if (epoll_ctl(ptr->epfd, FIX2INT(flag), fd, &ev) == -1) { char buf[128]; sprintf(buf, "epoll_ctl() was failed(epfd:%d, fd:%d)", ptr->epfd, fd); rb_sys_fail(buf); @@ -146,19 +148,22 @@ static VALUE rb_epoll_wait(int argc, VALUE *argv, VALUE self) { struct Epoll *ptr = get_epoll(self); VALUE ready_evlist; + VALUE cEvent; VALUE event; struct epoll_event *evlist; int i, ready; int timeout = -1; struct epoll_wait_args data; - if (argc == 1) { + if (argc == 1) timeout = FIX2INT(argv[0]); - } + if (ptr->ev_len <= 0) + rb_raise(rb_eIOError, "empty interest list"); + evlist = ruby_xmalloc(ptr->ev_len * sizeof(struct epoll_event)); data.ptr = ptr; data.evlist = evlist; data.timeout = timeout; @@ -174,13 +179,14 @@ rb_sys_fail("epoll_wait() was failed"); } } ready_evlist = rb_ary_new_capa(ready); + cEvent = rb_path2class("IO::Epoll::Event"); for (i = 0; i < ready; i++) { - event = rb_obj_alloc(rb_path2class("IO::Epoll::Event")); + event = rb_obj_alloc(cEvent); RSTRUCT_SET(event, 0, (VALUE) evlist[i].data.ptr); - RSTRUCT_SET(event, 1, INT2FIX(evlist[i].events)); + RSTRUCT_SET(event, 1, LONG2FIX(evlist[i].events)); rb_ary_store(ready_evlist, i, event); } ruby_xfree(evlist); return ready_evlist; }