-
Notifications
You must be signed in to change notification settings - Fork 23
Add finalizer to ZStream and make TranscodingStreams.finalize
a noop
#97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,18 @@ | |
reserved::Culong | ||
end | ||
|
||
@assert typemax(Csize_t) ≥ typemax(Cuint) | ||
|
||
function zalloc(::Ptr{Cvoid}, items::Cuint, size::Cuint)::Ptr{Cvoid} | ||
s, f = Base.Checked.mul_with_overflow(items, size) | ||
if f | ||
C_NULL | ||
else | ||
ccall(:jl_malloc, Ptr{Cvoid}, (Csize_t,), s%Csize_t) | ||
end | ||
end | ||
zfree(::Ptr{Cvoid}, p::Ptr{Cvoid}) = ccall(:jl_free, Cvoid, (Ptr{Cvoid},), p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They have different signatures. |
||
|
||
function ZStream() | ||
ZStream( | ||
# input | ||
|
@@ -32,7 +44,9 @@ | |
# message and state | ||
C_NULL, C_NULL, | ||
# memory allocation | ||
C_NULL, C_NULL, C_NULL, | ||
@cfunction(zalloc, Ptr{Cvoid}, (Ptr{Cvoid}, Cuint, Cuint)), | ||
@cfunction(zfree, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid})), | ||
C_NULL, | ||
# data type, adler and reserved | ||
0, 0, 0) | ||
end | ||
|
@@ -83,6 +97,11 @@ | |
return ccall((:deflateEnd, libz), Cint, (Ref{ZStream},), zstream) | ||
end | ||
|
||
function compress_finalizer!(zstream::ZStream) | ||
deflate_end!(zstream) | ||
nothing | ||
end | ||
|
||
function deflate!(zstream::ZStream, flush::Integer) | ||
return ccall((:deflate, libz), Cint, (Ref{ZStream}, Cint), zstream, flush) | ||
end | ||
|
@@ -99,6 +118,11 @@ | |
return ccall((:inflateEnd, libz), Cint, (Ref{ZStream},), zstream) | ||
end | ||
|
||
function decompress_finalizer!(zstream::ZStream) | ||
inflate_end!(zstream) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if this gets called twice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing.
The same happens in Also, since the |
||
nothing | ||
end | ||
|
||
function inflate!(zstream::ZStream, flush::Integer) | ||
return ccall((:inflate, libz), Cint, (Ref{ZStream}, Cint), zstream, flush) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we use
ccall
here rather thanLibc.malloc
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to make sure the memory being used is visible to the GC https://discourse.julialang.org/t/c-struct-garbage-collection-not-run-frequently-enough/116919/14
If I replace this with
Libc.malloc
Julia uses more memory in the memory leak test, because GC isn't run frequently enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some reference within the C code suggesting that
calloc
may be warranted here.https://github.com/madler/zlib/blob/develop/deflate.c#L397
The comments here suggest that malloc may be an appropriate optimization on modern systems.
madler/zlib#517
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it looks like
calloc
is only needed if pointers are 16 bits.