-
-
Save addaleax/2e3b6f83168e7d756340eb616c92e61f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/node_zlib.cc b/src/node_zlib.cc | |
index 639d7f87c5ab..8501eeaa881d 100644 | |
--- a/src/node_zlib.cc | |
+++ b/src/node_zlib.cc | |
@@ -38,6 +38,9 @@ | |
#include <sys/types.h> | |
#include <atomic> | |
+#include <sys/mman.h> | |
+#include <unistd.h> | |
+ | |
namespace node { | |
using v8::Array; | |
@@ -56,6 +59,49 @@ using v8::Uint32; | |
using v8::Uint32Array; | |
using v8::Value; | |
+const size_t pagesize = sysconf(_SC_PAGESIZE); | |
+ | |
+class PreferMMapAllocator { | |
+ public: | |
+ ~PreferMMapAllocator() { CHECK(allocations_.empty()); } | |
+ char* Allocate(size_t size); | |
+ void Free(char* pointer); | |
+ | |
+ private: | |
+ std::vector<uv_buf_t> allocations_; | |
+}; | |
+ | |
+char* PreferMMapAllocator::Allocate(size_t size) { | |
+ if (size < pagesize) | |
+ return UncheckedMalloc(size); | |
+ size = ROUND_UP(size, pagesize); | |
+ | |
+ char* mem = static_cast<char*>(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); | |
+ // fprintf(stderr, "+ mmap() %p %zu\n", mem, size); | |
+ if (mem == nullptr) return nullptr; | |
+ | |
+ allocations_.push_back(uv_buf_init(mem, size)); | |
+ return mem; | |
+} | |
+ | |
+void PreferMMapAllocator::Free(char* pointer) { | |
+ if (reinterpret_cast<uintptr_t>(pointer) % pagesize != 0) { | |
+ free(pointer); | |
+ return; | |
+ } | |
+ | |
+ auto it = std::find_if(allocations_.begin(), allocations_.end(), [&](const uv_buf_t& buf) { | |
+ return pointer == buf.base; | |
+ }); | |
+ if (UNLIKELY(it == allocations_.end())) { | |
+ free(pointer); | |
+ } else { | |
+ // fprintf(stderr, "- mmap() %p %zu\n", pointer, it->len); | |
+ CHECK_EQ(munmap(pointer, it->len), 0); | |
+ allocations_.erase(it); | |
+ } | |
+} | |
+ | |
namespace { | |
// Fewer than 64 bytes per chunk is not recommended. | |
@@ -453,6 +499,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { | |
init_done_ = true; | |
} | |
+ PreferMMapAllocator allocator_; | |
+ | |
// 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. | |
@@ -469,7 +517,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { | |
static void* AllocForBrotli(void* data, size_t size) { | |
size += sizeof(size_t); | |
CompressionStream* ctx = static_cast<CompressionStream*>(data); | |
- char* memory = UncheckedMalloc(size); | |
+ char* memory = ctx->allocator_.Allocate(size); | |
if (UNLIKELY(memory == nullptr)) return nullptr; | |
*reinterpret_cast<size_t*>(memory) = size; | |
ctx->unreported_allocations_.fetch_add(size, | |
@@ -484,7 +532,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { | |
size_t real_size = *reinterpret_cast<size_t*>(real_pointer); | |
ctx->unreported_allocations_.fetch_sub(real_size, | |
std::memory_order_relaxed); | |
- free(real_pointer); | |
+ ctx->allocator_.Free(real_pointer); | |
} | |
// This is called on the main thread after zlib may have allocated something |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment