… | |
… | |
18 | #include <fcntl.h> |
18 | #include <fcntl.h> |
19 | #include <sched.h> |
19 | #include <sched.h> |
20 | |
20 | |
21 | #if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES |
21 | #if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES |
22 | # include <sys/mman.h> |
22 | # include <sys/mman.h> |
23 | #endif |
|
|
24 | |
|
|
25 | #if !_POSIX_MAPPED_FILES |
|
|
26 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) |
|
|
27 | # define munmap(addr,length) (errno = ENOSYS, -1) |
|
|
28 | #endif |
23 | #endif |
29 | |
24 | |
30 | /* perl namespace pollution */ |
25 | /* perl namespace pollution */ |
31 | #undef VERSION |
26 | #undef VERSION |
32 | |
27 | |
… | |
… | |
145 | #endif |
140 | #endif |
146 | #ifndef POSIX_FADV_DONTNEED |
141 | #ifndef POSIX_FADV_DONTNEED |
147 | # define POSIX_FADV_DONTNEED 0 |
142 | # define POSIX_FADV_DONTNEED 0 |
148 | #endif |
143 | #endif |
149 | |
144 | |
|
|
145 | #ifndef POSIX_MADV_NORMAL |
|
|
146 | # define POSIX_MADV_NORMAL 0 |
|
|
147 | # define NO_MADVISE 1 |
|
|
148 | #endif |
|
|
149 | #ifndef POSIX_MADV_SEQUENTIAL |
|
|
150 | # define POSIX_MADV_SEQUENTIAL 0 |
|
|
151 | #endif |
|
|
152 | #ifndef POSIX_MADV_RANDOM |
|
|
153 | # define POSIX_MADV_RANDOM 0 |
|
|
154 | #endif |
|
|
155 | #ifndef POSIX_MADV_WILLNEED |
|
|
156 | # define POSIX_MADV_WILLNEED 0 |
|
|
157 | #endif |
|
|
158 | #ifndef POSIX_MADV_DONTNEED |
|
|
159 | # define POSIX_MADV_DONTNEED 0 |
|
|
160 | #endif |
|
|
161 | |
150 | #ifndef ST_NODEV |
162 | #ifndef ST_NODEV |
151 | # define ST_NODEV 0 |
163 | # define ST_NODEV 0 |
152 | #endif |
164 | #endif |
153 | #ifndef ST_NOEXEC |
165 | #ifndef ST_NOEXEC |
154 | # define ST_NOEXEC 0 |
166 | # define ST_NOEXEC 0 |
… | |
… | |
309 | } |
321 | } |
310 | } |
322 | } |
311 | |
323 | |
312 | static int req_invoke (eio_req *req) |
324 | static int req_invoke (eio_req *req) |
313 | { |
325 | { |
314 | dSP; |
|
|
315 | |
|
|
316 | if (req->flags & FLAG_SV2_RO_OFF) |
326 | if (req->flags & FLAG_SV2_RO_OFF) |
317 | SvREADONLY_off (req->sv2); |
327 | SvREADONLY_off (req->sv2); |
318 | |
328 | |
319 | if (!EIO_CANCELLED (req) && req->callback) |
329 | if (!EIO_CANCELLED (req) && req->callback) |
320 | { |
330 | { |
|
|
331 | dSP; |
|
|
332 | static SV *sv_result_cache; /* caches the result integer SV */ |
|
|
333 | SV *sv_result; |
|
|
334 | |
321 | ENTER; |
335 | ENTER; |
322 | SAVETMPS; |
336 | SAVETMPS; |
323 | PUSHMARK (SP); |
337 | PUSHMARK (SP); |
324 | EXTEND (SP, 1); |
338 | EXTEND (SP, 1); |
|
|
339 | |
|
|
340 | /* do not recreate the result IV from scratch each time */ |
|
|
341 | if (expect_true (sv_result_cache)) |
|
|
342 | { |
|
|
343 | sv_result = sv_result_cache; sv_result_cache = 0; |
|
|
344 | SvIV_set (sv_result, req->result); |
|
|
345 | } |
|
|
346 | else |
|
|
347 | { |
|
|
348 | sv_result = newSViv (req->result); |
|
|
349 | SvREADONLY_on (sv_result); |
|
|
350 | } |
325 | |
351 | |
326 | switch (req->type) |
352 | switch (req->type) |
327 | { |
353 | { |
328 | case EIO_READDIR: |
354 | case EIO_READDIR: |
329 | { |
355 | { |
… | |
… | |
474 | case EIO_LSTAT: |
500 | case EIO_LSTAT: |
475 | case EIO_FSTAT: |
501 | case EIO_FSTAT: |
476 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
502 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
477 | PL_laststatval = req->result; |
503 | PL_laststatval = req->result; |
478 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
504 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
479 | PUSHs (sv_2mortal (newSViv (req->result))); |
505 | PUSHs (sv_result); |
480 | break; |
506 | break; |
481 | |
507 | |
482 | case EIO_READ: |
508 | case EIO_READ: |
483 | { |
509 | { |
484 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
510 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
485 | *SvEND (req->sv2) = 0; |
511 | *SvEND (req->sv2) = 0; |
486 | SvPOK_only (req->sv2); |
512 | SvPOK_only (req->sv2); |
487 | SvSETMAGIC (req->sv2); |
513 | SvSETMAGIC (req->sv2); |
488 | PUSHs (sv_2mortal (newSViv (req->result))); |
514 | PUSHs (sv_result); |
489 | } |
515 | } |
490 | break; |
516 | break; |
491 | |
517 | |
492 | case EIO_DUP2: |
518 | case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */ |
493 | if (req->result > 0) |
519 | if (req->result > 0) |
494 | req->result = 0; |
520 | SvIV_set (sv_result, 0); |
495 | /* FALLTHROUGH */ |
521 | /* FALLTHROUGH */ |
496 | |
522 | |
497 | default: |
523 | default: |
498 | PUSHs (sv_2mortal (newSViv (req->result))); |
524 | PUSHs (sv_result); |
499 | break; |
525 | break; |
500 | } |
526 | } |
501 | |
527 | |
502 | errno = req->errorno; |
528 | errno = req->errorno; |
503 | |
529 | |
504 | PUTBACK; |
530 | PUTBACK; |
505 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
531 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
506 | SPAGAIN; |
532 | SPAGAIN; |
|
|
533 | |
|
|
534 | if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) |
|
|
535 | SvREFCNT_dec (sv_result); |
|
|
536 | else |
|
|
537 | sv_result_cache = sv_result; |
507 | |
538 | |
508 | FREETMPS; |
539 | FREETMPS; |
509 | LEAVE; |
540 | LEAVE; |
510 | |
541 | |
511 | PUTBACK; |
542 | PUTBACK; |
… | |
… | |
589 | create_respipe (); |
620 | create_respipe (); |
590 | } |
621 | } |
591 | |
622 | |
592 | /*****************************************************************************/ |
623 | /*****************************************************************************/ |
593 | |
624 | |
|
|
625 | #if !_POSIX_MAPPED_FILES |
|
|
626 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) |
|
|
627 | # define munmap(addr,length) (errno = ENOSYS, -1) |
|
|
628 | #endif |
|
|
629 | |
594 | #define MMAP_MAGIC PERL_MAGIC_ext |
630 | #define MMAP_MAGIC PERL_MAGIC_ext |
595 | |
631 | |
596 | static int |
632 | static int |
597 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
633 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
598 | { |
634 | { |
… | |
… | |
601 | errno = old_errno; |
637 | errno = old_errno; |
602 | |
638 | |
603 | mg->mg_obj = 0; /* just in case */ |
639 | mg->mg_obj = 0; /* just in case */ |
604 | |
640 | |
605 | SvREADONLY_off (sv); |
641 | SvREADONLY_off (sv); |
|
|
642 | |
|
|
643 | if (SvPVX (sv) != mg->mg_ptr) |
|
|
644 | croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); |
|
|
645 | |
606 | SvCUR_set (sv, 0); |
646 | SvCUR_set (sv, 0); |
607 | SvLEN_set (sv, 0); |
|
|
608 | SvPVX (sv) = 0; |
647 | SvPVX (sv) = 0; |
609 | SvOK_off (sv); |
648 | SvOK_off (sv); |
610 | |
649 | |
611 | return 0; |
650 | return 0; |
612 | } |
651 | } |
… | |
… | |
662 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
701 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
663 | const_iv (EXDEV) |
702 | const_iv (EXDEV) |
664 | const_iv (ENOSYS) |
703 | const_iv (ENOSYS) |
665 | const_iv (O_RDONLY) |
704 | const_iv (O_RDONLY) |
666 | const_iv (O_WRONLY) |
705 | const_iv (O_WRONLY) |
|
|
706 | const_iv (O_RDWR) |
667 | const_iv (O_CREAT) |
707 | const_iv (O_CREAT) |
668 | const_iv (O_TRUNC) |
708 | const_iv (O_TRUNC) |
|
|
709 | const_iv (O_EXCL) |
|
|
710 | const_iv (O_APPEND) |
669 | #ifndef _WIN32 |
711 | #ifndef _WIN32 |
670 | const_iv (S_IFIFO) |
712 | const_iv (S_IFIFO) |
671 | #endif |
713 | #endif |
672 | const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) |
714 | const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) |
673 | const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
715 | const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
674 | const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) |
716 | const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) |
675 | const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
717 | const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
676 | const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
718 | const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
677 | const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
719 | const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
|
|
720 | |
|
|
721 | const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) |
|
|
722 | const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) |
|
|
723 | const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) |
|
|
724 | const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) |
|
|
725 | const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) |
678 | |
726 | |
679 | const_iv (ST_RDONLY) |
727 | const_iv (ST_RDONLY) |
680 | const_iv (ST_NOSUID) |
728 | const_iv (ST_NOSUID) |
681 | const_iv (ST_NODEV) |
729 | const_iv (ST_NODEV) |
682 | const_iv (ST_NOEXEC) |
730 | const_iv (ST_NOEXEC) |
… | |
… | |
1364 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1412 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1365 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
1413 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
1366 | if (addr == (void *)-1) |
1414 | if (addr == (void *)-1) |
1367 | XSRETURN_NO; |
1415 | XSRETURN_NO; |
1368 | |
1416 | |
|
|
1417 | sv_force_normal (scalar); |
|
|
1418 | |
1369 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
1419 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
1370 | sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) |
1420 | sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) |
1371 | ->mg_obj = (SV *)length; |
1421 | ->mg_obj = (SV *)length; |
1372 | |
1422 | |
1373 | SvUPGRADE (scalar, SVt_PV); /* nop... */ |
1423 | SvUPGRADE (scalar, SVt_PV); /* nop... */ |
|
|
1424 | |
1374 | if (!(prot & PROT_WRITE)) |
1425 | if (!(prot & PROT_WRITE)) |
1375 | SvREADONLY_on (scalar); |
1426 | SvREADONLY_on (scalar); |
|
|
1427 | |
|
|
1428 | if (SvLEN (scalar)) |
|
|
1429 | Safefree (SvPVX (scalar)); |
1376 | |
1430 | |
1377 | SvPVX (scalar) = (char *)addr; |
1431 | SvPVX (scalar) = (char *)addr; |
1378 | SvCUR_set (scalar, length); |
1432 | SvCUR_set (scalar, length); |
1379 | SvLEN_set (scalar, 0); |
1433 | SvLEN_set (scalar, 0); |
1380 | SvPOK_only (scalar); |
1434 | SvPOK_only (scalar); |
… | |
… | |
1385 | void |
1439 | void |
1386 | munmap (SV *scalar) |
1440 | munmap (SV *scalar) |
1387 | PROTOTYPE: $ |
1441 | PROTOTYPE: $ |
1388 | CODE: |
1442 | CODE: |
1389 | sv_unmagic (scalar, MMAP_MAGIC); |
1443 | sv_unmagic (scalar, MMAP_MAGIC); |
|
|
1444 | |
|
|
1445 | int |
|
|
1446 | madvise (SV *scalar, off_t offset, off_t length, IV advice) |
|
|
1447 | PROTOTYPE: $$$$ |
|
|
1448 | CODE: |
|
|
1449 | { |
|
|
1450 | char *addr = SvPV_nolen (scalar) + offset; |
|
|
1451 | |
|
|
1452 | if (!SvOK (ST (2))) |
|
|
1453 | length = SvCUR (scalar) - offset; |
|
|
1454 | |
|
|
1455 | if (addr >= SvEND (scalar) || length <= 0) |
|
|
1456 | XSRETURN_EMPTY; |
|
|
1457 | |
|
|
1458 | #if _XOPEN_SOURCE >= 600 && !NO_MADVISE |
|
|
1459 | RETVAL = posix_madvise (addr, length, advice); |
|
|
1460 | #else |
|
|
1461 | RETVAL = errno = ENOSYS; /* yes, this is actually correct */ |
|
|
1462 | #endif |
|
|
1463 | } |
|
|
1464 | OUTPUT: |
|
|
1465 | RETVAL |
1390 | |
1466 | |
1391 | int |
1467 | int |
1392 | mlockall (int flags) |
1468 | mlockall (int flags) |
1393 | PROTOTYPE: $ |
1469 | PROTOTYPE: $ |
1394 | CODE: |
1470 | CODE: |