Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions stdlib/Mmap/src/Mmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Base: OS_HANDLE, INVALID_OS_HANDLE

export mmap

const PAGESIZE = Int(Sys.isunix() ? ccall(:jl_getpagesize, Clong, ()) : ccall(:jl_getallocationgranularity, Clong, ()))
pagesize() = Int(Sys.isunix() ? ccall(:jl_getpagesize, Clong, ()) : ccall(:jl_getallocationgranularity, Clong, ()))

# for mmaps not backed by files
mutable struct Anonymous <: IO
Expand Down Expand Up @@ -200,12 +200,12 @@ function mmap(io::IO,
end
len >= 0 || throw(ArgumentError("requested size must be ≥ 0, got $len"))
len == 0 && return Array{T}(undef, ntuple(x->0,Val(N)))
len < typemax(Int) - PAGESIZE || throw(ArgumentError("requested size must be < $(typemax(Int)-PAGESIZE), got $len"))
len < typemax(Int) - pagesize() || throw(ArgumentError("requested size must be < $(typemax(Int)-pagesize()), got $len"))

offset >= 0 || throw(ArgumentError("requested offset must be ≥ 0, got $offset"))

# shift `offset` to start of page boundary
offset_page::Int64 = div(offset, PAGESIZE) * PAGESIZE
offset_page::Int64 = div(offset, pagesize()) * pagesize()
# add (offset - offset_page) to `len` to get total length of memory-mapped region
mmaplen = (offset - offset_page) + len

Expand Down Expand Up @@ -365,7 +365,7 @@ Forces synchronization between the in-memory version of a memory-mapped `Array`
"""
function sync!(m::Array, flags::Integer=MS_SYNC)
ptr = pointer(m)
offset = rem(UInt(ptr), PAGESIZE)
offset = rem(UInt(ptr), pagesize())
ptr = ptr - offset
mmaplen = sizeof(m) + offset
GC.@preserve m @static if Sys.isunix()
Expand Down Expand Up @@ -428,7 +428,7 @@ Advises the kernel on the intended usage of the memory-mapped `array`, with the
"""
function madvise!(m::Array, flag::Integer=MADV_NORMAL)
ptr = pointer(m)
offset = rem(UInt(ptr), PAGESIZE)
offset = rem(UInt(ptr), pagesize())
ptr = ptr - offset
mmaplen = sizeof(m) + offset
GC.@preserve m begin
Expand Down
1 change: 1 addition & 0 deletions stdlib/Mmap/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ A2 = mmap(s, Matrix{Int}, (m,n))
seek(s, 0)
A3 = mmap(s, Matrix{Int}, (m,n), convert(Int64, 2*sizeof(Int)))
@test A == A3
seek(s, 0)
A4 = mmap(s, Matrix{Int}, (m,150), convert(Int64, (2+150*m)*sizeof(Int)))
@test A[:, 151:end] == A4
close(s)
Expand Down