From b8477d6f0a678161a078ed2f5bab5a56eaaf9d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Wed, 1 Oct 2025 00:17:53 +0100 Subject: [PATCH 1/2] [Mmap] Turn `PAGESIZE` into a function `PAGESIZE` is currently fixed at compile time, but the machine where Julia runs may have a different memory page size than the one where the stdlib was compiled on. It's not worth making this a `OncePerProcess` object because the underlying function call is very cheap, cheaper than the overhead of `OncePerProcess`. --- stdlib/Mmap/src/Mmap.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index c527123d51bb8..04e6dd5c1ea58 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -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 @@ -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 @@ -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() @@ -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 From 28aa6889137ec1549aa3c71060b285287d5af0d3 Mon Sep 17 00:00:00 2001 From: green-br Date: Tue, 30 Sep 2025 08:59:00 +0100 Subject: [PATCH 2/2] [Mmap] Seek file back to beginning before running next test in mmap --- stdlib/Mmap/test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/Mmap/test/runtests.jl b/stdlib/Mmap/test/runtests.jl index 4ab04bf733190..ee273e8debc13 100644 --- a/stdlib/Mmap/test/runtests.jl +++ b/stdlib/Mmap/test/runtests.jl @@ -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)