Skip to content

Commit 4f182f8

Browse files
committed
Auto initialize in startproc
1 parent e7edfed commit 4f182f8

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/compression.jl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,6 @@ end
7878
# Methods
7979
# -------
8080

81-
function TranscodingStreams.initialize(codec::ZstdCompressor)
82-
code = initialize!(codec.cstream, codec.level)
83-
if iserror(code)
84-
zstderror(codec.cstream, code)
85-
end
86-
reset!(codec.cstream.ibuffer)
87-
reset!(codec.cstream.obuffer)
88-
return
89-
end
90-
9181
function TranscodingStreams.finalize(codec::ZstdCompressor)
9282
if codec.cstream.ptr != C_NULL
9383
code = free!(codec.cstream)
@@ -96,12 +86,22 @@ function TranscodingStreams.finalize(codec::ZstdCompressor)
9686
end
9787
codec.cstream.ptr = C_NULL
9888
end
99-
reset!(codec.cstream.ibuffer)
100-
reset!(codec.cstream.obuffer)
101-
return
89+
nothing
10290
end
10391

10492
function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error::Error)
93+
if codec.cstream.ptr == C_NULL
94+
ptr = LibZstd.ZSTD_createCStream()
95+
if ptr == C_NULL
96+
throw(OutOfMemoryError())
97+
end
98+
codec.cstream.ptr = ptr
99+
i_code = initialize!(codec.cstream, codec.level)
100+
if iserror(i_code)
101+
error[] = ErrorException("zstd error")
102+
return :error
103+
end
104+
end
105105
code = reset!(codec.cstream, 0 #=unknown source size=#)
106106
if iserror(code)
107107
error[] = ErrorException("zstd error")
@@ -111,6 +111,9 @@ function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error
111111
end
112112

113113
function TranscodingStreams.process(codec::ZstdCompressor, input::Memory, output::Memory, error::Error)
114+
if codec.cstream.ptr == C_NULL
115+
error("startproc must be called before process")
116+
end
114117
cstream = codec.cstream
115118
ibuffer_starting_pos = UInt(0)
116119
if codec.endOp == LibZstd.ZSTD_e_end &&

src/libzstd.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ mutable struct CStream
4444
obuffer::OutBuffer
4545

4646
function CStream()
47-
ptr = LibZstd.ZSTD_createCStream()
48-
if ptr == C_NULL
49-
throw(OutOfMemoryError())
50-
end
51-
return new(ptr, InBuffer(), OutBuffer())
47+
return new(C_NULL, InBuffer(), OutBuffer())
5248
end
5349
end
5450

test/compress_endOp.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using Test
44
@testset "compress! endOp = :continue" begin
55
data = rand(1:100, 1024*1024)
66
cstream = CodecZstd.CStream()
7+
cstream.ptr = CodecZstd.LibZstd.ZSTD_createCStream()
78
cstream.ibuffer.src = pointer(data)
89
cstream.ibuffer.size = sizeof(data)
910
cstream.ibuffer.pos = 0
@@ -24,6 +25,7 @@ end
2425
@testset "compress! endOp = :flush" begin
2526
data = rand(1:100, 1024*1024)
2627
cstream = CodecZstd.CStream()
28+
cstream.ptr = CodecZstd.LibZstd.ZSTD_createCStream()
2729
cstream.ibuffer.src = pointer(data)
2830
cstream.ibuffer.size = sizeof(data)
2931
cstream.ibuffer.pos = 0
@@ -43,6 +45,7 @@ end
4345
@testset "compress! endOp = :end" begin
4446
data = rand(1:100, 1024*1024)
4547
cstream = CodecZstd.CStream()
48+
cstream.ptr = CodecZstd.LibZstd.ZSTD_createCStream()
4649
cstream.ibuffer.src = pointer(data)
4750
cstream.ibuffer.size = sizeof(data)
4851
cstream.ibuffer.pos = 0

0 commit comments

Comments
 (0)