… | |
… | |
171 | # define PAGESIZE sysconf (_SC_PAGESIZE) |
171 | # define PAGESIZE sysconf (_SC_PAGESIZE) |
172 | #endif |
172 | #endif |
173 | |
173 | |
174 | /*****************************************************************************/ |
174 | /*****************************************************************************/ |
175 | |
175 | |
|
|
176 | #if !_POSIX_MAPPED_FILES |
|
|
177 | # define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () |
|
|
178 | # define munmap(addr,length) EIO_ENOSYS () |
|
|
179 | #endif |
|
|
180 | |
|
|
181 | #if !_POSIX_MEMORY_PROTECTION |
|
|
182 | # define mprotect(addr,len,prot) EIO_ENOSYS () |
|
|
183 | #endif |
|
|
184 | |
|
|
185 | #define FOREIGN_MAGIC PERL_MAGIC_ext |
|
|
186 | |
|
|
187 | static int ecb_cold |
|
|
188 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
189 | { |
|
|
190 | int old_errno = errno; |
|
|
191 | munmap (mg->mg_ptr, (size_t)mg->mg_obj); |
|
|
192 | errno = old_errno; |
|
|
193 | |
|
|
194 | mg->mg_obj = 0; /* just in case */ |
|
|
195 | |
|
|
196 | SvREADONLY_off (sv); |
|
|
197 | |
|
|
198 | if (SvPVX (sv) != mg->mg_ptr) |
|
|
199 | croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); |
|
|
200 | |
|
|
201 | SvCUR_set (sv, 0); |
|
|
202 | SvPVX (sv) = 0; |
|
|
203 | SvOK_off (sv); |
|
|
204 | |
|
|
205 | return 0; |
|
|
206 | } |
|
|
207 | |
|
|
208 | static MGVTBL mmap_vtbl = { |
|
|
209 | 0, 0, 0, 0, mmap_free |
|
|
210 | }; |
|
|
211 | |
|
|
212 | static int ecb_cold |
|
|
213 | sysfree_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
214 | { |
|
|
215 | free (mg->mg_ptr); |
|
|
216 | mg->mg_obj = 0; /* just in case */ |
|
|
217 | |
|
|
218 | SvREADONLY_off (sv); |
|
|
219 | |
|
|
220 | if (SvPVX (sv) != mg->mg_ptr) |
|
|
221 | croak ("ERROR: IO::AIO mapped scalar changed location, detected"); |
|
|
222 | |
|
|
223 | SvCUR_set (sv, 0); |
|
|
224 | SvPVX (sv) = 0; |
|
|
225 | SvOK_off (sv); |
|
|
226 | |
|
|
227 | return 0; |
|
|
228 | } |
|
|
229 | |
|
|
230 | static MGVTBL sysfree_vtbl = { |
|
|
231 | 0, 0, 0, 0, sysfree_free |
|
|
232 | }; |
|
|
233 | |
|
|
234 | /*****************************************************************************/ |
|
|
235 | |
|
|
236 | /* helper: set scalar to foreign ptr with custom free */ |
|
|
237 | static void |
|
|
238 | sv_set_foreign (SV *sv, const MGVTBL *const vtbl, void *addr, IV length) |
|
|
239 | { |
|
|
240 | sv_force_normal (sv); |
|
|
241 | |
|
|
242 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
|
|
243 | sv_magicext (sv, 0, FOREIGN_MAGIC, vtbl, (char *)addr, 0) |
|
|
244 | ->mg_obj = (SV *)length; |
|
|
245 | |
|
|
246 | SvUPGRADE (sv, SVt_PV); /* nop... */ |
|
|
247 | |
|
|
248 | if (SvLEN (sv)) |
|
|
249 | Safefree (SvPVX (sv)); |
|
|
250 | |
|
|
251 | SvPVX (sv) = (char *)addr; |
|
|
252 | SvCUR_set (sv, length); |
|
|
253 | SvLEN_set (sv, 0); |
|
|
254 | SvPOK_only (sv); |
|
|
255 | } |
|
|
256 | |
|
|
257 | static void |
|
|
258 | sv_clear_foreign (SV *sv) |
|
|
259 | { |
|
|
260 | /* todo: iterate over magic and only free ours, but of course */ |
|
|
261 | /* the perl5porters will call that (correct) behaviour buggy */ |
|
|
262 | sv_unmagic (sv, FOREIGN_MAGIC); |
|
|
263 | } |
|
|
264 | |
|
|
265 | /*****************************************************************************/ |
|
|
266 | |
176 | static void |
267 | static void |
177 | fiemap (eio_req *req) |
268 | fiemap (eio_req *req) |
178 | { |
269 | { |
179 | req->result = -1; |
270 | req->result = -1; |
180 | |
271 | |
… | |
… | |
597 | SvSETMAGIC (req->sv2); |
688 | SvSETMAGIC (req->sv2); |
598 | PUSHs (sv_result); |
689 | PUSHs (sv_result); |
599 | } |
690 | } |
600 | break; |
691 | break; |
601 | |
692 | |
|
|
693 | case EIO_SLURP: |
|
|
694 | { |
|
|
695 | if (req->result >= 0) |
|
|
696 | { |
|
|
697 | /* if length was originally not known, we steal the malloc'ed memory */ |
|
|
698 | if (req->flags & EIO_FLAG_PTR2_FREE) |
|
|
699 | { |
|
|
700 | req->flags &= ~EIO_FLAG_PTR2_FREE; |
|
|
701 | sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); |
|
|
702 | } |
|
|
703 | else |
|
|
704 | SvCUR_set (req->sv2, req->result); |
|
|
705 | } |
|
|
706 | |
|
|
707 | PUSHs (sv_result); |
|
|
708 | } |
|
|
709 | break; |
|
|
710 | |
602 | case EIO_CUSTOM: |
711 | case EIO_CUSTOM: |
603 | if (req->feed == fiemap) |
712 | if (req->feed == fiemap) |
604 | { |
713 | { |
605 | #if HAVE_FIEMAP |
714 | #if HAVE_FIEMAP |
606 | if (!req->result) |
715 | if (!req->result) |
… | |
… | |
749 | create_respipe (); |
858 | create_respipe (); |
750 | |
859 | |
751 | if (eio_init (want_poll, done_poll) < 0) |
860 | if (eio_init (want_poll, done_poll) < 0) |
752 | croak ("IO::AIO: unable to initialise eio library"); |
861 | croak ("IO::AIO: unable to initialise eio library"); |
753 | } |
862 | } |
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 | |
|
|
768 | static int ecb_cold |
|
|
769 | mmap_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 | |
|
|
789 | static MGVTBL mmap_vtbl = { |
|
|
790 | 0, 0, 0, 0, mmap_free |
|
|
791 | }; |
|
|
792 | |
863 | |
793 | /*****************************************************************************/ |
864 | /*****************************************************************************/ |
794 | |
865 | |
795 | static SV * |
866 | static SV * |
796 | get_cb (SV *cb_sv) |
867 | get_cb (SV *cb_sv) |
… | |
… | |
1344 | } |
1415 | } |
1345 | else |
1416 | else |
1346 | { |
1417 | { |
1347 | /* read: check type and grow scalar as necessary */ |
1418 | /* read: check type and grow scalar as necessary */ |
1348 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1419 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1349 | svptr = sv_grow (data, len + dataoffset + 1); |
1420 | svptr = sv_grow (data, len + dataoffset); |
1350 | else if (SvCUR (data) < len + dataoffset) |
1421 | else if (SvCUR (data) < len + dataoffset) |
1351 | croak ("length + dataoffset outside of scalar, and cannot grow"); |
1422 | croak ("length + dataoffset outside of scalar, and cannot grow"); |
1352 | } |
1423 | } |
1353 | |
1424 | |
1354 | { |
1425 | { |
… | |
… | |
1735 | req->int2 = flags; |
1806 | req->int2 = flags; |
1736 | req->int3 = SvOK (count) ? SvIV (count) : -1; |
1807 | req->int3 = SvOK (count) ? SvIV (count) : -1; |
1737 | #endif |
1808 | #endif |
1738 | |
1809 | |
1739 | REQ_SEND; |
1810 | REQ_SEND; |
|
|
1811 | } |
|
|
1812 | |
|
|
1813 | void |
|
|
1814 | aio_slurp (SV *pathname, off_t offset, UV length, SV8 *data, SV *callback = &PL_sv_undef) |
|
|
1815 | PPCODE: |
|
|
1816 | { |
|
|
1817 | char *svptr = 0; |
|
|
1818 | |
|
|
1819 | sv_clear_foreign (data); |
|
|
1820 | |
|
|
1821 | if (length) /* known length, directly read into scalar */ |
|
|
1822 | { |
|
|
1823 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
|
|
1824 | svptr = sv_grow (data, length); |
|
|
1825 | else if (SvCUR (data) < length) |
|
|
1826 | croak ("length outside of scalar, and cannot grow"); |
|
|
1827 | else |
|
|
1828 | svptr = SvPVbyte_nolen (data); |
|
|
1829 | } |
|
|
1830 | |
|
|
1831 | { |
|
|
1832 | dREQ; |
|
|
1833 | |
|
|
1834 | req->type = EIO_SLURP; |
|
|
1835 | req_set_path1 (req, pathname); |
|
|
1836 | req->offs = offset; |
|
|
1837 | req->size = length; |
|
|
1838 | req->sv2 = SvREFCNT_inc (data); |
|
|
1839 | req->ptr2 = svptr; |
|
|
1840 | |
|
|
1841 | if (!SvREADONLY (data)) |
|
|
1842 | { |
|
|
1843 | SvREADONLY_on (data); |
|
|
1844 | req->flags |= FLAG_SV2_RO_OFF; |
|
|
1845 | } |
|
|
1846 | |
|
|
1847 | REQ_SEND; |
|
|
1848 | } |
1740 | } |
1849 | } |
1741 | |
1850 | |
1742 | void |
1851 | void |
1743 | aio_busy (double delay, SV *callback = &PL_sv_undef) |
1852 | aio_busy (double delay, SV *callback = &PL_sv_undef) |
1744 | PPCODE: |
1853 | PPCODE: |
… | |
… | |
1881 | RETVAL |
1990 | RETVAL |
1882 | |
1991 | |
1883 | void |
1992 | void |
1884 | mmap (SV *scalar, STRLEN length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) |
1993 | mmap (SV *scalar, STRLEN length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) |
1885 | PPCODE: |
1994 | PPCODE: |
1886 | sv_unmagic (scalar, MMAP_MAGIC); |
1995 | sv_clear_foreign (scalar); |
1887 | { |
1996 | { |
1888 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1997 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1889 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
1998 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
1890 | if (addr == (void *)-1) |
1999 | if (addr == (void *)-1) |
1891 | XSRETURN_NO; |
2000 | XSRETURN_NO; |
1892 | |
2001 | |
1893 | sv_force_normal (scalar); |
2002 | 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 | |
2003 | |
1901 | if (!(prot & PROT_WRITE)) |
2004 | if (!(prot & PROT_WRITE)) |
1902 | SvREADONLY_on (scalar); |
2005 | SvREADONLY_on (scalar); |
1903 | |
2006 | |
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; |
2007 | XSRETURN_YES; |
1913 | } |
2008 | } |
1914 | |
2009 | |
1915 | void |
2010 | void |
1916 | munmap (SV *scalar) |
2011 | munmap (SV *scalar) |
1917 | CODE: |
2012 | CODE: |
1918 | sv_unmagic (scalar, MMAP_MAGIC); |
2013 | sv_clear_foreign (scalar); |
1919 | |
2014 | |
1920 | int |
2015 | int |
1921 | madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) |
2016 | madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) |
1922 | ALIAS: |
2017 | ALIAS: |
1923 | mprotect = 1 |
2018 | mprotect = 1 |