Skip to content

Commit b970c0b

Browse files
jhofsteetargos
authored andcommitted
zlib: reduce code duplication
The offset in the allocated memory was calculated in alloc and free, this makes it a single constant so it only needs to be defined once. PR-URL: #57810 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent e330f03 commit b970c0b

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/node_zlib.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,11 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
612612
return AllocForBrotli(data, real_size);
613613
}
614614

615+
static constexpr size_t reserveSizeAndAlign =
616+
std::max(sizeof(size_t), alignof(max_align_t));
617+
615618
static void* AllocForBrotli(void* data, size_t size) {
616-
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
617-
size += offset;
619+
size += reserveSizeAndAlign;
618620
CompressionStream* ctx = static_cast<CompressionStream*>(data);
619621
char* memory = UncheckedMalloc(size);
620622
if (memory == nullptr) [[unlikely]] {
@@ -623,16 +625,15 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
623625
*reinterpret_cast<size_t*>(memory) = size;
624626
ctx->unreported_allocations_.fetch_add(size,
625627
std::memory_order_relaxed);
626-
return memory + offset;
628+
return memory + reserveSizeAndAlign;
627629
}
628630

629631
static void FreeForZlib(void* data, void* pointer) {
630632
if (pointer == nullptr) [[unlikely]] {
631633
return;
632634
}
633635
CompressionStream* ctx = static_cast<CompressionStream*>(data);
634-
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
635-
char* real_pointer = static_cast<char*>(pointer) - offset;
636+
char* real_pointer = static_cast<char*>(pointer) - reserveSizeAndAlign;
636637
size_t real_size = *reinterpret_cast<size_t*>(real_pointer);
637638
ctx->unreported_allocations_.fetch_sub(real_size,
638639
std::memory_order_relaxed);

0 commit comments

Comments
 (0)