Skip to content

Commit f0b2c17

Browse files
committed
Merge pull request #11280 from JuliaLang/jq/mmap
RFC: mmap makeover
2 parents 3e199fc + edf46b7 commit f0b2c17

File tree

11 files changed

+574
-299
lines changed

11 files changed

+574
-299
lines changed

base/datafmt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ end
4848

4949
function as_mmap(fname::AbstractString, fsz::Int64)
5050
open(fname) do io
51-
mmap_array(UInt8, (Int(fsz),), io)
51+
Mmap.mmap(io, Vector{UInt8}, (Int(fsz),))
5252
end
5353
end
5454

base/deprecated.jl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,89 @@ macro math_const(sym, val, def)
549549
end
550550

551551
export 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)

base/exports.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export
1313
Test,
1414
Libc,
1515
Libdl,
16+
Mmap,
1617
LinAlg,
1718
BLAS,
1819
LAPACK,
@@ -1142,9 +1143,6 @@ export
11421143
listenany,
11431144
ltoh,
11441145
mark,
1145-
mmap_array,
1146-
mmap_bitarray,
1147-
msync,
11481146
nb_available,
11491147
ntoh,
11501148
open,

base/libc.jl

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
module Libc
44

55
export FILE, TmStruct, strftime, strptime, getpid, gethostname, free, malloc, calloc, realloc,
6-
errno, strerror, flush_cstdio, systemsleep, time,
7-
MS_ASYNC, MS_INVALIDATE, MS_SYNC, mmap, munmap, msync
6+
errno, strerror, flush_cstdio, systemsleep, time
87

98
include("errno.jl")
109

@@ -163,64 +162,4 @@ malloc(size::Integer) = ccall(:malloc, Ptr{Void}, (Csize_t,), size)
163162
realloc(p::Ptr, size::Integer) = ccall(:realloc, Ptr{Void}, (Ptr{Void}, Csize_t), p, size)
164163
calloc(num::Integer, size::Integer) = ccall(:calloc, Ptr{Void}, (Csize_t, Csize_t), num, size)
165164

166-
## mmap ##
167-
168-
msync{T}(A::Array{T}) = msync(pointer(A), length(A)*sizeof(T))
169-
170-
msync(B::BitArray) = msync(pointer(B.chunks), length(B.chunks)*sizeof(UInt64))
171-
172-
@unix_only begin
173-
# Low-level routines
174-
# These are needed for things like MAP_ANONYMOUS
175-
function mmap(len::Integer, prot::Integer, flags::Integer, fd, offset::Integer)
176-
const pagesize::Int = ccall(:jl_getpagesize, Clong, ())
177-
# Check that none of the computations will overflow
178-
if len < 0
179-
throw(ArgumentError("requested size must be ≥ 0, got $len"))
180-
end
181-
if len > typemax(Int)-pagesize
182-
throw(ArgumentError("requested size must be ≤ $(typemax(Int)-pagesize), got $len"))
183-
end
184-
# Set the offset to a page boundary
185-
offset_page::FileOffset = floor(Integer,offset/pagesize)*pagesize
186-
len_page::Int = (offset-offset_page) + len
187-
# Mmap the file
188-
p = ccall(:jl_mmap, Ptr{Void}, (Ptr{Void}, Csize_t, Cint, Cint, Cint, FileOffset), C_NULL, len_page, prot, flags, fd, offset_page)
189-
systemerror("memory mapping failed", reinterpret(Int,p) == -1)
190-
# Also return a pointer that compensates for any adjustment in the offset
191-
return p, Int(offset-offset_page)
192-
end
193-
194-
function munmap(p::Ptr,len::Integer)
195-
systemerror("munmap", ccall(:munmap,Cint,(Ptr{Void},Int),p,len) != 0)
196-
end
197-
198-
const MS_ASYNC = 1
199-
const MS_INVALIDATE = 2
200-
const MS_SYNC = 4
201-
function msync(p::Ptr, len::Integer, flags::Integer)
202-
systemerror("msync", ccall(:msync, Cint, (Ptr{Void}, Csize_t, Cint), p, len, flags) != 0)
203-
end
204-
msync(p::Ptr, len::Integer) = msync(p, len, MS_SYNC)
205-
end
206-
207-
208-
@windows_only begin
209-
function munmap(viewhandle::Ptr, mmaphandle::Ptr)
210-
status = ccall(:UnmapViewOfFile, stdcall, Cint, (Ptr{Void},), viewhandle)!=0
211-
status |= ccall(:CloseHandle, stdcall, Cint, (Ptr{Void},), mmaphandle)!=0
212-
if !status
213-
error("could not unmap view: $(FormatMessage())")
214-
end
215-
end
216-
217-
function msync(p::Ptr, len::Integer)
218-
status = ccall(:FlushViewOfFile, stdcall, Cint, (Ptr{Void}, Csize_t), p, len)!=0
219-
if !status
220-
error("could not msync: $(FormatMessage())")
221-
end
222-
end
223-
224-
end
225-
226165
end # module

0 commit comments

Comments
 (0)