--- IO-AIO/AIO.xs 2010/01/10 23:44:02 1.163 +++ IO-AIO/AIO.xs 2011/07/25 16:51:07 1.193 @@ -13,73 +13,67 @@ #include #include #include -#include #include #include #include -#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES +#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES # include #endif /* perl namespace pollution */ #undef VERSION +/* perl stupidly overrides readdir and maybe others */ +/* with thread-unsafe versions, imagine that :( */ +#undef readdir +#undef opendir +#undef closedir + #ifdef _WIN32 -# define EIO_STRUCT_DIRENT Direntry_t -# undef malloc -# undef free - -// perl overrides all those nice win32 functions -# undef open -# undef read -# undef write -# undef send -# undef recv -# undef stat -# undef fstat -# define lstat stat -# undef truncate -# undef ftruncate -# undef open -# undef close -# undef unlink -# undef rmdir -# undef rename -# undef lseek - -# define chown(a,b,c) (errno = ENOSYS, -1) -# define fchown(a,b,c) (errno = ENOSYS, -1) -# define fchmod(a,b) (errno = ENOSYS, -1) -# define symlink(a,b) (errno = ENOSYS, -1) -# define readlink(a,b,c) (errno = ENOSYS, -1) -# define mknod(a,b,c) (errno = ENOSYS, -1) -# define truncate(a,b) (errno = ENOSYS, -1) -# define ftruncate(fd,o) chsize ((fd), (o)) -# define fsync(fd) _commit (fd) -# define opendir(fd) (errno = ENOSYS, 0) -# define readdir(fd) (errno = ENOSYS, -1) -# define closedir(fd) (errno = ENOSYS, -1) -# define mkdir(a,b) mkdir (a) + // perl overrides all those nice libc functions + + #undef malloc + #undef free + #undef open + #undef read + #undef write + #undef send + #undef recv + #undef stat + #undef lstat + #undef fstat + #undef truncate + #undef ftruncate + #undef open + #undef link + #undef close + #undef unlink + #undef mkdir + #undef rmdir + #undef rename + #undef lseek + #undef opendir + #undef readdir + #undef closedir + #undef chmod + #undef fchmod + #undef dup + #undef dup2 + #undef abort + #undef pipe #else -# include -# include -# include -# include -# include -# define EIO_STRUCT_DIRENT struct dirent + #include + #include + #include + #include + #include #endif -/* perl stupidly overrides readdir and maybe others */ -/* with thread-unsafe versions, imagine that :( */ -#undef readdir -#undef opendir -#undef closedir - #define EIO_STRUCT_STAT Stat_t /* use NV for 32 bit perls as it allows larger offsets */ @@ -120,11 +114,64 @@ #define EIO_NO_WRAPPERS 1 +#include "libeio/config.h" #include "libeio/eio.h" +static int req_invoke (eio_req *req); +#define EIO_FINISH(req) req_invoke (req) +static void req_destroy (eio_req *grp); +#define EIO_DESTROY(req) req_destroy (req) + +#include "libeio/eio.c" + +/* Linux/others */ +#ifndef O_ASYNC +# define O_ASYNC 0 +#endif +#ifndef O_DIRECT +# define O_DIRECT 0 +#endif +#ifndef O_NOATIME +# define O_NOATIME 0 +#endif + +/* POSIX */ +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif +#ifndef O_NOFOLLOW +# define O_NOFOLLOW 0 +#endif +#ifndef O_NOCTTY +# define O_NOCTTY 0 +#endif +#ifndef O_NONBLOCK +# define O_NONBLOCK 0 +#endif +#ifndef O_EXEC +# define O_EXEC 0 +#endif +#ifndef O_SEARCH +# define O_SEARCH 0 +#endif +#ifndef O_DIRECTORY +# define O_DIRECTORY 0 +#endif +#ifndef O_DSYNC +# define O_DSYNC 0 +#endif +#ifndef O_RSYNC +# define O_RSYNC 0 +#endif +#ifndef O_SYNC +# define O_SYNC 0 +#endif +#ifndef O_TTY_INIT +# define O_TTY_INIT 0 +#endif + #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 +189,49 @@ # 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_RDONLY +# define ST_RDONLY 0 +#endif +#ifndef ST_NOSUID +# define ST_NOSUID 0 +#endif #ifndef ST_NODEV # define ST_NODEV 0 #endif @@ -173,11 +263,29 @@ # define ST_RELATIME 0 #endif -#ifndef MCL_CURRENT -# define MCL_CURRENT 0 +#ifndef S_IFIFO +# define S_IFIFO 0 +#endif +#ifndef S_IFCHR +# define S_IFCHR 0 +#endif +#ifndef S_IFBLK +# define S_IFBLK 0 +#endif +#ifndef S_IFLNK +# define S_IFLNK 0 +#endif +#ifndef S_IFREG +# define S_IFREG 0 +#endif +#ifndef S_IFDIR +# define S_IFDIR 0 #endif -#ifndef MCL_FUTURE -# define MCL_FUTURE 0 +#ifndef S_IFWHT +# define S_IFWHT 0 +#endif +#ifndef S_IFSOCK +# define S_IFSOCK 0 #endif #ifndef MAP_ANONYMOUS @@ -203,21 +311,24 @@ # define MAP_NONBLOCK 0 #endif +#ifndef makedev +# define makedev(maj,min) (((maj) << 8) | (min)) +#endif +#ifndef major +# define major(dev) ((dev) >> 8) +#endif +#ifndef minor +# define minor(dev) ((dev) & 0xff) +#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); -#define EIO_DESTROY(req) req_destroy (req) - enum { FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ }; -#include "libeio/eio.c" - typedef eio_req *aio_req; typedef eio_req *aio_req_ornot; @@ -306,18 +417,33 @@ static int req_invoke (eio_req *req) { - dSP; - if (req->flags & FLAG_SV2_RO_OFF) SvREADONLY_off (req->sv2); if (!EIO_CANCELLED (req) && req->callback) { + dSP; + static SV *sv_result_cache; /* caches the result integer SV */ + SV *sv_result; + ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 1); + /* do not recreate the result IV from scratch each time */ + if (expect_true (sv_result_cache)) + { + sv_result = sv_result_cache; sv_result_cache = 0; + SvIV_set (sv_result, req->result); + SvIOK_only (sv_result); + } + else + { + sv_result = newSViv (req->result); + SvREADONLY_on (sv_result); + } + switch (req->type) { case EIO_READDIR: @@ -417,6 +543,7 @@ { SV *rv = &PL_sv_undef; +#ifndef _WIN32 if (req->result >= 0) { EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); @@ -436,6 +563,7 @@ hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); } +#endif PUSHs (rv); } @@ -461,6 +589,7 @@ break; case EIO_READLINK: + case EIO_REALPATH: if (req->result > 0) PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); break; @@ -470,8 +599,9 @@ case EIO_FSTAT: PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; PL_laststatval = req->result; + /* if compilation fails here then perl's Stat_t is not struct _stati64 */ PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); break; case EIO_READ: @@ -480,17 +610,17 @@ *SvEND (req->sv2) = 0; SvPOK_only (req->sv2); SvSETMAGIC (req->sv2); - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); } break; - case EIO_DUP2: + case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */ if (req->result > 0) - req->result = 0; + SvIV_set (sv_result, 0); /* FALLTHROUGH */ default: - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); break; } @@ -500,6 +630,11 @@ call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); SPAGAIN; + if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) + SvREFCNT_dec (sv_result); + else + sv_result_cache = sv_result; + FREETMPS; LEAVE; @@ -526,8 +661,6 @@ static void req_cancel_subs (aio_req grp) { - aio_req sub; - if (grp->type != EIO_GROUP) return; @@ -537,8 +670,7 @@ eio_grp_cancel (grp); } -static void -create_respipe (void) +static void create_respipe (void) { if (s_epipe_renew (&respipe)) croak ("IO::AIO: unable to initialize result pipe"); @@ -579,9 +711,13 @@ } } -static void atfork_child (void) +static void ecb_cold +reinit (void) { create_respipe (); + + if (eio_init (want_poll, done_poll) < 0) + croak ("IO::AIO: unable to initialise eio library"); } /*****************************************************************************/ @@ -591,10 +727,18 @@ # define munmap(addr,length) (errno = ENOSYS, -1) #endif +#if !_POSIX_MEMORY_PROTECTION +# 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 -static int -mmap_free (pTHX_ SV *sv, MAGIC *mg) +static int mmap_free (pTHX_ SV *sv, MAGIC *mg) { int old_errno = errno; munmap (mg->mg_ptr, (size_t)mg->mg_obj); @@ -603,8 +747,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); @@ -617,8 +764,7 @@ /*****************************************************************************/ -static SV * -get_cb (SV *cb_sv) +static SV * get_cb (SV *cb_sv) { SvGETMAGIC (cb_sv); return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; @@ -664,11 +810,38 @@ const_iv (ENOSYS) const_iv (O_RDONLY) const_iv (O_WRONLY) + const_iv (O_RDWR) const_iv (O_CREAT) const_iv (O_TRUNC) -#ifndef _WIN32 + const_iv (O_EXCL) + const_iv (O_APPEND) + + const_iv (O_ASYNC) + const_iv (O_DIRECT) + const_iv (O_NOATIME) + + const_iv (O_CLOEXEC) + const_iv (O_NOCTTY) + const_iv (O_NOFOLLOW) + const_iv (O_NONBLOCK) + const_iv (O_EXEC) + const_iv (O_SEARCH) + const_iv (O_DIRECTORY) + const_iv (O_DSYNC) + const_iv (O_RSYNC) + const_iv (O_SYNC) + const_iv (O_TTY_INIT) + const_iv (S_IFIFO) -#endif + const_iv (S_IFCHR) + const_iv (S_IFBLK) + const_iv (S_IFLNK) + const_iv (S_IFREG) + const_iv (S_IFDIR) + const_iv (S_IFWHT) + const_iv (S_IFSOCK) + const_iv (S_IFMT) + const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) @@ -676,6 +849,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) @@ -689,8 +868,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) @@ -706,8 +885,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) @@ -719,6 +898,8 @@ const_eio (SYNC_FILE_RANGE_WRITE) const_eio (SYNC_FILE_RANGE_WAIT_AFTER) + const_eio (FALLOC_FL_KEEP_SIZE) + const_eio (READDIR_DENTS) const_eio (READDIR_DIRS_FIRST) const_eio (READDIR_STAT_ORDER) @@ -744,17 +925,15 @@ newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); - create_respipe (); - - if (eio_init (want_poll, done_poll) < 0) - croak ("IO::AIO: unable to initialise eio library"); - - /* atfork child called in fifo order, so before eio's handler */ - X_THREAD_ATFORK (0, 0, atfork_child); + reinit (); } void -max_poll_reqs (int nreqs) +reinit () + PROTOTYPE: + +void +max_poll_reqs (unsigned int nreqs) PROTOTYPE: $ CODE: eio_set_max_poll_reqs (nreqs); @@ -766,32 +945,37 @@ eio_set_max_poll_time (nseconds); void -min_parallel (int nthreads) +min_parallel (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_min_parallel (nthreads); void -max_parallel (int nthreads) +max_parallel (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_max_parallel (nthreads); void -max_idle (int nthreads) +max_idle (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_max_idle (nthreads); void -max_outstanding (int maxreqs) +idle_timeout (unsigned int seconds) + PROTOTYPE: $ + CODE: + eio_set_idle_timeout (seconds); + +void +max_outstanding (unsigned int maxreqs) PROTOTYPE: $ CODE: max_outstanding = maxreqs; void aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ PPCODE: { dREQ; @@ -807,10 +991,10 @@ void aio_fsync (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ ALIAS: aio_fsync = EIO_FSYNC aio_fdatasync = EIO_FDATASYNC + aio_syncfs = EIO_SYNCFS PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -825,7 +1009,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); @@ -842,28 +1025,50 @@ } void +aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + dREQ; + + req->type = EIO_FALLOCATE; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->int2 = mode; + req->offs = offset; + req->size = len; + + REQ_SEND (req); +} + +void aio_close (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ PPCODE: { - static int close_pipe = -1; /* dummy fd to close fds via dup2 */ + static int close_fd = -1; /* dummy fd to close fds via dup2 */ int fd = s_fileno_croak (fh, 0); dREQ; - if (close_pipe < 0) + if (expect_false (close_fd < 0)) { int pipefd [2]; - if (pipe (pipefd) < 0 - || close (pipefd [1]) < 0 - || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) + if ( +#ifdef _WIN32 + _pipe (pipefd, 1, _O_BINARY) < 0 +#else + pipe (pipefd) < 0 + || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0 +#endif + || close (pipefd [1]) < 0 + ) abort (); /*D*/ - close_pipe = pipefd [0]; + close_fd = pipefd [0]; } req->type = EIO_DUP2; - req->int1 = close_pipe; + req->int1 = close_fd; req->sv2 = newSVsv (fh); req->int2 = fd; @@ -875,7 +1080,6 @@ ALIAS: aio_read = EIO_READ aio_write = EIO_WRITE - PROTOTYPE: $$$$$;$ PPCODE: { STRLEN svlen; @@ -926,13 +1130,14 @@ void aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) - PROTOTYPE: $$;$ + ALIAS: + aio_readlink = EIO_READLINK + aio_realpath = EIO_REALPATH PPCODE: { - SV *data; dREQ; - req->type = EIO_READLINK; + req->type = ix; req->sv1 = newSVsv (path); req->ptr1 = SvPVbyte_nolen (req->sv1); @@ -941,7 +1146,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); @@ -961,7 +1165,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); @@ -1002,6 +1205,22 @@ REQ_SEND; } +UV +major (UV dev) + ALIAS: + minor = 1 + CODE: + RETVAL = ix ? major (dev) : minor (dev); + OUTPUT: + RETVAL + +UV +makedev (UV maj, UV min) + CODE: + RETVAL = makedev (maj, min); + OUTPUT: + RETVAL + void aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) PPCODE: @@ -1172,12 +1391,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; @@ -1192,9 +1410,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; @@ -1202,6 +1420,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: { @@ -1215,7 +1474,6 @@ void aio_group (SV *callback=&PL_sv_undef) - PROTOTYPE: ;$ PPCODE: { dREQ; @@ -1242,7 +1500,6 @@ int aioreq_pri (int pri = 0) - PROTOTYPE: ;$ CODE: RETVAL = next_pri; if (items > 0) @@ -1264,7 +1521,6 @@ void flush () - PROTOTYPE: CODE: while (eio_nreqs ()) { @@ -1273,8 +1529,7 @@ } int -poll() - PROTOTYPE: +poll () CODE: poll_wait (); RETVAL = poll_cb (); @@ -1282,15 +1537,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 (); @@ -1298,38 +1552,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: @@ -1337,19 +1586,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 +IV sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) - PROTOTYPE: $$$$ CODE: RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); OUTPUT: @@ -1357,7 +1600,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); { @@ -1366,14 +1608,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); @@ -1384,30 +1632,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: -#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); +{ + 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_RANGE + RETVAL = munlock (addr, len); #else - RETVAL = -1; - errno = ENOSYS; + RETVAL = ((errno = ENOSYS), -1); #endif +} OUTPUT: RETVAL int munlockall () - PROTOTYPE: CODE: #if _POSIX_MEMLOCK munlockall ();