Skip to content

Commit 816e1be

Browse files
jhofsteeaduh95
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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 53cb9f3 commit 816e1be

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

612+
static constexpr size_t reserveSizeAndAlign =
613+
std::max(sizeof(size_t), alignof(max_align_t));
614+
612615
static void* AllocForBrotli(void* data, size_t size) {
613-
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
614-
size += offset;
616+
size += reserveSizeAndAlign;
615617
CompressionStream* ctx = static_cast<CompressionStream*>(data);
616618
char* memory = UncheckedMalloc(size);
617619
if (memory == nullptr) [[unlikely]] {
@@ -620,16 +622,15 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
620622
*reinterpret_cast<size_t*>(memory) = size;
621623
ctx->unreported_allocations_.fetch_add(size,
622624
std::memory_order_relaxed);
623-
return memory + offset;
625+
return memory + reserveSizeAndAlign;
624626
}
625627

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

0 commit comments

Comments
 (0)