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.256 by root, Tue Feb 20 06:54:46 2018 UTC vs.
Revision 1.268 by root, Thu Nov 29 19:49:48 2018 UTC

3#include <errno.h> 3#include <errno.h>
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
9#if !defined mg_findext
10# define mg_findext(sv,type,vtbl) mg_find (sv, type)
11#endif
8 12
9#include <stddef.h> 13#include <stddef.h>
10#include <stdlib.h> 14#include <stdlib.h>
11#include <errno.h> 15#include <errno.h>
12#include <sys/types.h> 16#include <sys/types.h>
17#include <sys/socket.h>
13#include <sys/stat.h> 18#include <sys/stat.h>
14#include <limits.h> 19#include <limits.h>
15#include <fcntl.h> 20#include <fcntl.h>
16#include <sched.h> 21#include <sched.h>
17
18#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
19# include <sys/mman.h>
20#endif
21 22
22/* the incompetent fool that created musl keeps __linux__, refuses 23/* the incompetent fool that created musl keeps __linux__, refuses
23 * to implement any linux standard apis, and also has no way to test 24 * to implement any linux standard apis, and also has no way to test
24 * for his broken iplementation. don't complain to me if this fails 25 * for his broken iplementation. don't complain to me if this fails
25 * for you. 26 * for you.
73 #undef fchmod 74 #undef fchmod
74 #undef dup 75 #undef dup
75 #undef dup2 76 #undef dup2
76 #undef abort 77 #undef abort
77 #undef pipe 78 #undef pipe
79 #undef utime
78 80
79 #define EIO_STRUCT_STAT struct _stati64 81 #define EIO_STRUCT_STAT struct _stati64
80 #define EIO_STRUCT_STATI64 82 #define EIO_STRUCT_STATI64
81 83
82#else 84#else
102#define expect_false(expr) expect ((expr) != 0, 0) 104#define expect_false(expr) expect ((expr) != 0, 0)
103#define expect_true(expr) expect ((expr) != 0, 1) 105#define expect_true(expr) expect ((expr) != 0, 1)
104 106
105/*****************************************************************************/ 107/*****************************************************************************/
106 108
107#include "libeio/config.h" 109#include "config.h"
110
111#if HAVE_SYS_MKDEV_H
112# include <sys/mkdev.h>
113#elif HAVE_SYS_SYSMACROS_H
114# include <sys/sysmacros.h>
115#endif
116
117#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
118# include <sys/mman.h>
119#endif
120
121#if HAVE_ST_XTIMENSEC
122# define ATIMENSEC PL_statcache.st_atimensec
123# define MTIMENSEC PL_statcache.st_mtimensec
124# define CTIMENSEC PL_statcache.st_ctimensec
125#elif HAVE_ST_XTIMESPEC
126# define ATIMENSEC PL_statcache.st_atim.tv_nsec
127# define MTIMENSEC PL_statcache.st_mtim.tv_nsec
128# define CTIMENSEC PL_statcache.st_ctim.tv_nsec
129#else
130# define ATIMENSEC 0
131# define MTIMENSEC 0
132# define CTIMENSEC 0
133#endif
134
135#if HAVE_ST_BIRTHTIMENSEC
136# define BTIMESEC PL_statcache.st_birthtime
137# define BTIMENSEC PL_statcache.st_birthtimensec
138#elif HAVE_ST_BIRTHTIMESPEC
139# define BTIMESEC PL_statcache.st_birthtim.tv_sec
140# define BTIMENSEC PL_statcache.st_birthtim.tv_nsec
141#else
142# define BTIMESEC 0
143# define BTIMENSEC 0
144#endif
145
146#if HAVE_ST_GEN
147# define ST_GEN PL_statcache.st_gen
148#else
149# define ST_GEN 0
150#endif
108 151
109#include "schmorp.h" 152#include "schmorp.h"
110 153
111#if HAVE_EVENTFD 154#if HAVE_EVENTFD
112# include <sys/eventfd.h> 155# include <sys/eventfd.h>
159# else 202# else
160# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ 203# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
161# endif 204# endif
162#endif 205#endif
163 206
164/* defines all sorts of constants to 0 unless they are already defined */
165/* also provides const_iv_ and const_niv_ macros for them */
166#include "def0.h"
167
168#ifndef makedev 207#ifndef makedev
169# define makedev(maj,min) (((maj) << 8) | (min)) 208# define makedev(maj,min) (((maj) << 8) | (min))
170#endif 209#endif
171#ifndef major 210#ifndef major
172# define major(dev) ((dev) >> 8) 211# define major(dev) ((dev) >> 8)
173#endif 212#endif
174#ifndef minor 213#ifndef minor
175# define minor(dev) ((dev) & 0xff) 214# define minor(dev) ((dev) & 0xff)
176#endif 215#endif
177 216
217/* solaris has a non-posix/unix compliant PAGESIZE that breaks compilation */
218#ifdef __sun
219# undef PAGESIZE
220#endif
221
178#if PAGESIZE <= 0 222#if PAGESIZE <= 0
179# define PAGESIZE sysconf (_SC_PAGESIZE) 223# define PAGESIZE sysconf (_SC_PAGESIZE)
180#endif 224#endif
181 225
182/*****************************************************************************/ 226/*****************************************************************************/
183 227
184#if !_POSIX_MAPPED_FILES 228#if !_POSIX_MAPPED_FILES
185# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () 229# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, (void *)-1)
186# define munmap(addr,length) EIO_ENOSYS () 230# define munmap(addr,length) EIO_ENOSYS ()
187#endif 231#endif
188 232
189#if !_POSIX_MEMORY_PROTECTION 233#if !_POSIX_MEMORY_PROTECTION
190# define mprotect(addr,len,prot) EIO_ENOSYS () 234# define mprotect(addr,len,prot) EIO_ENOSYS ()
235#endif
236
237#if !MREMAP_MAYMOVE
238# define mremap(old_address,old_size,new_size,flags,new_address) (errno = ENOSYS, (void *)-1)
191#endif 239#endif
192 240
193#define FOREIGN_MAGIC PERL_MAGIC_ext 241#define FOREIGN_MAGIC PERL_MAGIC_ext
194 242
195static int ecb_cold 243static int ecb_cold
267{ 315{
268 /* todo: iterate over magic and only free ours, but of course */ 316 /* todo: iterate over magic and only free ours, but of course */
269 /* the perl5porters will call that (correct) behaviour buggy */ 317 /* the perl5porters will call that (correct) behaviour buggy */
270 sv_unmagic (sv, FOREIGN_MAGIC); 318 sv_unmagic (sv, FOREIGN_MAGIC);
271} 319}
320
321/*****************************************************************************/
322
323/* defines all sorts of constants to 0 unless they are already defined */
324/* also provides const_iv_ and const_niv_ macros for them */
325#include "def0.h"
272 326
273/*****************************************************************************/ 327/*****************************************************************************/
274 328
275static void 329static void
276fiemap (eio_req *req) 330fiemap (eio_req *req)
367 errno = ENOSYS; 421 errno = ENOSYS;
368#endif 422#endif
369} 423}
370 424
371/*****************************************************************************/ 425/*****************************************************************************/
426
427static int close_fd; /* dummy fd to close fds via dup2 */
372 428
373enum { 429enum {
374 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ 430 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
375}; 431};
376 432
1034 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) 1090 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
1035 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) 1091 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
1036 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) 1092 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
1037 1093
1038 /* the second block will be 0 when missing */ 1094 /* the second block will be 0 when missing */
1095 const_iv (O_ACCMODE)
1096
1039 const_iv (O_RDONLY) 1097 const_iv (O_RDONLY)
1040 const_iv (O_WRONLY) 1098 const_iv (O_WRONLY)
1041 const_iv (O_RDWR) 1099 const_iv (O_RDWR)
1042 const_iv (O_CREAT) 1100 const_iv (O_CREAT)
1043 const_iv (O_TRUNC) 1101 const_iv (O_TRUNC)
1103 const_iv (MAP_GROWSDOWN) 1161 const_iv (MAP_GROWSDOWN)
1104 const_iv (MAP_32BIT) 1162 const_iv (MAP_32BIT)
1105 const_iv (MAP_HUGETLB) 1163 const_iv (MAP_HUGETLB)
1106 const_iv (MAP_STACK) 1164 const_iv (MAP_STACK)
1107 1165
1166 const_iv (MREMAP_MAYMOVE)
1167 const_iv (MREMAP_FIXED)
1168
1108 const_iv (F_DUPFD_CLOEXEC) 1169 const_iv (F_DUPFD_CLOEXEC)
1170
1171 const_iv (MSG_CMSG_CLOEXEC)
1172 const_iv (SOCK_CLOEXEC)
1109 1173
1110 const_iv (F_OFD_GETLK) 1174 const_iv (F_OFD_GETLK)
1111 const_iv (F_OFD_SETLK) 1175 const_iv (F_OFD_SETLK)
1112 const_iv (F_OFD_GETLKW) 1176 const_iv (F_OFD_GETLKW)
1113 1177
1209 const_eio (SEEK_CUR) 1273 const_eio (SEEK_CUR)
1210 const_eio (SEEK_END) 1274 const_eio (SEEK_END)
1211 1275
1212 const_eio (MCL_FUTURE) 1276 const_eio (MCL_FUTURE)
1213 const_eio (MCL_CURRENT) 1277 const_eio (MCL_CURRENT)
1278 const_eio (MCL_ONFAULT)
1214 1279
1215 const_eio (MS_ASYNC) 1280 const_eio (MS_ASYNC)
1216 const_eio (MS_INVALIDATE) 1281 const_eio (MS_INVALIDATE)
1217 const_eio (MS_SYNC) 1282 const_eio (MS_SYNC)
1218 1283
1257 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1322 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1258 newCONSTSUB (aio_stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1323 newCONSTSUB (aio_stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1259 1324
1260 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); 1325 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
1261 1326
1327 /* allocate dummy pipe fd for aio_close */
1328 {
1329 int pipefd [2];
1330
1331 if (
1332#ifdef _WIN32
1333 _pipe (pipefd, 1, _O_BINARY) < 0
1334#else
1335 pipe (pipefd) < 0
1336 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0
1337#endif
1338 || close (pipefd [1]) < 0
1339 )
1340 croak ("IO::AIO: unable to create dummy pipe for aio_close");
1341
1342 close_fd = pipefd [0];
1343 }
1344
1262 reinit (); 1345 reinit ();
1263} 1346}
1264 1347
1265void 1348void
1266reinit () 1349reinit ()
1388 1471
1389void 1472void
1390aio_close (SV *fh, SV *callback = &PL_sv_undef) 1473aio_close (SV *fh, SV *callback = &PL_sv_undef)
1391 PPCODE: 1474 PPCODE:
1392{ 1475{
1393 static int close_fd = -1; /* dummy fd to close fds via dup2 */
1394 int fd = s_fileno_croak (fh, 0); 1476 int fd = s_fileno_croak (fh, 0);
1395 dREQ; 1477 dREQ;
1396
1397 if (expect_false (close_fd < 0))
1398 {
1399 int pipefd [2];
1400
1401 if (
1402#ifdef _WIN32
1403 _pipe (pipefd, 1, _O_BINARY) < 0
1404#else
1405 pipe (pipefd) < 0
1406 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0
1407#endif
1408 || close (pipefd [1]) < 0
1409 )
1410 abort (); /*D*/
1411
1412 close_fd = pipefd [0];
1413 }
1414 1478
1415 req->type = EIO_DUP2; 1479 req->type = EIO_DUP2;
1416 req->int1 = close_fd; 1480 req->int1 = close_fd;
1417 req->sv2 = newSVsv (fh); 1481 req->sv2 = newSVsv (fh);
1418 req->int2 = fd; 1482 req->int2 = fd;
1594 req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path); 1658 req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path);
1595 1659
1596 REQ_SEND; 1660 REQ_SEND;
1597} 1661}
1598 1662
1663void
1664st_xtime ()
1665 ALIAS:
1666 st_atime = 0x01
1667 st_mtime = 0x02
1668 st_ctime = 0x04
1669 st_btime = 0x08
1670 st_xtime = 0x0f
1671 PPCODE:
1672 EXTEND (SP, 4);
1673 if (ix & 0x01) PUSHs (newSVnv (PL_statcache.st_atime + 1e-9 * ATIMENSEC));
1674 if (ix & 0x02) PUSHs (newSVnv (PL_statcache.st_mtime + 1e-9 * MTIMENSEC));
1675 if (ix & 0x04) PUSHs (newSVnv (PL_statcache.st_ctime + 1e-9 * CTIMENSEC));
1676 if (ix & 0x08) PUSHs (newSVnv (BTIMESEC + 1e-9 * BTIMENSEC));
1677
1678void
1679st_xtimensec ()
1680 ALIAS:
1681 st_atimensec = 0x01
1682 st_mtimensec = 0x02
1683 st_ctimensec = 0x04
1684 st_btimensec = 0x08
1685 st_xtimensec = 0x0f
1686 st_btimesec = 0x10
1687 st_gen = 0x20
1688 PPCODE:
1689 EXTEND (SP, 4);
1690 if (ix & 0x01) PUSHs (newSViv (ATIMENSEC));
1691 if (ix & 0x02) PUSHs (newSViv (MTIMENSEC));
1692 if (ix & 0x04) PUSHs (newSViv (CTIMENSEC));
1693 if (ix & 0x08) PUSHs (newSViv (BTIMENSEC));
1694 if (ix & 0x10) PUSHs (newSVuv (BTIMESEC));
1695 if (ix & 0x20) PUSHs (newSVuv (ST_GEN));
1696
1599UV 1697UV
1600major (UV dev) 1698major (UV dev)
1601 ALIAS: 1699 ALIAS:
1602 minor = 1 1700 minor = 1
1603 CODE: 1701 CODE:
2057void 2155void
2058munmap (SV *scalar) 2156munmap (SV *scalar)
2059 CODE: 2157 CODE:
2060 sv_clear_foreign (scalar); 2158 sv_clear_foreign (scalar);
2061 2159
2160SV *
2161mremap (SV *scalar, STRLEN new_length, int flags = MREMAP_MAYMOVE, IV new_address = 0)
2162 CODE:
2163{
2164 MAGIC *mg = mg_findext (scalar, FOREIGN_MAGIC, &mmap_vtbl);
2165 void *new;
2166
2167 if (!mg || SvPVX (scalar) != mg->mg_ptr)
2168 croak ("IO::AIO::mremap: scalar not mapped by IO::AIO::mmap or improperly modified");
2169
2170 new = mremap (mg->mg_ptr, (size_t)mg->mg_obj, new_length, flags, (void *)new_address);
2171
2172 RETVAL = &PL_sv_no;
2173
2174 if (new != (void *)-1)
2175 {
2176 RETVAL = new == (void *)mg->mg_ptr
2177 ? newSVpvn ("0 but true", 10)
2178 : &PL_sv_yes;
2179
2180 mg->mg_ptr = (char *)new;
2181 mg->mg_obj = (SV *)new_length;
2182
2183 SvPVX (scalar) = mg->mg_ptr;
2184 SvCUR_set (scalar, new_length);
2185 }
2186}
2187 OUTPUT:
2188 RETVAL
2189
2062int 2190int
2063madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) 2191madvise (SV *scalar, IV offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot)
2064 ALIAS: 2192 ALIAS:
2065 mprotect = 1 2193 mprotect = 1
2066 CODE: 2194 CODE:
2067{ 2195{
2068 STRLEN svlen; 2196 STRLEN svlen;
2089} 2217}
2090 OUTPUT: 2218 OUTPUT:
2091 RETVAL 2219 RETVAL
2092 2220
2093int 2221int
2094munlock (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef) 2222munlock (SV *scalar, IV offset = 0, SV *length = &PL_sv_undef)
2095 CODE: 2223 CODE:
2096{ 2224{
2097 STRLEN svlen; 2225 STRLEN svlen;
2098 void *addr = SvPVbyte (scalar, svlen); 2226 void *addr = SvPVbyte (scalar, svlen);
2099 size_t len = SvUV (length); 2227 size_t len = SvUV (length);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines