--- IO-AIO/AIO.xs 2018/01/06 01:04:42 1.251 +++ IO-AIO/AIO.xs 2018/07/17 23:20:09 1.257 @@ -6,8 +6,6 @@ #include "perl.h" #include "XSUB.h" -#include "schmorp.h" - #include #include #include @@ -108,6 +106,16 @@ #include "libeio/config.h" +#include "schmorp.h" + +#if HAVE_EVENTFD +# include +#endif + +#if HAVE_TIMERFD +# include +#endif + #if HAVE_RLIMITS #include #include @@ -182,6 +190,10 @@ # define mprotect(addr,len,prot) EIO_ENOSYS () #endif +#if !MREMAP_MAYMOVE +# define mremap(old_address,old_size,new_size,flags,new_address) (errno = ENOSYS, (void *)-1) +#endif + #define FOREIGN_MAGIC PERL_MAGIC_ext static int ecb_cold @@ -701,7 +713,13 @@ sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); } else - SvCUR_set (req->sv2, req->result); + { + SvCUR_set (req->sv2, req->result); + *SvEND (req->sv2) = 0; + SvPOK_only (req->sv2); + } + + SvSETMAGIC (req->sv2); } PUSHs (sv_result); @@ -965,6 +983,23 @@ } } +/*****************************************************************************/ + +static void +ts_set (struct timespec *ts, NV value) +{ + ts->tv_sec = value; + ts->tv_nsec = (value - ts->tv_sec) * 1e9; +} + +static NV +ts_get (const struct timespec *ts) +{ + return ts->tv_sec + ts->tv_nsec * 1e-9; +} + +/*****************************************************************************/ + XS(boot_IO__AIO) ecb_cold; MODULE = IO::AIO PACKAGE = IO::AIO @@ -1074,6 +1109,9 @@ const_iv (MAP_HUGETLB) const_iv (MAP_STACK) + const_iv (MREMAP_MAYMOVE) + const_iv (MREMAP_FIXED) + const_iv (F_DUPFD_CLOEXEC) const_iv (F_OFD_GETLK) @@ -1157,6 +1195,22 @@ const_iv (SPLICE_F_MORE) const_iv (SPLICE_F_GIFT) + const_iv (EFD_CLOEXEC) + const_iv (EFD_NONBLOCK) + const_iv (EFD_SEMAPHORE) + + const_iv (CLOCK_REALTIME) + const_iv (CLOCK_MONOTONIC) + const_iv (CLOCK_BOOTTIME) + const_iv (CLOCK_REALTIME_ALARM) + const_iv (CLOCK_BOOTTIME_ALARM) + + const_iv (TFD_NONBLOCK) + const_iv (TFD_CLOEXEC) + + const_iv (TFD_TIMER_ABSTIME) + const_iv (TFD_TIMER_CANCEL_ON_SET) + /* these are libeio constants, and are independent of gendef0 */ const_eio (SEEK_SET) const_eio (SEEK_CUR) @@ -1417,7 +1471,7 @@ { /* read: check type and grow scalar as necessary */ if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) - svptr = sv_grow (data, len + dataoffset); + svptr = sv_grow (data, len + dataoffset + 1); else if (SvCUR (data) < len + dataoffset) croak ("length + dataoffset outside of scalar, and cannot grow"); } @@ -1821,7 +1875,7 @@ if (length) /* known length, directly read into scalar */ { if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) - svptr = sv_grow (data, length); + svptr = sv_grow (data, length + 1); else if (SvCUR (data) < length) croak ("length outside of scalar, and cannot grow"); else @@ -2012,6 +2066,36 @@ CODE: sv_clear_foreign (scalar); +SV * +mremap (SV *scalar, STRLEN new_length, int flags = 0, IV new_address = 0) + CODE: +{ + MAGIC *mg = mg_findext (scalar, FOREIGN_MAGIC, &mmap_vtbl); + void *new; + + if (!mg || SvPVX (scalar) != mg->mg_ptr) + croak ("IO::AIO::mremap: scalar not mapped by IO::AIO::mmap or improperly modified"); + + new = mremap (mg->mg_ptr, (size_t)mg->mg_obj, new_length, flags, (void *)new_address); + + RETVAL = &PL_sv_no; + + if (new != (void *)-1) + { + RETVAL = new == (void *)mg->mg_ptr + ? newSVpvn ("0 but true", 10) + : &PL_sv_yes; + + mg->mg_ptr = (char *)new; + mg->mg_obj = (SV *)new_length; + + SvPVX (scalar) = mg->mg_ptr; + SvCUR_set (scalar, new_length); + } +} + OUTPUT: + RETVAL + int madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) ALIAS: @@ -2152,6 +2236,79 @@ } } +void +eventfd (unsigned int initval = 0, int flags = 0) + PPCODE: +{ + int fd; +#if HAVE_EVENTFD + fd = eventfd (initval, flags); +#else + fd = (errno = ENOSYS, -1); +#endif + + XPUSHs (newmortalFH (fd, O_RDWR)); +} + +void +timerfd_create (int clockid, int flags = 0) + PPCODE: +{ + int fd; +#if HAVE_TIMERFD + fd = timerfd_create (clockid, flags); +#else + fd = (errno = ENOSYS, -1); +#endif + + XPUSHs (newmortalFH (fd, O_RDWR)); +} + +void +timerfd_settime (SV *fh, int flags, NV interval, NV value) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); +#if HAVE_TIMERFD + int res; + struct itimerspec its, ots; + + ts_set (&its.it_interval, interval); + ts_set (&its.it_value , value); + res = timerfd_settime (fd, flags, &its, &ots); + + if (!res) + { + EXTEND (SP, 2); + PUSHs (newSVnv (ts_get (&ots.it_interval))); + PUSHs (newSVnv (ts_get (&ots.it_value))); + } +#else + errno = ENOSYS; +#endif +} + +void +timerfd_gettime (SV *fh) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); +#if HAVE_TIMERFD + int res; + struct itimerspec ots; + res = timerfd_gettime (fd, &ots); + + if (!res) + { + EXTEND (SP, 2); + PUSHs (newSVnv (ts_get (&ots.it_interval))); + PUSHs (newSVnv (ts_get (&ots.it_value))); + } +#else + errno = ENOSYS; +#endif +} + UV get_fdlimit () CODE: