--- IO-AIO/AIO.xs 2010/05/09 20:49:52 1.167 +++ IO-AIO/AIO.xs 2010/12/01 04:43:12 1.175 @@ -124,7 +124,6 @@ #ifndef POSIX_FADV_NORMAL # define POSIX_FADV_NORMAL 0 -# define NO_FADVISE 1 #endif #ifndef POSIX_FADV_SEQUENTIAL # define POSIX_FADV_SEQUENTIAL 0 @@ -142,6 +141,43 @@ # define POSIX_FADV_DONTNEED 0 #endif +#if !HAVE_POSIX_FADVISE +# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */ +#endif + +#ifndef POSIX_MADV_NORMAL +# define POSIX_MADV_NORMAL 0 +#endif +#ifndef POSIX_MADV_SEQUENTIAL +# define POSIX_MADV_SEQUENTIAL 0 +#endif +#ifndef POSIX_MADV_RANDOM +# define POSIX_MADV_RANDOM 0 +#endif +#ifndef POSIX_MADV_WILLNEED +# define POSIX_MADV_WILLNEED 0 +#endif +#ifndef POSIX_MADV_DONTNEED +# define POSIX_MADV_DONTNEED 0 +#endif + +#if !HAVE_POSIX_MADVISE +# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */ +#endif + +#ifndef PROT_NONE +# define PROT_NONE 0 +#endif +#ifndef PROT_READ +# define PROT_READ 0 +#endif +#ifndef PROT_WRITE +# define PROT_READ 0 +#endif +#ifndef PROT_EXEC +# define PROT_EXEC 0 +#endif + #ifndef ST_NODEV # define ST_NODEV 0 #endif @@ -173,13 +209,6 @@ # 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 @@ -608,6 +637,12 @@ #if !_POSIX_MAPPED_FILES # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) # define munmap(addr,length) (errno = ENOSYS, -1) +# define mprotect(addr,len,prot) (errno = ENOSYS, -1) +# define PROT_NONE 0 +# define PROT_WRITE 0 +# define MAP_PRIVATE 0 +# define MAP_SHARED 0 +# define MAP_FIXED 0 #endif #define MMAP_MAGIC PERL_MAGIC_ext @@ -622,8 +657,11 @@ mg->mg_obj = 0; /* just in case */ SvREADONLY_off (sv); + + if (SvPVX (sv) != mg->mg_ptr) + croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); + SvCUR_set (sv, 0); - SvLEN_set (sv, 0); SvPVX (sv) = 0; SvOK_off (sv); @@ -698,6 +736,12 @@ const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) + const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) + const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) + const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) + const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) + const_iv (ST_RDONLY) const_iv (ST_NOSUID) const_iv (ST_NODEV) @@ -711,8 +755,8 @@ const_iv (ST_NODIRATIME) const_iv (ST_RELATIME) - const_iv (PROT_EXEC) const_iv (PROT_NONE) + const_iv (PROT_EXEC) const_iv (PROT_READ) const_iv (PROT_WRITE) @@ -728,8 +772,8 @@ const_iv (MAP_POPULATE) const_iv (MAP_NONBLOCK) - const_iv (MCL_FUTURE) - const_iv (MCL_CURRENT) + const_eio (MCL_FUTURE) + const_eio (MCL_CURRENT) const_eio (MS_ASYNC) const_eio (MS_INVALIDATE) @@ -813,7 +857,6 @@ void aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ PPCODE: { dREQ; @@ -829,7 +872,6 @@ void aio_fsync (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ ALIAS: aio_fsync = EIO_FSYNC aio_fdatasync = EIO_FDATASYNC @@ -847,7 +889,6 @@ void aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$$;$ PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -865,7 +906,6 @@ void aio_close (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ PPCODE: { static int close_pipe = -1; /* dummy fd to close fds via dup2 */ @@ -897,7 +937,6 @@ ALIAS: aio_read = EIO_READ aio_write = EIO_WRITE - PROTOTYPE: $$$$$;$ PPCODE: { STRLEN svlen; @@ -948,7 +987,6 @@ void aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) - PROTOTYPE: $$;$ PPCODE: { SV *data; @@ -963,7 +1001,6 @@ void aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$$;$ PPCODE: { int ifd = s_fileno_croak (in_fh , 0); @@ -983,7 +1020,6 @@ void aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -1194,12 +1230,11 @@ ALIAS: aio_mtouch = EIO_MTOUCH aio_msync = EIO_MSYNC - PROTOTYPE: $$$$;$ PPCODE: { STRLEN svlen; - UV len = SvUV (length); char *svptr = SvPVbyte (data, svlen); + UV len = SvUV (length); if (offset < 0) offset += svlen; @@ -1214,9 +1249,9 @@ dREQ; req->type = ix; - req->size = len; req->sv2 = SvREFCNT_inc (data); req->ptr2 = (char *)svptr + offset; + req->size = len; req->int1 = flags; REQ_SEND; @@ -1224,6 +1259,47 @@ } void +aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef) + PPCODE: +{ + STRLEN svlen; + char *svptr = SvPVbyte (data, svlen); + UV len = SvUV (length); + + 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 = EIO_MLOCK; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = (char *)svptr + offset; + req->size = len; + + REQ_SEND; + } +} + +void +aio_mlockall (IV flags, SV *callback=&PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_MLOCKALL; + req->int1 = flags; + + REQ_SEND; +} + +void aio_busy (double delay, SV *callback=&PL_sv_undef) PPCODE: { @@ -1237,7 +1313,6 @@ void aio_group (SV *callback=&PL_sv_undef) - PROTOTYPE: ;$ PPCODE: { dREQ; @@ -1264,7 +1339,6 @@ int aioreq_pri (int pri = 0) - PROTOTYPE: ;$ CODE: RETVAL = next_pri; if (items > 0) @@ -1286,7 +1360,6 @@ void flush () - PROTOTYPE: CODE: while (eio_nreqs ()) { @@ -1295,8 +1368,7 @@ } int -poll() - PROTOTYPE: +poll () CODE: poll_wait (); RETVAL = poll_cb (); @@ -1304,15 +1376,14 @@ RETVAL int -poll_fileno() - PROTOTYPE: +poll_fileno () CODE: RETVAL = s_epipe_fd (&respipe); OUTPUT: RETVAL int -poll_cb(...) +poll_cb (...) PROTOTYPE: CODE: RETVAL = poll_cb (); @@ -1320,38 +1391,33 @@ RETVAL void -poll_wait() - PROTOTYPE: +poll_wait () CODE: poll_wait (); int -nreqs() - PROTOTYPE: +nreqs () CODE: RETVAL = eio_nreqs (); OUTPUT: RETVAL int -nready() - PROTOTYPE: +nready () CODE: RETVAL = eio_nready (); OUTPUT: RETVAL int -npending() - PROTOTYPE: +npending () CODE: RETVAL = eio_npending (); OUTPUT: RETVAL int -nthreads() - PROTOTYPE: +nthreads () CODE: RETVAL = eio_nthreads (); OUTPUT: @@ -1359,19 +1425,13 @@ int fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) - PROTOTYPE: $$$$ CODE: -#if _XOPEN_SOURCE >= 600 && !NO_FADVISE RETVAL = posix_fadvise (fh, offset, length, advice); -#else - RETVAL = errno = ENOSYS; /* yes, this is actually correct */ -#endif OUTPUT: RETVAL ssize_t sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) - PROTOTYPE: $$$$ CODE: RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); OUTPUT: @@ -1379,7 +1439,6 @@ void mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) - PROTOTYPE: $$$$$;$ PPCODE: sv_unmagic (scalar, MMAP_MAGIC); { @@ -1388,14 +1447,20 @@ if (addr == (void *)-1) XSRETURN_NO; + sv_force_normal (scalar); + /* 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); + if (SvLEN (scalar)) + Safefree (SvPVX (scalar)); + SvPVX (scalar) = (char *)addr; SvCUR_set (scalar, length); SvLEN_set (scalar, 0); @@ -1406,30 +1471,70 @@ void munmap (SV *scalar) - PROTOTYPE: $ CODE: sv_unmagic (scalar, MMAP_MAGIC); int -mlockall (int flags) - PROTOTYPE: $ +madvise (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) + ALIAS: + mprotect = 1 CODE: +{ + STRLEN svlen; + void *addr = SvPVbyte (scalar, svlen); + size_t len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + addr = (void *)(((intptr_t)addr) + offset); + eio_page_align (&addr, &len); + + switch (ix) + { + case 0: RETVAL = posix_madvise (addr, len, advice_or_prot); break; + case 1: RETVAL = mprotect (addr, len, advice_or_prot); break; + } +} + OUTPUT: + RETVAL + +int +munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) + CODE: +{ + STRLEN svlen; + void *addr = SvPVbyte (scalar, svlen); + size_t len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + addr = (void *)(((intptr_t)addr) + offset); + eio_page_align (&addr, &len); #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); + RETVAL = munlock (addr, len); #else - RETVAL = -1; - errno = ENOSYS; + RETVAL = ((errno = ENOSYS), -1); #endif +} OUTPUT: RETVAL int munlockall () - PROTOTYPE: CODE: #if _POSIX_MEMLOCK munlockall ();