Skip to content

Commit

Permalink
Let ccall handle pointers (#30)
Browse files Browse the repository at this point in the history
* Move pointer conversions to Base.unsafe_convert

* Explicitly refer to out and in buffers

* Normalize path separators in include, add comments

* Bump to version 0.7.2
  • Loading branch information
mkitti authored Oct 14, 2021
1 parent ae77a0f commit 6327ffa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "6b39b394-51ab-5f42-8807-6242bab2b4c2"
license = "MIT"
authors = ["Kenta Sato <[email protected]>",
"JuliaIO Github Organization"]
version = "0.7.1"
version = "0.7.2"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
Expand Down
4 changes: 3 additions & 1 deletion src/CodecZstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import TranscodingStreams:

using Zstd_jll

include(joinpath("libzstd","LibZstd.jl"))
# Module containing directly wrapped ccalls generated by CLang.jl
include("libzstd/LibZstd.jl")
# Library functions that have a Julian interface. This file originally preceded the above module
include("libzstd.jl")
include("compression.jl")
include("decompression.jl")
Expand Down
33 changes: 21 additions & 12 deletions src/libzstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ const MAX_CLEVEL = max_clevel()

const InBuffer = LibZstd.ZSTD_inBuffer
InBuffer() = InBuffer(C_NULL, 0, 0)
Base.unsafe_convert(::Type{Ptr{InBuffer}}, buffer::InBuffer) = Ptr{InBuffer}(pointer_from_objref(buffer))
const OutBuffer = LibZstd.ZSTD_outBuffer
OutBuffer() = OutBuffer(C_NULL, 0, 0)
Base.unsafe_convert(::Type{Ptr{OutBuffer}}, buffer::OutBuffer) = Ptr{OutBuffer}(pointer_from_objref(buffer))

# ZSTD_CStream
mutable struct CStream
ptr::Ptr{Cvoid}
ptr::Ptr{LibZstd.ZSTD_CStream}
ibuffer::InBuffer
obuffer::OutBuffer

Expand All @@ -36,14 +38,18 @@ mutable struct CStream
end
end

Base.unsafe_convert(::Type{Ptr{LibZstd.ZSTD_CStream}}, cstream::CStream) = cstream.ptr
Base.unsafe_convert(::Type{Ptr{InBuffer}}, cstream::CStream) = Base.unsafe_convert(Ptr{InBuffer}, cstream.ibuffer)
Base.unsafe_convert(::Type{Ptr{OutBuffer}}, cstream::CStream) = Base.unsafe_convert(Ptr{OutBuffer}, cstream.obuffer)

function initialize!(cstream::CStream, level::Integer)
return LibZstd.ZSTD_initCStream(cstream.ptr, level)
return LibZstd.ZSTD_initCStream(cstream, level)
end

function reset!(cstream::CStream, srcsize::Integer)
# ZSTD_resetCStream is deprecated
# https://github.com/facebook/zstd/blob/9d2a45a705e22ad4817b41442949cd0f78597154/lib/zstd.h#L2253-L2272
res = LibZstd.ZSTD_CCtx_reset(cstream.ptr, LibZstd.ZSTD_reset_session_only)
res = LibZstd.ZSTD_CCtx_reset(cstream, LibZstd.ZSTD_reset_session_only)
if iserror(res)
return res
end
Expand All @@ -54,26 +60,26 @@ function reset!(cstream::CStream, srcsize::Integer)
# explicitly specified.
srcsize = ZSTD_CONTENTSIZE_UNKNOWN
end
return LibZstd.ZSTD_CCtx_setPledgedSrcSize(cstream.ptr, srcsize)
return LibZstd.ZSTD_CCtx_setPledgedSrcSize(cstream, srcsize)
#return ccall((:ZSTD_resetCStream, libzstd), Csize_t, (Ptr{Cvoid}, Culonglong), cstream.ptr, srcsize)

end

function compress!(cstream::CStream)
return LibZstd.ZSTD_compressStream(cstream.ptr, pointer_from_objref(cstream.obuffer), pointer_from_objref(cstream.ibuffer))
return LibZstd.ZSTD_compressStream(cstream, cstream.obuffer, cstream.ibuffer)
end

function finish!(cstream::CStream)
return LibZstd.ZSTD_endStream(cstream.ptr, pointer_from_objref(cstream.obuffer))
return LibZstd.ZSTD_endStream(cstream, cstream.obuffer)
end

function free!(cstream::CStream)
return LibZstd.ZSTD_freeCStream(cstream.ptr)
return LibZstd.ZSTD_freeCStream(cstream)
end

# ZSTD_DStream
mutable struct DStream
ptr::Ptr{Cvoid}
ptr::Ptr{LibZstd.ZSTD_DStream}
ibuffer::InBuffer
obuffer::OutBuffer

Expand All @@ -85,23 +91,26 @@ mutable struct DStream
return new(ptr, InBuffer(), OutBuffer())
end
end
Base.unsafe_convert(::Type{Ptr{LibZstd.ZSTD_DStream}}, dstream::DStream) = dstream.ptr
Base.unsafe_convert(::Type{Ptr{InBuffer}}, dstream::DStream) = Ptr{InBuffer}(Base.unsafe_convert(Ptr{InBuffer}, dstream.ibuffer))
Base.unsafe_convert(::Type{Ptr{OutBuffer}}, dstream::DStream) = Ptr{OutBuffer}(Base.unsafe_convert(Ptr{OutBuffer}, dstream.obuffer))

function initialize!(dstream::DStream)
return LibZstd.ZSTD_initDStream(dstream.ptr)
return LibZstd.ZSTD_initDStream(dstream)
end

function reset!(dstream::DStream)
# LibZstd.ZSTD_resetDStream is deprecated
# https://github.com/facebook/zstd/blob/9d2a45a705e22ad4817b41442949cd0f78597154/lib/zstd.h#L2332-L2339
return LibZstd.ZSTD_DCtx_reset(dstream.ptr, LibZstd.ZSTD_reset_session_only)
return LibZstd.ZSTD_DCtx_reset(dstream, LibZstd.ZSTD_reset_session_only)
end

function decompress!(dstream::DStream)
return LibZstd.ZSTD_decompressStream(dstream.ptr, pointer_from_objref(dstream.obuffer), pointer_from_objref(dstream.ibuffer))
return LibZstd.ZSTD_decompressStream(dstream, dstream.obuffer, dstream.ibuffer)
end

function free!(dstream::DStream)
return LibZstd.ZSTD_freeDStream(dstream.ptr)
return LibZstd.ZSTD_freeDStream(dstream)
end


Expand Down

2 comments on commit 6327ffa

@mkitti
Copy link
Member Author

@mkitti mkitti commented on 6327ffa Oct 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/46738

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.2 -m "<description of version>" 6327ffa9a3a12fc46d465dcfc8b30bed91cf284b
git push origin v0.7.2

Please sign in to comment.