diff --git a/src/headers/lz4.jl b/src/headers/lz4.jl index 374b3cd..7a95368 100644 --- a/src/headers/lz4.jl +++ b/src/headers/lz4.jl @@ -46,7 +46,20 @@ This function never writes outside `dst` buffer, nor read outside `source` buffe Returns the number of bytes written into buffer `dst` (necessarily <= dstcapacity) """ function LZ4_compress_fast(src, dst, srcsize, dstcapacity, acceleration=1) - ret = ccall((:LZ4_compress_fast, liblz4), Cint, (Ptr{UInt8}, Ptr{UInt8}, Cint, Cint, Cint), src, dst, srcsize, dstcapacity, acceleration) + src = Base.cconvert(Ptr{UInt8}, src) + dst = Base.cconvert(Ptr{UInt8}, dst) + csrc = Base.unsafe_convert(Ptr{UInt8}, src)::Ptr{UInt8} + cdst = Base.unsafe_convert(Ptr{UInt8}, dst)::Ptr{UInt8} + srcsize = convert(Cint, srcsize)::Cint + dstcapacity = convert(Cint, dstcapacity)::Cint + acceleration = convert(Cint, acceleration)::Cint + GC.@preserve src dst begin + # Allow Julia to GC while compressing + gc_state = @ccall(jl_gc_safe_enter()::Int8) + ret = ccall((:LZ4_compress_fast, liblz4), Cint, (Ptr{UInt8}, Ptr{UInt8}, Cint, Cint, Cint), csrc, cdst, srcsize, dstcapacity, acceleration) + # Leave GC-safe region, waiting for GC to complete if it's running + @ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid) + end check_compression_error(ret, "LZ4_compress_fast") end @@ -63,7 +76,20 @@ or fill `dst` buffer completely with as much data as possible from `src`. Returns number of bytes written into `dst` (necessarily <= dstcapacity) """ function LZ4_compress_destSize(src, dst, srcsize, dstcapacity) - ret = ccall((:LZ4_compress_destSize, liblz4), Cint, (Ptr{UInt8}, Ptr{UInt8}, Ptr{Cint}, Cint), src, dst, srcsize, dstcapacity) + src = Base.cconvert(Ptr{UInt8}, src) + dst = Base.cconvert(Ptr{UInt8}, dst) + srcsize = Base.cconvert(Ptr{Cint}, srcsize) + csrc = Base.unsafe_convert(Ptr{UInt8}, src)::Ptr{UInt8} + cdst = Base.unsafe_convert(Ptr{UInt8}, dst)::Ptr{UInt8} + csrcsize = Base.unsafe_convert(Ptr{Cint}, srcsize)::Ptr{Cint} + dstcapacity = convert(Cint, dstcapacity)::Cint + GC.@preserve src dst srcsize begin + # Allow Julia to GC while compressing + gc_state = @ccall(jl_gc_safe_enter()::Int8) + ret = ccall((:LZ4_compress_destSize, liblz4), Cint, (Ptr{UInt8}, Ptr{UInt8}, Ptr{Cint}, Cint), csrc, cdst, csrcsize, dstcapacity) + # Leave GC-safe region, waiting for GC to complete if it's running + @ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid) + end check_compression_error(ret, "LZ4_compress_destSize") end @@ -150,7 +176,19 @@ dstcapacity : is the size of destination buffer, which must be already allocated Returns the number of bytes decompressed into destination buffer (necessarily <= dstcapacity) """ function LZ4_decompress_safe(src, dst, cmpsize, dstcapacity) - ret = ccall((:LZ4_decompress_safe, liblz4), Cint, (Ptr{UInt8}, Ptr{UInt8}, Cint, Cint), src, dst, cmpsize, dstcapacity) + src = Base.cconvert(Ptr{UInt8}, src) + dst = Base.cconvert(Ptr{UInt8}, dst) + csrc = Base.unsafe_convert(Ptr{UInt8}, src)::Ptr{UInt8} + cdst = Base.unsafe_convert(Ptr{UInt8}, dst)::Ptr{UInt8} + cmpsize = convert(Cint, cmpsize)::Cint + dstcapacity = convert(Cint, dstcapacity)::Cint + GC.@preserve src dst begin + # Allow Julia to GC while decompressing + gc_state = @ccall(jl_gc_safe_enter()::Int8) + ret = ccall((:LZ4_decompress_safe, liblz4), Cint, (Ptr{UInt8}, Ptr{UInt8}, Cint, Cint),csrc, cdst, cmpsize, dstcapacity) + # Leave GC-safe region, waiting for GC to complete if it's running + @ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid) + end check_decompression_error(ret, "LZ4_decompress_safe") end