--- IO-AIO/AIO.xs 2009/07/15 01:36:04 1.153 +++ IO-AIO/AIO.xs 2010/01/10 23:44:02 1.163 @@ -13,10 +13,15 @@ #include #include #include +#include #include #include #include +#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES +# include +#endif + /* perl namespace pollution */ #undef VERSION @@ -101,13 +106,11 @@ /*****************************************************************************/ -static HV *stash; typedef SV SV8; /* byte-sv, used for argument-checking */ typedef int aio_rfd; /* read file desriptor */ typedef int aio_wfd; /* write file descriptor */ -#define AIO_REQ_KLASS "IO::AIO::REQ" -#define AIO_GRP_KLASS "IO::AIO::GRP" +static HV *aio_stash, *aio_req_stash, *aio_grp_stash; #define EIO_REQ_MEMBERS \ SV *callback; \ @@ -123,27 +126,87 @@ # define POSIX_FADV_NORMAL 0 # define NO_FADVISE 1 #endif - #ifndef POSIX_FADV_SEQUENTIAL # define POSIX_FADV_SEQUENTIAL 0 #endif - #ifndef POSIX_FADV_RANDOM # define POSIX_FADV_RANDOM 0 #endif - #ifndef POSIX_FADV_NOREUSE # define POSIX_FADV_NOREUSE 0 #endif - #ifndef POSIX_FADV_WILLNEED # define POSIX_FADV_WILLNEED 0 #endif - #ifndef POSIX_FADV_DONTNEED # define POSIX_FADV_DONTNEED 0 #endif +#ifndef ST_NODEV +# define ST_NODEV 0 +#endif +#ifndef ST_NOEXEC +# define ST_NOEXEC 0 +#endif +#ifndef ST_SYNCHRONOUS +# define ST_SYNCHRONOUS 0 +#endif +#ifndef ST_MANDLOCK +# define ST_MANDLOCK 0 +#endif +#ifndef ST_WRITE +# define ST_WRITE 0 +#endif +#ifndef ST_APPEND +# define ST_APPEND 0 +#endif +#ifndef ST_IMMUTABLE +# define ST_IMMUTABLE 0 +#endif +#ifndef ST_NOATIME +# define ST_NOATIME 0 +#endif +#ifndef ST_NODIRATIME +# define ST_NODIRATIME 0 +#endif +#ifndef ST_RELATIME +# define ST_RELATIME 0 +#endif + +#ifndef MCL_CURRENT +# define MCL_CURRENT 0 +#endif +#ifndef MCL_FUTURE +# define MCL_FUTURE 0 +#endif + +#ifndef MAP_ANONYMOUS +# ifdef MAP_ANON +# define MAP_ANONYMOUS MAP_ANON +# else +# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ +# endif +#endif +#ifndef MAP_HUGETLB +# define MAP_HUGETLB 0 +#endif +#ifndef MAP_LOCKED +# define MAP_LOCKED 0 +#endif +#ifndef MAP_NORESERVE +# define MAP_NORESERVE 0 +#endif +#ifndef MAP_POPULATE +# define MAP_POPULATE 0 +#endif +#ifndef MAP_NONBLOCK +# define MAP_NONBLOCK 0 +#endif + +#ifndef PAGESIZE +# define PAGESIZE sysconf (_SC_PAGESIZE) +#endif + static int req_invoke (eio_req *req); #define EIO_FINISH(req) req_invoke (req) static void req_destroy (eio_req *grp); @@ -162,7 +225,6 @@ static int next_pri = EIO_PRI_DEFAULT; static int max_outstanding; -static int respipe_osf [2]; static s_epipe respipe; static void req_destroy (aio_req req); @@ -181,7 +243,7 @@ } /* must be called at most once */ -static SV *req_sv (aio_req req, const char *klass) +static SV *req_sv (aio_req req, HV *stash) { if (!req->self) { @@ -189,15 +251,18 @@ sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); } - return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); + return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); } static aio_req SvAIO_REQ (SV *sv) { MAGIC *mg; - if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) - croak ("object of class " AIO_REQ_KLASS " expected"); + if (!SvROK (sv) + || (SvSTASH (SvRV (sv)) != aio_grp_stash + && SvSTASH (SvRV (sv)) != aio_req_stash + && !sv_derived_from (sv, "IO::AIO::REQ"))) + croak ("object of class IO::AIO::REQ expected"); mg = mg_find (SvRV (sv), PERL_MAGIC_ext); @@ -213,7 +278,7 @@ ENTER; SAVETMPS; PUSHMARK (SP); - XPUSHs (req_sv (grp, AIO_GRP_KLASS)); + XPUSHs (req_sv (grp, aio_grp_stash)); PUTBACK; call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); SPAGAIN; @@ -329,7 +394,7 @@ int symlen; symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); - gv_init (gv, stash, sym, symlen, 0); + gv_init (gv, aio_stash, sym, symlen, 0); symlen = snprintf ( sym, @@ -347,6 +412,36 @@ } break; + case EIO_STATVFS: + case EIO_FSTATVFS: + { + SV *rv = &PL_sv_undef; + + if (req->result >= 0) + { + EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); + HV *hv = newHV (); + + rv = sv_2mortal (newRV_noinc ((SV *)hv)); + + hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); + hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0); + hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0); + hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0); + hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0); + hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); + hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); + hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); + hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); + hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); + hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); + } + + PUSHs (rv); + } + + break; + case EIO_GROUP: req->int1 = 2; /* mark group as finished */ @@ -446,10 +541,7 @@ create_respipe (void) { if (s_epipe_renew (&respipe)) - croak ("unable to initialize result pipe"); - - respipe_osf [0] = S_TO_SOCKET (respipe.fd [0]); - respipe_osf [1] = S_TO_SOCKET (respipe.fd [1]); + croak ("IO::AIO: unable to initialize result pipe"); } static void poll_wait (void) @@ -492,6 +584,39 @@ create_respipe (); } +/*****************************************************************************/ + +#if !_POSIX_MAPPED_FILES +# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) +# define munmap(addr,length) (errno = ENOSYS, -1) +#endif + +#define MMAP_MAGIC PERL_MAGIC_ext + +static int +mmap_free (pTHX_ SV *sv, MAGIC *mg) +{ + int old_errno = errno; + munmap (mg->mg_ptr, (size_t)mg->mg_obj); + errno = old_errno; + + mg->mg_obj = 0; /* just in case */ + + SvREADONLY_off (sv); + SvCUR_set (sv, 0); + SvLEN_set (sv, 0); + SvPVX (sv) = 0; + SvOK_off (sv); + + return 0; +} + +static MGVTBL mmap_vtbl = { + 0, 0, 0, 0, mmap_free +}; + +/*****************************************************************************/ + static SV * get_cb (SV *cb_sv) { @@ -520,7 +645,7 @@ SPAGAIN; \ \ if (GIMME_V != G_VOID) \ - XPUSHs (req_sv (req, AIO_REQ_KLASS)); + XPUSHs (req_sv (req, aio_req_stash)); MODULE = IO::AIO PACKAGE = IO::AIO @@ -532,23 +657,63 @@ const char *name; IV iv; } *civ, const_iv[] = { -# define const_iv(name, value) { # name, (IV) value }, +# define const_niv(name, value) { # name, (IV) value }, +# define const_iv(name) { # name, (IV) name }, # define const_eio(name) { # name, (IV) EIO_ ## name }, - const_iv (EXDEV , EXDEV) - const_iv (ENOSYS , ENOSYS) - const_iv (O_RDONLY, O_RDONLY) - const_iv (O_WRONLY, O_WRONLY) - const_iv (O_CREAT , O_CREAT) - const_iv (O_TRUNC , O_TRUNC) + const_iv (EXDEV) + const_iv (ENOSYS) + const_iv (O_RDONLY) + const_iv (O_WRONLY) + const_iv (O_CREAT) + const_iv (O_TRUNC) #ifndef _WIN32 - const_iv (S_IFIFO , S_IFIFO) + const_iv (S_IFIFO) #endif - const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) - const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) - const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) - const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) - const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) - const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) + const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) + const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) + const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) + const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) + const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + + const_iv (ST_RDONLY) + const_iv (ST_NOSUID) + const_iv (ST_NODEV) + const_iv (ST_NOEXEC) + const_iv (ST_SYNCHRONOUS) + const_iv (ST_MANDLOCK) + const_iv (ST_WRITE) + const_iv (ST_APPEND) + const_iv (ST_IMMUTABLE) + const_iv (ST_NOATIME) + const_iv (ST_NODIRATIME) + const_iv (ST_RELATIME) + + const_iv (PROT_EXEC) + const_iv (PROT_NONE) + const_iv (PROT_READ) + const_iv (PROT_WRITE) + + /*const_iv (MAP_FIXED)*/ + const_iv (MAP_PRIVATE) + const_iv (MAP_SHARED) + const_iv (MAP_ANONYMOUS) + + /* linuxish */ + const_iv (MAP_HUGETLB) + const_iv (MAP_LOCKED) + const_iv (MAP_NORESERVE) + const_iv (MAP_POPULATE) + const_iv (MAP_NONBLOCK) + + const_iv (MCL_FUTURE) + const_iv (MCL_CURRENT) + + const_eio (MS_ASYNC) + const_eio (MS_INVALIDATE) + const_eio (MS_SYNC) + + const_eio (MT_MODIFY) const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) const_eio (SYNC_FILE_RANGE_WRITE) @@ -570,10 +735,14 @@ const_eio (DT_WHT) }; - stash = gv_stashpv ("IO::AIO", 1); + aio_stash = gv_stashpv ("IO::AIO" , 1); + aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); + aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) - newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); + newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); + + newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); create_respipe (); @@ -810,8 +979,9 @@ void aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) ALIAS: - aio_stat = EIO_STAT - aio_lstat = EIO_LSTAT + aio_stat = EIO_STAT + aio_lstat = EIO_LSTAT + aio_statvfs = EIO_STATVFS PPCODE: { dREQ; @@ -825,7 +995,7 @@ } else { - req->type = EIO_FSTAT; + req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT; req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); } @@ -998,6 +1168,40 @@ } void +aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) + ALIAS: + aio_mtouch = EIO_MTOUCH + aio_msync = EIO_MSYNC + PROTOTYPE: $$$$;$ + PPCODE: +{ + STRLEN svlen; + UV len = SvUV (length); + char *svptr = SvPVbyte (data, svlen); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + { + dREQ; + + req->type = ix; + req->size = len; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = (char *)svptr + offset; + req->int1 = flags; + + REQ_SEND; + } +} + +void aio_busy (double delay, SV *callback=&PL_sv_undef) PPCODE: { @@ -1019,7 +1223,7 @@ req->type = EIO_GROUP; req_submit (req); - XPUSHs (req_sv (req, AIO_GRP_KLASS)); + XPUSHs (req_sv (req, aio_grp_stash)); } void @@ -1138,7 +1342,7 @@ #if _XOPEN_SOURCE >= 600 && !NO_FADVISE RETVAL = posix_fadvise (fh, offset, length, advice); #else - RETVAL = errno = ENOSYS; + RETVAL = errno = ENOSYS; /* yes, this is actually correct */ #endif OUTPUT: RETVAL @@ -1147,7 +1351,72 @@ sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) PROTOTYPE: $$$$ CODE: - eio_sendfile_sync (ofh, ifh, offset, count); + RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); + OUTPUT: + RETVAL + +void +mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) + PROTOTYPE: $$$$$;$ + PPCODE: + sv_unmagic (scalar, MMAP_MAGIC); +{ + int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; + void *addr = (void *)mmap (0, length, prot, flags, fd, offset); + if (addr == (void *)-1) + XSRETURN_NO; + + /* we store the length in mg_obj, as namlen is I32 :/ */ + sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) + ->mg_obj = (SV *)length; + + SvUPGRADE (scalar, SVt_PV); /* nop... */ + if (!(prot & PROT_WRITE)) + SvREADONLY_on (scalar); + + SvPVX (scalar) = (char *)addr; + SvCUR_set (scalar, length); + SvLEN_set (scalar, 0); + SvPOK_only (scalar); + + XSRETURN_YES; +} + +void +munmap (SV *scalar) + PROTOTYPE: $ + CODE: + sv_unmagic (scalar, MMAP_MAGIC); + +int +mlockall (int flags) + PROTOTYPE: $ + CODE: +#if _POSIX_MEMLOCK +#if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 + extern int mallopt (int, int); + mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ +#endif + mlockall (flags); +#else + RETVAL = -1; + errno = ENOSYS; +#endif + OUTPUT: + RETVAL + +int +munlockall () + PROTOTYPE: + CODE: +#if _POSIX_MEMLOCK + munlockall (); +#else + RETVAL = -1; + errno = ENOSYS; +#endif + OUTPUT: + RETVAL void _on_next_submit (SV *cb) CODE: