Skip to content

Commit

Permalink
Merge pull request JuliaLang#13695 from JuliaLang/jq/mmap_0_len
Browse files Browse the repository at this point in the history
Allow 0-length arrays to be returned from Mmap.mmap. Fixes JuliaLang#10516
  • Loading branch information
stevengj committed Oct 21, 2015
2 parents 976ff98 + a6f4615 commit 1079755
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion base/mmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ function mmap{T,N}(io::IO,
isbits(T) || throw(ArgumentError("unable to mmap $T; must satisfy isbits(T) == true"))

len = prod(dims) * sizeof(T)
len > 0 || throw(ArgumentError("requested size must be > 0, got $len"))
len >= 0 || throw(ArgumentError("requested size must be ≥ 0, got $len"))
len == 0 && return Array(T,ntuple(x->0,N))
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"))
Expand Down
10 changes: 5 additions & 5 deletions test/mmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ gc(); gc()
gc(); gc()
@test Mmap.mmap(file, Array{UInt8,3}, (1,1,11)) == reshape(t,(1,1,11))
gc(); gc()
@test_throws ArgumentError Mmap.mmap(file, Array{UInt8,3}, (11,0,1)) # 0-dimension results in len=0
@test Mmap.mmap(file, Array{UInt8,3}, (11,0,1)) == Array(UInt8,(0,0,0))
@test Mmap.mmap(file, Vector{UInt8}, (11,)) == t
gc(); gc()
@test Mmap.mmap(file, Array{UInt8,2}, (1,11)) == t'
gc(); gc()
@test_throws ArgumentError Mmap.mmap(file, Array{UInt8,2}, (0,12))
@test Mmap.mmap(file, Array{UInt8,2}, (0,12)) == Array(UInt8,(0,0))
m = Mmap.mmap(file, Array{UInt8,3}, (1,2,1))
@test m == reshape("He".data,(1,2,1))
finalize(m); m=nothing; gc()
Expand Down Expand Up @@ -48,8 +48,8 @@ close(s)
gc(); gc()

s = open(f->f,file,"w")
@test_throws ArgumentError Mmap.mmap(file) # requested len=0 on empty file
@test_throws ArgumentError Mmap.mmap(file,Vector{UInt8},0)
@test Mmap.mmap(file) == Array(UInt8, 0) # requested len=0 on empty file
@test Mmap.mmap(file,Vector{UInt8},0) == Array(UInt8, 0)
m = Mmap.mmap(file,Vector{UInt8},12)
m[:] = "Hello World\n".data
Mmap.sync!(m)
Expand Down Expand Up @@ -284,4 +284,4 @@ n = similar(m, (2,2))
n = similar(m, 12)
@test length(n) == 12
@test size(n) == (12,)
finalize(m); m = nothing; gc()
finalize(m); m = nothing; gc()

0 comments on commit 1079755

Please sign in to comment.