Repository: nodejs/node
File: src/node_zlib.cc (C++)

Code before:
  }

 protected:
  CompressionContext* context() { return &ctx_; }

  void InitStream(uint32_t* write_result, Local<Function> write_js_callback) {
    write_result_ = write_result;
    object()->SetInternalField(kWriteJSCallback, write_js_callback);
    init_done_ = true;
  }

  // Allocation functions provided to zlib itself. We store the real size of
  // the allocated memory chunk just before the "payload" memory we return
  // to zlib.
  // Because we use zlib off the thread pool, we can not report memory directly
  // to V8; rather, we first store it as "unreported" memory in a separate
  // field and later report it back from the main thread.
  static void* AllocForZlib(void* data, uInt items, uInt size) {
    size_t real_size =
        MultiplyWithOverflowCheck(static_cast<size_t>(items),
                                  static_cast<size_t>(size));
    return AllocForBrotli(data, real_size);
  }

  static void* AllocForBrotli(void* data, size_t size) {
    constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
    size += offset;
    CompressionStream* ctx = static_cast<CompressionStream*>(data);
    char* memory = UncheckedMalloc(size);
    if (memory == nullptr) [[unlikely]] {
      return nullptr;
    }
    *reinterpret_cast<size_t*>(memory) = size;
    ctx->unreported_allocations_.fetch_add(size,
                                           std::memory_order_relaxed);
    return memory + offset;
  }

  static void FreeForZlib(void* data, void* pointer) {
    if (pointer == nullptr) [[unlikely]] {
      return;
    }
    CompressionStream* ctx = static_cast<CompressionStream*>(data);
    constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
    char* real_pointer = static_cast<char*>(pointer) - offset;
    size_t real_size = *reinterpret_cast<size_t*>(real_pointer);
    ctx->unreported_allocations_.fetch_sub(real_size,
                                           std::memory_order_relaxed);
    free(real_pointer);
  }


Review comment (suggestion):
```suggestion
  static constexpr size_t kReserveSizeAndAlign =
```
Not 100% sure but I think this doesn't fit the code style, it seems inconsistent already in the codebase
Quite some places use a `k` prefix, others use define style (e.g. `RESERVER_SIZE_AND_ALIGN`).

Code after:
                                zlib_memory_ + unreported_allocations_);
  }

 protected:
  CompressionContext* context() { return &ctx_; }

  void InitStream(uint32_t* write_result, Local<Function> write_js_callback) {
    write_result_ = write_result;
    object()->SetInternalField(kWriteJSCallback, write_js_callback);
    init_done_ = true;
  }

  // Allocation functions provided to zlib itself. We store the real size of
  // the allocated memory chunk just before the "payload" memory we return
  // to zlib.
  // Because we use zlib off the thread pool, we can not report memory directly
  // to V8; rather, we first store it as "unreported" memory in a separate
  // field and later report it back from the main thread.
  static void* AllocForZlib(void* data, uInt items, uInt size) {
    size_t real_size =
        MultiplyWithOverflowCheck(static_cast<size_t>(items),
                                  static_cast<size_t>(size));
    return AllocForBrotli(data, real_size);
  }

  static constexpr size_t reserveSizeAndAlign =
      std::max(sizeof(size_t), alignof(max_align_t));

  static void* AllocForBrotli(void* data, size_t size) {
    size += reserveSizeAndAlign;
    CompressionStream* ctx = static_cast<CompressionStream*>(data);
    char* memory = UncheckedMalloc(size);
    if (memory == nullptr) [[unlikely]] {
      return nullptr;
    }
    *reinterpret_cast<size_t*>(memory) = size;
    ctx->unreported_allocations_.fetch_add(size,
                                           std::memory_order_relaxed);
    return memory + reserveSizeAndAlign;
  }

  static void FreeForZlib(void* data, void* pointer) {
    if (pointer == nullptr) [[unlikely]] {
      return;
    }
    CompressionStream* ctx = static_cast<CompressionStream*>(data);
    char* real_pointer = static_cast<char*>(pointer) - reserveSizeAndAlign;
    size_t real_size = *reinterpret_cast<size_t*>(real_pointer);
    ctx->unreported_allocations_.fetch_sub(real_size,
                                           std::memory_order_relaxed);
    free(real_pointer);