ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.250 by root, Fri Sep 22 05:20:39 2017 UTC vs.
Revision 1.259 by root, Sun Jul 22 20:39:19 2018 UTC

4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8 8
9#include "schmorp.h" 9#if !defined mg_findext
10# define mg_findext(sv,type,vtbl) mg_find (sv, type)
11#endif
10 12
11#include <stddef.h> 13#include <stddef.h>
12#include <stdlib.h> 14#include <stdlib.h>
13#include <errno.h> 15#include <errno.h>
14#include <sys/types.h> 16#include <sys/types.h>
106 108
107/*****************************************************************************/ 109/*****************************************************************************/
108 110
109#include "libeio/config.h" 111#include "libeio/config.h"
110 112
113#include "schmorp.h"
114
115#if HAVE_EVENTFD
116# include <sys/eventfd.h>
117#endif
118
119#if HAVE_TIMERFD
120# include <sys/timerfd.h>
121#endif
122
111#if HAVE_RLIMITS 123#if HAVE_RLIMITS
112 #include <sys/time.h> 124 #include <sys/time.h>
113 #include <sys/resource.h> 125 #include <sys/resource.h>
114#endif 126#endif
115 127
168#endif 180#endif
169 181
170#if PAGESIZE <= 0 182#if PAGESIZE <= 0
171# define PAGESIZE sysconf (_SC_PAGESIZE) 183# define PAGESIZE sysconf (_SC_PAGESIZE)
172#endif 184#endif
185
186/*****************************************************************************/
187
188#if !_POSIX_MAPPED_FILES
189# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS ()
190# define munmap(addr,length) EIO_ENOSYS ()
191#endif
192
193#if !_POSIX_MEMORY_PROTECTION
194# define mprotect(addr,len,prot) EIO_ENOSYS ()
195#endif
196
197#if !MREMAP_MAYMOVE
198# define mremap(old_address,old_size,new_size,flags,new_address) (errno = ENOSYS, (void *)-1)
199#endif
200
201#define FOREIGN_MAGIC PERL_MAGIC_ext
202
203static int ecb_cold
204mmap_free (pTHX_ SV *sv, MAGIC *mg)
205{
206 int old_errno = errno;
207 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
208 errno = old_errno;
209
210 mg->mg_obj = 0; /* just in case */
211
212 SvREADONLY_off (sv);
213
214 if (SvPVX (sv) != mg->mg_ptr)
215 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
216
217 SvCUR_set (sv, 0);
218 SvPVX (sv) = 0;
219 SvOK_off (sv);
220
221 return 0;
222}
223
224static MGVTBL mmap_vtbl = {
225 0, 0, 0, 0, mmap_free
226};
227
228static int ecb_cold
229sysfree_free (pTHX_ SV *sv, MAGIC *mg)
230{
231 free (mg->mg_ptr);
232 mg->mg_obj = 0; /* just in case */
233
234 SvREADONLY_off (sv);
235
236 if (SvPVX (sv) != mg->mg_ptr)
237 croak ("ERROR: IO::AIO mapped scalar changed location, detected");
238
239 SvCUR_set (sv, 0);
240 SvPVX (sv) = 0;
241 SvOK_off (sv);
242
243 return 0;
244}
245
246static MGVTBL sysfree_vtbl = {
247 0, 0, 0, 0, sysfree_free
248};
249
250/*****************************************************************************/
251
252/* helper: set scalar to foreign ptr with custom free */
253static void
254sv_set_foreign (SV *sv, const MGVTBL *const vtbl, void *addr, IV length)
255{
256 sv_force_normal (sv);
257
258 /* we store the length in mg_obj, as namlen is I32 :/ */
259 sv_magicext (sv, 0, FOREIGN_MAGIC, vtbl, (char *)addr, 0)
260 ->mg_obj = (SV *)length;
261
262 SvUPGRADE (sv, SVt_PV); /* nop... */
263
264 if (SvLEN (sv))
265 Safefree (SvPVX (sv));
266
267 SvPVX (sv) = (char *)addr;
268 SvCUR_set (sv, length);
269 SvLEN_set (sv, 0);
270 SvPOK_only (sv);
271}
272
273static void
274sv_clear_foreign (SV *sv)
275{
276 /* todo: iterate over magic and only free ours, but of course */
277 /* the perl5porters will call that (correct) behaviour buggy */
278 sv_unmagic (sv, FOREIGN_MAGIC);
279}
173 280
174/*****************************************************************************/ 281/*****************************************************************************/
175 282
176static void 283static void
177fiemap (eio_req *req) 284fiemap (eio_req *req)
597 SvSETMAGIC (req->sv2); 704 SvSETMAGIC (req->sv2);
598 PUSHs (sv_result); 705 PUSHs (sv_result);
599 } 706 }
600 break; 707 break;
601 708
709 case EIO_SLURP:
710 {
711 if (req->result >= 0)
712 {
713 /* if length was originally not known, we steal the malloc'ed memory */
714 if (req->flags & EIO_FLAG_PTR2_FREE)
715 {
716 req->flags &= ~EIO_FLAG_PTR2_FREE;
717 sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result);
718 }
719 else
720 {
721 SvCUR_set (req->sv2, req->result);
722 *SvEND (req->sv2) = 0;
723 SvPOK_only (req->sv2);
724 }
725
726 SvSETMAGIC (req->sv2);
727 }
728
729 PUSHs (sv_result);
730 }
731 break;
732
602 case EIO_CUSTOM: 733 case EIO_CUSTOM:
603 if (req->feed == fiemap) 734 if (req->feed == fiemap)
604 { 735 {
605#if HAVE_FIEMAP 736#if HAVE_FIEMAP
606 if (!req->result) 737 if (!req->result)
749 create_respipe (); 880 create_respipe ();
750 881
751 if (eio_init (want_poll, done_poll) < 0) 882 if (eio_init (want_poll, done_poll) < 0)
752 croak ("IO::AIO: unable to initialise eio library"); 883 croak ("IO::AIO: unable to initialise eio library");
753} 884}
754
755/*****************************************************************************/
756
757#if !_POSIX_MAPPED_FILES
758# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS ()
759# define munmap(addr,length) EIO_ENOSYS ()
760#endif
761
762#if !_POSIX_MEMORY_PROTECTION
763# define mprotect(addr,len,prot) EIO_ENOSYS ()
764#endif
765
766#define MMAP_MAGIC PERL_MAGIC_ext
767
768static int ecb_cold
769mmap_free (pTHX_ SV *sv, MAGIC *mg)
770{
771 int old_errno = errno;
772 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
773 errno = old_errno;
774
775 mg->mg_obj = 0; /* just in case */
776
777 SvREADONLY_off (sv);
778
779 if (SvPVX (sv) != mg->mg_ptr)
780 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
781
782 SvCUR_set (sv, 0);
783 SvPVX (sv) = 0;
784 SvOK_off (sv);
785
786 return 0;
787}
788
789static MGVTBL mmap_vtbl = {
790 0, 0, 0, 0, mmap_free
791};
792 885
793/*****************************************************************************/ 886/*****************************************************************************/
794 887
795static SV * 888static SV *
796get_cb (SV *cb_sv) 889get_cb (SV *cb_sv)
892 req_set_path1 (req, fh_or_path); 985 req_set_path1 (req, fh_or_path);
893 break; 986 break;
894 } 987 }
895} 988}
896 989
990/*****************************************************************************/
991
992static void
993ts_set (struct timespec *ts, NV value)
994{
995 ts->tv_sec = value;
996 ts->tv_nsec = (value - ts->tv_sec) * 1e9;
997}
998
999static NV
1000ts_get (const struct timespec *ts)
1001{
1002 return ts->tv_sec + ts->tv_nsec * 1e-9;
1003}
1004
1005/*****************************************************************************/
1006
897XS(boot_IO__AIO) ecb_cold; 1007XS(boot_IO__AIO) ecb_cold;
898 1008
899MODULE = IO::AIO PACKAGE = IO::AIO 1009MODULE = IO::AIO PACKAGE = IO::AIO
900 1010
901PROTOTYPES: ENABLE 1011PROTOTYPES: ENABLE
932 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) 1042 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
933 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) 1043 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
934 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) 1044 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
935 1045
936 /* the second block will be 0 when missing */ 1046 /* the second block will be 0 when missing */
1047 const_iv (O_ACCMODE)
1048
937 const_iv (O_RDONLY) 1049 const_iv (O_RDONLY)
938 const_iv (O_WRONLY) 1050 const_iv (O_WRONLY)
939 const_iv (O_RDWR) 1051 const_iv (O_RDWR)
940 const_iv (O_CREAT) 1052 const_iv (O_CREAT)
941 const_iv (O_TRUNC) 1053 const_iv (O_TRUNC)
1000 const_iv (MAP_NONBLOCK) 1112 const_iv (MAP_NONBLOCK)
1001 const_iv (MAP_GROWSDOWN) 1113 const_iv (MAP_GROWSDOWN)
1002 const_iv (MAP_32BIT) 1114 const_iv (MAP_32BIT)
1003 const_iv (MAP_HUGETLB) 1115 const_iv (MAP_HUGETLB)
1004 const_iv (MAP_STACK) 1116 const_iv (MAP_STACK)
1117
1118 const_iv (MREMAP_MAYMOVE)
1119 const_iv (MREMAP_FIXED)
1005 1120
1006 const_iv (F_DUPFD_CLOEXEC) 1121 const_iv (F_DUPFD_CLOEXEC)
1007 1122
1008 const_iv (F_OFD_GETLK) 1123 const_iv (F_OFD_GETLK)
1009 const_iv (F_OFD_SETLK) 1124 const_iv (F_OFD_SETLK)
1084 const_iv (SPLICE_F_MOVE) 1199 const_iv (SPLICE_F_MOVE)
1085 const_iv (SPLICE_F_NONBLOCK) 1200 const_iv (SPLICE_F_NONBLOCK)
1086 const_iv (SPLICE_F_MORE) 1201 const_iv (SPLICE_F_MORE)
1087 const_iv (SPLICE_F_GIFT) 1202 const_iv (SPLICE_F_GIFT)
1088 1203
1204 const_iv (EFD_CLOEXEC)
1205 const_iv (EFD_NONBLOCK)
1206 const_iv (EFD_SEMAPHORE)
1207
1208 const_iv (CLOCK_REALTIME)
1209 const_iv (CLOCK_MONOTONIC)
1210 const_iv (CLOCK_BOOTTIME)
1211 const_iv (CLOCK_REALTIME_ALARM)
1212 const_iv (CLOCK_BOOTTIME_ALARM)
1213
1214 const_iv (TFD_NONBLOCK)
1215 const_iv (TFD_CLOEXEC)
1216
1217 const_iv (TFD_TIMER_ABSTIME)
1218 const_iv (TFD_TIMER_CANCEL_ON_SET)
1219
1089 /* these are libeio constants, and are independent of gendef0 */ 1220 /* these are libeio constants, and are independent of gendef0 */
1090 const_eio (SEEK_SET) 1221 const_eio (SEEK_SET)
1091 const_eio (SEEK_CUR) 1222 const_eio (SEEK_CUR)
1092 const_eio (SEEK_END) 1223 const_eio (SEEK_END)
1093 1224
1738 1869
1739 REQ_SEND; 1870 REQ_SEND;
1740} 1871}
1741 1872
1742void 1873void
1874aio_slurp (SV *pathname, off_t offset, UV length, SV8 *data, SV *callback = &PL_sv_undef)
1875 PPCODE:
1876{
1877 char *svptr = 0;
1878
1879 sv_clear_foreign (data);
1880
1881 if (length) /* known length, directly read into scalar */
1882 {
1883 if (!SvPOK (data) || SvLEN (data) >= SvCUR (data))
1884 svptr = sv_grow (data, length + 1);
1885 else if (SvCUR (data) < length)
1886 croak ("length outside of scalar, and cannot grow");
1887 else
1888 svptr = SvPVbyte_nolen (data);
1889 }
1890
1891 {
1892 dREQ;
1893
1894 req->type = EIO_SLURP;
1895 req_set_path1 (req, pathname);
1896 req->offs = offset;
1897 req->size = length;
1898 req->sv2 = SvREFCNT_inc (data);
1899 req->ptr2 = svptr;
1900
1901 if (!SvREADONLY (data))
1902 {
1903 SvREADONLY_on (data);
1904 req->flags |= FLAG_SV2_RO_OFF;
1905 }
1906
1907 REQ_SEND;
1908 }
1909}
1910
1911void
1743aio_busy (double delay, SV *callback = &PL_sv_undef) 1912aio_busy (double delay, SV *callback = &PL_sv_undef)
1744 PPCODE: 1913 PPCODE:
1745{ 1914{
1746 dREQ; 1915 dREQ;
1747 1916
1881 RETVAL 2050 RETVAL
1882 2051
1883void 2052void
1884mmap (SV *scalar, STRLEN length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) 2053mmap (SV *scalar, STRLEN length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0)
1885 PPCODE: 2054 PPCODE:
1886 sv_unmagic (scalar, MMAP_MAGIC); 2055 sv_clear_foreign (scalar);
1887{ 2056{
1888 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; 2057 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1889 void *addr = (void *)mmap (0, length, prot, flags, fd, offset); 2058 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1890 if (addr == (void *)-1) 2059 if (addr == (void *)-1)
1891 XSRETURN_NO; 2060 XSRETURN_NO;
1892 2061
1893 sv_force_normal (scalar); 2062 sv_set_foreign (scalar, &mmap_vtbl, addr, length);
1894
1895 /* we store the length in mg_obj, as namlen is I32 :/ */
1896 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1897 ->mg_obj = (SV *)length;
1898
1899 SvUPGRADE (scalar, SVt_PV); /* nop... */
1900 2063
1901 if (!(prot & PROT_WRITE)) 2064 if (!(prot & PROT_WRITE))
1902 SvREADONLY_on (scalar); 2065 SvREADONLY_on (scalar);
1903 2066
1904 if (SvLEN (scalar))
1905 Safefree (SvPVX (scalar));
1906
1907 SvPVX (scalar) = (char *)addr;
1908 SvCUR_set (scalar, length);
1909 SvLEN_set (scalar, 0);
1910 SvPOK_only (scalar);
1911
1912 XSRETURN_YES; 2067 XSRETURN_YES;
1913} 2068}
1914 2069
1915void 2070void
1916munmap (SV *scalar) 2071munmap (SV *scalar)
1917 CODE: 2072 CODE:
1918 sv_unmagic (scalar, MMAP_MAGIC); 2073 sv_clear_foreign (scalar);
2074
2075SV *
2076mremap (SV *scalar, STRLEN new_length, int flags = 0, IV new_address = 0)
2077 CODE:
2078{
2079 MAGIC *mg = mg_findext (scalar, FOREIGN_MAGIC, &mmap_vtbl);
2080 void *new;
2081
2082 if (!mg || SvPVX (scalar) != mg->mg_ptr)
2083 croak ("IO::AIO::mremap: scalar not mapped by IO::AIO::mmap or improperly modified");
2084
2085 new = mremap (mg->mg_ptr, (size_t)mg->mg_obj, new_length, flags, (void *)new_address);
2086
2087 RETVAL = &PL_sv_no;
2088
2089 if (new != (void *)-1)
2090 {
2091 RETVAL = new == (void *)mg->mg_ptr
2092 ? newSVpvn ("0 but true", 10)
2093 : &PL_sv_yes;
2094
2095 mg->mg_ptr = (char *)new;
2096 mg->mg_obj = (SV *)new_length;
2097
2098 SvPVX (scalar) = mg->mg_ptr;
2099 SvCUR_set (scalar, new_length);
2100 }
2101}
2102 OUTPUT:
2103 RETVAL
1919 2104
1920int 2105int
1921madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) 2106madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot)
1922 ALIAS: 2107 ALIAS:
1923 mprotect = 1 2108 mprotect = 1
2053 { 2238 {
2054 EXTEND (SP, 2); 2239 EXTEND (SP, 2);
2055 PUSHs (newmortalFH (fd[0], O_RDONLY)); 2240 PUSHs (newmortalFH (fd[0], O_RDONLY));
2056 PUSHs (newmortalFH (fd[1], O_WRONLY)); 2241 PUSHs (newmortalFH (fd[1], O_WRONLY));
2057 } 2242 }
2243}
2244
2245void
2246eventfd (unsigned int initval = 0, int flags = 0)
2247 PPCODE:
2248{
2249 int fd;
2250#if HAVE_EVENTFD
2251 fd = eventfd (initval, flags);
2252#else
2253 fd = (errno = ENOSYS, -1);
2254#endif
2255
2256 XPUSHs (newmortalFH (fd, O_RDWR));
2257}
2258
2259void
2260timerfd_create (int clockid, int flags = 0)
2261 PPCODE:
2262{
2263 int fd;
2264#if HAVE_TIMERFD
2265 fd = timerfd_create (clockid, flags);
2266#else
2267 fd = (errno = ENOSYS, -1);
2268#endif
2269
2270 XPUSHs (newmortalFH (fd, O_RDWR));
2271}
2272
2273void
2274timerfd_settime (SV *fh, int flags, NV interval, NV value)
2275 PPCODE:
2276{
2277 int fd = s_fileno_croak (fh, 0);
2278#if HAVE_TIMERFD
2279 int res;
2280 struct itimerspec its, ots;
2281
2282 ts_set (&its.it_interval, interval);
2283 ts_set (&its.it_value , value);
2284 res = timerfd_settime (fd, flags, &its, &ots);
2285
2286 if (!res)
2287 {
2288 EXTEND (SP, 2);
2289 PUSHs (newSVnv (ts_get (&ots.it_interval)));
2290 PUSHs (newSVnv (ts_get (&ots.it_value)));
2291 }
2292#else
2293 errno = ENOSYS;
2294#endif
2295}
2296
2297void
2298timerfd_gettime (SV *fh)
2299 PPCODE:
2300{
2301 int fd = s_fileno_croak (fh, 0);
2302#if HAVE_TIMERFD
2303 int res;
2304 struct itimerspec ots;
2305 res = timerfd_gettime (fd, &ots);
2306
2307 if (!res)
2308 {
2309 EXTEND (SP, 2);
2310 PUSHs (newSVnv (ts_get (&ots.it_interval)));
2311 PUSHs (newSVnv (ts_get (&ots.it_value)));
2312 }
2313#else
2314 errno = ENOSYS;
2315#endif
2058} 2316}
2059 2317
2060UV 2318UV
2061get_fdlimit () 2319get_fdlimit ()
2062 CODE: 2320 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines