From f890855dea1410fd2e26e570c4b2eb585f15e3eb Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 9 Sep 2024 14:25:28 -0400 Subject: [PATCH] remove raiicontext and replace close() calls --- src/workerd/api/node/zlib-util.c++ | 24 ++++++------------------ src/workerd/api/node/zlib-util.h | 15 ++++++--------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/workerd/api/node/zlib-util.c++ b/src/workerd/api/node/zlib-util.c++ index 3493b1a283a3..9c6d55b7d0da 100644 --- a/src/workerd/api/node/zlib-util.c++ +++ b/src/workerd/api/node/zlib-util.c++ @@ -397,7 +397,7 @@ kj::Maybe ZlibContext::setParams(int _level, int _strategy) { return kj::none; } -void ZlibContext::close() { +ZlibContext::~ZlibContext() { if (!initialized) { dictionary.clear(); mode = ZlibMode::NONE; @@ -527,7 +527,7 @@ void ZlibUtil::CompressionStream::close() { } closed = true; JSG_ASSERT(initialized, Error, "Closing before initialized"_kj); - context()->close(); + // Context is closed on the destructor. } template @@ -655,7 +655,7 @@ BrotliEncoderContext::BrotliEncoderContext(ZlibMode _mode): BrotliContext(_mode) state = kj::disposeWith(instance); } -void BrotliEncoderContext::close() { +BrotliEncoderContext::~BrotliEncoderContext() { auto instance = BrotliEncoderCreateInstance(alloc_brotli, free_brotli, alloc_opaque_brotli); state = kj::disposeWith(kj::mv(instance)); mode = ZlibMode::NONE; @@ -713,7 +713,7 @@ BrotliDecoderContext::BrotliDecoderContext(ZlibMode _mode): BrotliContext(_mode) state = kj::disposeWith(instance); } -void BrotliDecoderContext::close() { +BrotliDecoderContext::~BrotliDecoderContext() { auto instance = BrotliDecoderCreateInstance(alloc_brotli, free_brotli, alloc_opaque_brotli); state = kj::disposeWith(kj::mv(instance)); mode = ZlibMode::NONE; @@ -840,18 +840,6 @@ void ZlibUtil::CompressionStream::FreeForZlib(void* data, vo JSG_REQUIRE(ctx->allocations.erase(real_pointer), Error, "Zlib allocation should exist"_kj); } namespace { -// A RAII wrapper around a compression context class -// TODO(soon): See if this functionality can just be embedded into each CompressionContext -template -class ContextRAII: public CompressionContext { -public: - using CompressionContext::CompressionContext; - - ~ContextRAII() { - static_cast(this)->close(); - } -}; - template static kj::Array syncProcessBuffer(Context& ctx, GrowableBuffer& result) { do { @@ -873,7 +861,7 @@ static kj::Array syncProcessBuffer(Context& ctx, GrowableBuffer& resul kj::Array ZlibUtil::zlibSync( ZlibUtil::InputSource data, ZlibContext::Options opts, ZlibModeValue mode) { - ContextRAII ctx(static_cast(mode)); + ZlibContext ctx(static_cast(mode)); auto chunkSize = opts.chunkSize.orDefault(ZLIB_PERFORMANT_CHUNK_SIZE); auto maxOutputLength = opts.maxOutputLength.orDefault(Z_MAX_CHUNK); @@ -922,7 +910,7 @@ void ZlibUtil::zlibWithCallback(jsg::Lock& js, template kj::Array ZlibUtil::brotliSync(InputSource data, BrotliContext::Options opts) { - ContextRAII ctx(Context::Mode); + Context ctx(Context::Mode); auto chunkSize = opts.chunkSize.orDefault(ZLIB_PERFORMANT_CHUNK_SIZE); auto maxOutputLength = opts.maxOutputLength.orDefault(Z_MAX_CHUNK); diff --git a/src/workerd/api/node/zlib-util.h b/src/workerd/api/node/zlib-util.h index d617ee9126cd..67c928009eb5 100644 --- a/src/workerd/api/node/zlib-util.h +++ b/src/workerd/api/node/zlib-util.h @@ -90,15 +90,14 @@ struct CompressionError { int err; }; -// TODO(soon): See if RAII support can be added directly to this class, and we can mark it final -class ZlibContext { +class ZlibContext final { public: explicit ZlibContext(ZlibMode _mode): mode(_mode) {} ZlibContext() = default; + ~ZlibContext(); KJ_DISALLOW_COPY(ZlibContext); - void close(); void setBuffers(kj::ArrayPtr input, uint32_t inputLength, kj::ArrayPtr output, @@ -244,13 +243,12 @@ class BrotliContext { void* alloc_opaque_brotli = nullptr; }; -// TODO(soon): See if RAII support can be added directly to this class, and we can mark it final -class BrotliEncoderContext: public BrotliContext { +class BrotliEncoderContext final: public BrotliContext { public: static const ZlibMode Mode = ZlibMode::BROTLI_ENCODE; explicit BrotliEncoderContext(ZlibMode _mode); + ~BrotliEncoderContext(); - void close(); // Equivalent to Node.js' `DoThreadPoolWork` implementation. void work(); kj::Maybe initialize( @@ -264,13 +262,12 @@ class BrotliEncoderContext: public BrotliContext { kj::Own state; }; -// TODO(soon): See if RAII support can be added directly to this class, and we can mark it final -class BrotliDecoderContext: public BrotliContext { +class BrotliDecoderContext final: public BrotliContext { public: static const ZlibMode Mode = ZlibMode::BROTLI_DECODE; explicit BrotliDecoderContext(ZlibMode _mode); + ~BrotliDecoderContext(); - void close(); // Equivalent to Node.js' `DoThreadPoolWork` implementation. void work(); kj::Maybe initialize(