Skip to content

Commit

Permalink
Merge pull request #31 from JuliaComputing/jb/align8
Browse files Browse the repository at this point in the history
make sure alignment is always a multiple of 8
  • Loading branch information
joshday authored Jul 1, 2019
2 parents 86cf0ca + 99bdef5 commit 18869cc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ can_mmap(io::IOStream) = true
can_mmap(io::IO) = false

function padalign(io::IOStream, align=8)
align = (align + 7) & -8 # make sure alignment is a multiple of 8
p = position(io)
if p & (align-1) !== 0
write(io, zeros(UInt8, (align-p % align)))
if p & (align-1) != 0
write(io, zeros(UInt8, (align - (p % align))))
end
end
padalign(io, align=8) = nothing

function seekpadded(io::IOStream, align=8)
align = (align + 7) & -8
p = position(io)
if p & (align-1) !== 0
seek(io, p+(align-p%align))
if p & (align-1) != 0
seek(io, p + (align - (p % align)))
end
end
seekpadded(io, align=8) = nothing
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ function roundtrip(x, eq=(==), io=IOBuffer())
end
end

primitive type TestInt160 160 end

@testset "Array" begin
roundtrip(rand(10))
roundtrip(BitArray(rand(Bool,10)))
roundtrip(map(Ref, rand(10)), (x,y)->getindex.(x) == getindex.(y))
mktemp() do path, f
roundtrip(Vector{TestInt160}(undef, 10), (x,y)->true, f)
end

io = IOBuffer()
x = Array{Union{}}(undef, 10)
Expand Down

0 comments on commit 18869cc

Please sign in to comment.