Skip to content

Commit 28bd8e7

Browse files
committed
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.
1 parent 038d829 commit 28bd8e7

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
@@ -607,9 +607,11 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
607607
return AllocForBrotli(data, real_size);
608608
}
609609

610+
static constexpr size_t reserveSizeAndAlign =
611+
std::max(sizeof(size_t), alignof(max_align_t));
612+
610613
static void* AllocForBrotli(void* data, size_t size) {
611-
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
612-
size += offset;
614+
size += CompressionStream::reserveSizeAndAlign;
613615
CompressionStream* ctx = static_cast<CompressionStream*>(data);
614616
char* memory = UncheckedMalloc(size);
615617
if (memory == nullptr) [[unlikely]] {
@@ -618,16 +620,15 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
618620
*reinterpret_cast<size_t*>(memory) = size;
619621
ctx->unreported_allocations_.fetch_add(size,
620622
std::memory_order_relaxed);
621-
return memory + offset;
623+
return memory + CompressionStream::reserveSizeAndAlign;
622624
}
623625

624626
static void FreeForZlib(void* data, void* pointer) {
625627
if (pointer == nullptr) [[unlikely]] {
626628
return;
627629
}
628630
CompressionStream* ctx = static_cast<CompressionStream*>(data);
629-
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
630-
char* real_pointer = static_cast<char*>(pointer) - offset;
631+
char* real_pointer = static_cast<char*>(pointer) - CompressionStream::reserveSizeAndAlign;
631632
size_t real_size = *reinterpret_cast<size_t*>(real_pointer);
632633
ctx->unreported_allocations_.fetch_sub(real_size,
633634
std::memory_order_relaxed);

0 commit comments

Comments
 (0)