Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 94dbb36

Browse filesBrowse files
addaleaxaduh95
authored andcommitted
src: do not store compression methods on Brotli classes
This addresses a long-standing TODO comment, referencing the fact that these values are either known at compile time or can be inferred from the `this` value in the context class. PR-URL: #61717 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent bef661f commit 94dbb36
Copy full SHA for 94dbb36

1 file changed

+27-43Lines changed: 27 additions & 43 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_zlib.cc‎

Copy file name to clipboardExpand all lines: src/node_zlib.cc
+27-43Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,11 @@ class ZlibContext final : public MemoryRetainer {
187187
CompressionError ResetStream();
188188

189189
// Zlib-specific:
190-
void Init(int level, int window_bits, int mem_level, int strategy,
190+
void Init(int level,
191+
int window_bits,
192+
int mem_level,
193+
int strategy,
191194
std::vector<unsigned char>&& dictionary);
192-
void SetAllocationFunctions(alloc_func alloc, free_func free, void* opaque);
193195
CompressionError SetParams(int level, int strategy);
194196

195197
SET_MEMORY_INFO_NAME(ZlibContext)
@@ -243,21 +245,14 @@ class BrotliContext : public MemoryRetainer {
243245
size_t avail_in_ = 0;
244246
size_t avail_out_ = 0;
245247
BrotliEncoderOperation flush_ = BROTLI_OPERATION_PROCESS;
246-
// TODO(addaleax): These should not need to be stored here.
247-
// This is currently only done this way to make implementing ResetStream()
248-
// easier.
249-
brotli_alloc_func alloc_ = nullptr;
250-
brotli_free_func free_ = nullptr;
251248
void* alloc_opaque_ = nullptr;
252249
};
253250

254251
class BrotliEncoderContext final : public BrotliContext {
255252
public:
256253
void Close();
257254
void DoThreadPoolWork();
258-
CompressionError Init(brotli_alloc_func alloc,
259-
brotli_free_func free,
260-
void* opaque);
255+
CompressionError Init();
261256
CompressionError ResetStream();
262257
CompressionError SetParams(int key, uint32_t value);
263258
CompressionError GetErrorInfo() const;
@@ -275,9 +270,7 @@ class BrotliDecoderContext final : public BrotliContext {
275270
public:
276271
void Close();
277272
void DoThreadPoolWork();
278-
CompressionError Init(brotli_alloc_func alloc,
279-
brotli_free_func free,
280-
void* opaque);
273+
CompressionError Init();
281274
CompressionError ResetStream();
282275
CompressionError SetParams(int key, uint32_t value);
283276
CompressionError GetErrorInfo() const;
@@ -783,8 +776,6 @@ class ZlibStream final : public CompressionStream<ZlibContext> {
783776
wrap->InitStream(write_result, write_js_callback);
784777

785778
AllocScope alloc_scope(wrap);
786-
wrap->context()->SetAllocationFunctions(
787-
AllocForZlib, FreeForZlib, wrap->as_allocator_opaque_value());
788779
wrap->context()->Init(level, window_bits, mem_level, strategy,
789780
std::move(dictionary));
790781
}
@@ -846,10 +837,7 @@ class BrotliCompressionStream final :
846837
wrap->InitStream(write_result, write_js_callback);
847838

848839
AllocScope alloc_scope(wrap);
849-
CompressionError err = wrap->context()->Init(
850-
CompressionStream<CompressionContext>::AllocForBrotli,
851-
CompressionStream<CompressionContext>::FreeForZlib,
852-
wrap->as_allocator_opaque_value());
840+
CompressionError err = wrap->context()->Init();
853841
if (err.IsError()) {
854842
wrap->EmitError(err);
855843
// TODO(addaleax): Sometimes we generate better error codes in C++ land,
@@ -1208,19 +1196,15 @@ CompressionError ZlibContext::ResetStream() {
12081196
return SetDictionary();
12091197
}
12101198

1211-
1212-
void ZlibContext::SetAllocationFunctions(alloc_func alloc,
1213-
free_func free,
1214-
void* opaque) {
1215-
strm_.zalloc = alloc;
1216-
strm_.zfree = free;
1217-
strm_.opaque = opaque;
1218-
}
1219-
1220-
12211199
void ZlibContext::Init(
12221200
int level, int window_bits, int mem_level, int strategy,
12231201
std::vector<unsigned char>&& dictionary) {
1202+
// Set allocation functions
1203+
strm_.zalloc = CompressionStreamMemoryOwner::AllocForZlib;
1204+
strm_.zfree = CompressionStreamMemoryOwner::FreeForZlib;
1205+
strm_.opaque =
1206+
CompressionStream<ZlibContext>::AllocatorOpaquePointerForContext(this);
1207+
12241208
if (!((window_bits == 0) &&
12251209
(mode_ == INFLATE ||
12261210
mode_ == GUNZIP ||
@@ -1402,12 +1386,12 @@ void BrotliEncoderContext::Close() {
14021386
mode_ = NONE;
14031387
}
14041388

1405-
CompressionError BrotliEncoderContext::Init(brotli_alloc_func alloc,
1406-
brotli_free_func free,
1407-
void* opaque) {
1408-
alloc_ = alloc;
1409-
free_ = free;
1410-
alloc_opaque_ = opaque;
1389+
CompressionError BrotliEncoderContext::Init() {
1390+
brotli_alloc_func alloc = CompressionStreamMemoryOwner::AllocForBrotli;
1391+
brotli_free_func free = CompressionStreamMemoryOwner::FreeForZlib;
1392+
void* opaque =
1393+
CompressionStream<BrotliEncoderContext>::AllocatorOpaquePointerForContext(
1394+
this);
14111395
state_.reset(BrotliEncoderCreateInstance(alloc, free, opaque));
14121396
if (!state_) {
14131397
return CompressionError("Could not initialize Brotli instance",
@@ -1419,7 +1403,7 @@ CompressionError BrotliEncoderContext::Init(brotli_alloc_func alloc,
14191403
}
14201404

14211405
CompressionError BrotliEncoderContext::ResetStream() {
1422-
return Init(alloc_, free_, alloc_opaque_);
1406+
return Init();
14231407
}
14241408

14251409
CompressionError BrotliEncoderContext::SetParams(int key, uint32_t value) {
@@ -1467,12 +1451,12 @@ void BrotliDecoderContext::DoThreadPoolWork() {
14671451
}
14681452
}
14691453

1470-
CompressionError BrotliDecoderContext::Init(brotli_alloc_func alloc,
1471-
brotli_free_func free,
1472-
void* opaque) {
1473-
alloc_ = alloc;
1474-
free_ = free;
1475-
alloc_opaque_ = opaque;
1454+
CompressionError BrotliDecoderContext::Init() {
1455+
brotli_alloc_func alloc = CompressionStreamMemoryOwner::AllocForBrotli;
1456+
brotli_free_func free = CompressionStreamMemoryOwner::FreeForZlib;
1457+
void* opaque =
1458+
CompressionStream<BrotliDecoderContext>::AllocatorOpaquePointerForContext(
1459+
this);
14761460
state_.reset(BrotliDecoderCreateInstance(alloc, free, opaque));
14771461
if (!state_) {
14781462
return CompressionError("Could not initialize Brotli instance",
@@ -1484,7 +1468,7 @@ CompressionError BrotliDecoderContext::Init(brotli_alloc_func alloc,
14841468
}
14851469

14861470
CompressionError BrotliDecoderContext::ResetStream() {
1487-
return Init(alloc_, free_, alloc_opaque_);
1471+
return Init();
14881472
}
14891473

14901474
CompressionError BrotliDecoderContext::SetParams(int key, uint32_t value) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.