@@ -549,3 +549,89 @@ macro math_const(sym, val, def)
549549end
550550
551551export MathConst, @math_const
552+
553+ # 11280, mmap
554+
555+ export msync
556+ msync {T} (A:: Array{T} ) = msync (pointer (A), length (A)* sizeof (T))
557+ msync (B:: BitArray ) = msync (pointer (B. chunks), length (B. chunks)* sizeof (UInt64))
558+
559+ @unix_only begin
560+
561+ function mmap (len:: Integer , prot:: Integer , flags:: Integer , fd, offset:: Integer )
562+ depwarn (" `mmap` is deprecated, use `mmap(io, Array{T,N}, dims, offset)` instead to return an mmapped-array" , :mmap )
563+ const pagesize:: Int = ccall (:jl_getpagesize , Clong, ())
564+ # Check that none of the computations will overflow
565+ if len < 0
566+ throw (ArgumentError (" requested size must be ≥ 0, got $len " ))
567+ end
568+ if len > typemax (Int)- pagesize
569+ throw (ArgumentError (" requested size must be ≤ $(typemax (Int)- pagesize) , got $len " ))
570+ end
571+ # Set the offset to a page boundary
572+ offset_page:: FileOffset = floor (Integer,offset/ pagesize)* pagesize
573+ len_page:: Int = (offset- offset_page) + len
574+ # Mmap the file
575+ p = ccall (:jl_mmap , Ptr{Void}, (Ptr{Void}, Csize_t, Cint, Cint, Cint, FileOffset), C_NULL , len_page, prot, flags, fd, offset_page)
576+ systemerror (" memory mapping failed" , reinterpret (Int,p) == - 1 )
577+ # Also return a pointer that compensates for any adjustment in the offset
578+ return p, Int (offset- offset_page)
579+ end
580+
581+ function munmap (p:: Ptr ,len:: Integer )
582+ depwarn (" `munmap` is deprecated, `mmap` Arrays are automatically munmapped when finalized" , :munmap )
583+ systemerror (" munmap" , ccall (:munmap ,Cint,(Ptr{Void},Int),p,len) != 0 )
584+ end
585+
586+ const MS_ASYNC = 1
587+ const MS_INVALIDATE = 2
588+ const MS_SYNC = 4
589+ function msync (p:: Ptr , len:: Integer , flags:: Integer = MS_SYNC)
590+ depwarn (" `msync` is deprecated, use `Mmap.sync!(array)` instead" , :msync )
591+ systemerror (" msync" , ccall (:msync , Cint, (Ptr{Void}, Csize_t, Cint), p, len, flags) != 0 )
592+ end
593+ end
594+
595+
596+ @windows_only begin
597+ function munmap (viewhandle:: Ptr , mmaphandle:: Ptr )
598+ depwarn (" `munmap` is deprecated, `mmap` Arrays are automatically munmapped when finalized" , :munmap )
599+ status = ccall (:UnmapViewOfFile , stdcall, Cint, (Ptr{Void},), viewhandle)!= 0
600+ status |= ccall (:CloseHandle , stdcall, Cint, (Ptr{Void},), mmaphandle)!= 0
601+ if ! status
602+ error (" could not unmap view: $(FormatMessage ()) " )
603+ end
604+ end
605+
606+ function msync (p:: Ptr , len:: Integer )
607+ depwarn (" `msync` is deprecated, use `Mmap.sync!(array)` instead" , :msync )
608+ status = ccall (:FlushViewOfFile , stdcall, Cint, (Ptr{Void}, Csize_t), p, len)!= 0
609+ if ! status
610+ error (" could not msync: $(FormatMessage ()) " )
611+ end
612+ end
613+
614+ end
615+
616+ @unix_only @deprecate mmap_array {T,N} (:: Type{T} , dims:: NTuple{N,Integer} , s:: IO , offset= position (s)) mmap (s, Array{T,N}, dims, offset)
617+
618+ @windows_only begin
619+ type SharedMemSpec
620+ name :: AbstractString
621+ readonly :: Bool
622+ create :: Bool
623+ end
624+ export mmap_array
625+ function mmap_array {T,N} (:: Type{T} , dims:: NTuple{N,Integer} , s:: Union(IO,SharedMemSpec) , offset:: FileOffset )
626+ depwarn (" `mmap_array` is deprecated, use `mmap(io, Array{T,N}, dims, offset)` instead to return an mmapped-array" , :mmap_array )
627+ if isa (s,SharedMemSpec)
628+ a = Mmap. AnonymousMmap (s. name, s. readonly, s. create)
629+ else
630+ a = s
631+ end
632+ return mmap (a, Array{T,N}, dims, offset)
633+ end
634+ end
635+
636+ @deprecate mmap_bitarray {N} (:: Type{Bool} , dims:: NTuple{N,Integer} , s:: IOStream , offset:: FileOffset = position (s)) mmap (s, BitArray, dims, offset)
637+ @deprecate mmap_bitarray {N} (dims:: NTuple{N,Integer} , s:: IOStream , offset= position (s)) mmap (s, BitArray, dims, offset)
0 commit comments