… | |
… | |
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 | |
… | |
… | |
309 | } |
304 | } |
310 | } |
305 | } |
311 | |
306 | |
312 | static int req_invoke (eio_req *req) |
307 | static int req_invoke (eio_req *req) |
313 | { |
308 | { |
314 | dSP; |
|
|
315 | |
|
|
316 | if (req->flags & FLAG_SV2_RO_OFF) |
309 | if (req->flags & FLAG_SV2_RO_OFF) |
317 | SvREADONLY_off (req->sv2); |
310 | SvREADONLY_off (req->sv2); |
318 | |
311 | |
319 | if (!EIO_CANCELLED (req) && req->callback) |
312 | if (!EIO_CANCELLED (req) && req->callback) |
320 | { |
313 | { |
|
|
314 | dSP; |
|
|
315 | static SV *sv_result_cache; /* caches the result integer SV */ |
|
|
316 | SV *sv_result; |
|
|
317 | |
321 | ENTER; |
318 | ENTER; |
322 | SAVETMPS; |
319 | SAVETMPS; |
323 | PUSHMARK (SP); |
320 | PUSHMARK (SP); |
324 | EXTEND (SP, 1); |
321 | EXTEND (SP, 1); |
|
|
322 | |
|
|
323 | /* do not recreate the result IV from scratch each time */ |
|
|
324 | if (expect_true (sv_result_cache)) |
|
|
325 | { |
|
|
326 | sv_result = sv_result_cache; sv_result_cache = 0; |
|
|
327 | SvIV_set (sv_result, req->result); |
|
|
328 | } |
|
|
329 | else |
|
|
330 | { |
|
|
331 | sv_result = newSViv (req->result); |
|
|
332 | SvREADONLY_on (sv_result); |
|
|
333 | } |
325 | |
334 | |
326 | switch (req->type) |
335 | switch (req->type) |
327 | { |
336 | { |
328 | case EIO_READDIR: |
337 | case EIO_READDIR: |
329 | { |
338 | { |
… | |
… | |
474 | case EIO_LSTAT: |
483 | case EIO_LSTAT: |
475 | case EIO_FSTAT: |
484 | case EIO_FSTAT: |
476 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
485 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
477 | PL_laststatval = req->result; |
486 | PL_laststatval = req->result; |
478 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
487 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
479 | PUSHs (sv_2mortal (newSViv (req->result))); |
488 | PUSHs (sv_result); |
480 | break; |
489 | break; |
481 | |
490 | |
482 | case EIO_READ: |
491 | case EIO_READ: |
483 | { |
492 | { |
484 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
493 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
485 | *SvEND (req->sv2) = 0; |
494 | *SvEND (req->sv2) = 0; |
486 | SvPOK_only (req->sv2); |
495 | SvPOK_only (req->sv2); |
487 | SvSETMAGIC (req->sv2); |
496 | SvSETMAGIC (req->sv2); |
488 | PUSHs (sv_2mortal (newSViv (req->result))); |
497 | PUSHs (sv_result); |
489 | } |
498 | } |
490 | break; |
499 | break; |
491 | |
500 | |
492 | case EIO_DUP2: |
501 | case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */ |
493 | if (req->result > 0) |
502 | if (req->result > 0) |
494 | req->result = 0; |
503 | SvIV_set (sv_result, 0); |
495 | /* FALLTHROUGH */ |
504 | /* FALLTHROUGH */ |
496 | |
505 | |
497 | default: |
506 | default: |
498 | PUSHs (sv_2mortal (newSViv (req->result))); |
507 | PUSHs (sv_result); |
499 | break; |
508 | break; |
500 | } |
509 | } |
501 | |
510 | |
502 | errno = req->errorno; |
511 | errno = req->errorno; |
503 | |
512 | |
504 | PUTBACK; |
513 | PUTBACK; |
505 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
514 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
506 | SPAGAIN; |
515 | SPAGAIN; |
|
|
516 | |
|
|
517 | if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) |
|
|
518 | SvREFCNT_dec (sv_result); |
|
|
519 | else |
|
|
520 | sv_result_cache = sv_result; |
507 | |
521 | |
508 | FREETMPS; |
522 | FREETMPS; |
509 | LEAVE; |
523 | LEAVE; |
510 | |
524 | |
511 | PUTBACK; |
525 | PUTBACK; |
… | |
… | |
589 | create_respipe (); |
603 | create_respipe (); |
590 | } |
604 | } |
591 | |
605 | |
592 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
593 | |
607 | |
|
|
608 | #if !_POSIX_MAPPED_FILES |
|
|
609 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) |
|
|
610 | # define munmap(addr,length) (errno = ENOSYS, -1) |
|
|
611 | #endif |
|
|
612 | |
594 | #define MMAP_MAGIC PERL_MAGIC_ext |
613 | #define MMAP_MAGIC PERL_MAGIC_ext |
595 | |
614 | |
596 | static int |
615 | static int |
597 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
616 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
598 | { |
617 | { |
… | |
… | |
601 | errno = old_errno; |
620 | errno = old_errno; |
602 | |
621 | |
603 | mg->mg_obj = 0; /* just in case */ |
622 | mg->mg_obj = 0; /* just in case */ |
604 | |
623 | |
605 | SvREADONLY_off (sv); |
624 | SvREADONLY_off (sv); |
|
|
625 | |
|
|
626 | if (SvPVX (sv) != mg->mg_ptr) |
|
|
627 | croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); |
|
|
628 | |
606 | SvCUR_set (sv, 0); |
629 | SvCUR_set (sv, 0); |
607 | SvLEN_set (sv, 0); |
|
|
608 | SvPVX (sv) = 0; |
630 | SvPVX (sv) = 0; |
609 | SvOK_off (sv); |
631 | SvOK_off (sv); |
610 | |
632 | |
611 | return 0; |
633 | return 0; |
612 | } |
634 | } |
… | |
… | |
662 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
684 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
663 | const_iv (EXDEV) |
685 | const_iv (EXDEV) |
664 | const_iv (ENOSYS) |
686 | const_iv (ENOSYS) |
665 | const_iv (O_RDONLY) |
687 | const_iv (O_RDONLY) |
666 | const_iv (O_WRONLY) |
688 | const_iv (O_WRONLY) |
|
|
689 | const_iv (O_RDWR) |
667 | const_iv (O_CREAT) |
690 | const_iv (O_CREAT) |
668 | const_iv (O_TRUNC) |
691 | const_iv (O_TRUNC) |
|
|
692 | const_iv (O_EXCL) |
|
|
693 | const_iv (O_APPEND) |
669 | #ifndef _WIN32 |
694 | #ifndef _WIN32 |
670 | const_iv (S_IFIFO) |
695 | const_iv (S_IFIFO) |
671 | #endif |
696 | #endif |
672 | const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) |
697 | const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) |
673 | const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
698 | const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
… | |
… | |
1364 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1389 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1365 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
1390 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
1366 | if (addr == (void *)-1) |
1391 | if (addr == (void *)-1) |
1367 | XSRETURN_NO; |
1392 | XSRETURN_NO; |
1368 | |
1393 | |
|
|
1394 | sv_force_normal (scalar); |
|
|
1395 | |
1369 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
1396 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
1370 | sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) |
1397 | sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) |
1371 | ->mg_obj = (SV *)length; |
1398 | ->mg_obj = (SV *)length; |
1372 | |
1399 | |
1373 | SvUPGRADE (scalar, SVt_PV); /* nop... */ |
1400 | SvUPGRADE (scalar, SVt_PV); /* nop... */ |
|
|
1401 | |
1374 | if (!(prot & PROT_WRITE)) |
1402 | if (!(prot & PROT_WRITE)) |
1375 | SvREADONLY_on (scalar); |
1403 | SvREADONLY_on (scalar); |
|
|
1404 | |
|
|
1405 | if (SvLEN (scalar)) |
|
|
1406 | Safefree (SvPVX (scalar)); |
1376 | |
1407 | |
1377 | SvPVX (scalar) = (char *)addr; |
1408 | SvPVX (scalar) = (char *)addr; |
1378 | SvCUR_set (scalar, length); |
1409 | SvCUR_set (scalar, length); |
1379 | SvLEN_set (scalar, 0); |
1410 | SvLEN_set (scalar, 0); |
1380 | SvPOK_only (scalar); |
1411 | SvPOK_only (scalar); |