diff --git a/src/allocator.h b/src/allocator.h index 2f48f38b..998653be 100644 --- a/src/allocator.h +++ b/src/allocator.h @@ -36,8 +36,12 @@ struct MemBlock { uint8_t *begin, *last, *end; }; -template struct BlockAllocator { - BlockAllocator() : retain(nullptr), head(nullptr) {} +struct BlockAllocator { + BlockAllocator(size_t block_size, size_t isolation_threshold) + : retain(nullptr), + head(nullptr), + block_size(block_size), + isolation_threshold(std::min(block_size, isolation_threshold)) {} ~BlockAllocator() { for (auto mb = retain; mb;) { @@ -59,14 +63,14 @@ template struct BlockAllocator { } void *alloc(size_t size) { - if (size >= BLOCK_SIZE) { + if (size >= isolation_threshold) { auto mb = alloc_mem_block(size); mb->last = mb->end; return mb->begin; } if (!head || head->end - head->last < static_cast(size)) { - head = alloc_mem_block(BLOCK_SIZE); + head = alloc_mem_block(block_size); } auto res = head->last; @@ -79,6 +83,11 @@ template struct BlockAllocator { MemBlock *retain; MemBlock *head; + // size of single memory block + size_t block_size; + // if allocation greater or equal to isolation_threshold bytes is + // requested, allocate dedicated block. + size_t isolation_threshold; }; template @@ -101,8 +110,6 @@ StringRef concat_string_ref(BlockAllocator &alloc, const StringRef &a, return StringRef{dst, a.size() + b.size()}; } -using DefaultBlockAllocator = BlockAllocator<1024>; - } // namespace aria2 #endif // ALLOCATOR_H diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index 11462d7c..ed1edf2e 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -116,6 +116,7 @@ Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool, : dlnext(nullptr), dlprev(nullptr), response_sent_body_length(0), + balloc_(1024, 1024), req_(balloc_), resp_(balloc_), request_start_time_(std::chrono::high_resolution_clock::now()), @@ -357,9 +358,8 @@ void add_header(size_t &sum, HeaderRefs &headers, const StringRef &name, } // namespace namespace { -void append_last_header_key(DefaultBlockAllocator &balloc, bool &key_prev, - size_t &sum, HeaderRefs &headers, const char *data, - size_t len) { +void append_last_header_key(BlockAllocator &balloc, bool &key_prev, size_t &sum, + HeaderRefs &headers, const char *data, size_t len) { assert(key_prev); sum += len; auto &item = headers.back(); @@ -371,7 +371,7 @@ void append_last_header_key(DefaultBlockAllocator &balloc, bool &key_prev, } // namespace namespace { -void append_last_header_value(DefaultBlockAllocator &balloc, bool &key_prev, +void append_last_header_value(BlockAllocator &balloc, bool &key_prev, size_t &sum, HeaderRefs &headers, const char *data, size_t len) { key_prev = false; @@ -926,6 +926,6 @@ void Downstream::set_assoc_stream_id(int32_t stream_id) { int32_t Downstream::get_assoc_stream_id() const { return assoc_stream_id_; } -DefaultBlockAllocator &Downstream::get_block_allocator() { return balloc_; } +BlockAllocator &Downstream::get_block_allocator() { return balloc_; } } // namespace shrpx diff --git a/src/shrpx_downstream.h b/src/shrpx_downstream.h index 7b9dfa83..2ea0bf2e 100644 --- a/src/shrpx_downstream.h +++ b/src/shrpx_downstream.h @@ -52,7 +52,7 @@ struct BlockedLink; class FieldStore { public: - FieldStore(DefaultBlockAllocator &balloc, size_t headers_initial_capacity) + FieldStore(BlockAllocator &balloc, size_t headers_initial_capacity) : content_length(-1), balloc_(balloc), buffer_size_(0), @@ -112,7 +112,7 @@ public: int64_t content_length; private: - DefaultBlockAllocator &balloc_; + BlockAllocator &balloc_; HeaderRefs headers_; // trailer fields. For HTTP/1.1, trailer fields are only included // with chunked encoding. For HTTP/2, there is no such limit. @@ -126,7 +126,7 @@ private: }; struct Request { - Request(DefaultBlockAllocator &balloc) + Request(BlockAllocator &balloc) : fs(balloc, 16), recv_body_length(0), unconsumed_body_length(0), @@ -182,7 +182,7 @@ struct Request { }; struct Response { - Response(DefaultBlockAllocator &balloc) + Response(BlockAllocator &balloc) : fs(balloc, 32), recv_body_length(0), unconsumed_body_length(0), @@ -376,7 +376,7 @@ public: DefaultMemchunks pop_response_buf(); - DefaultBlockAllocator &get_block_allocator(); + BlockAllocator &get_block_allocator(); enum { EVENT_ERROR = 0x1, @@ -397,7 +397,7 @@ public: int64_t response_sent_body_length; private: - DefaultBlockAllocator balloc_; + BlockAllocator balloc_; Request req_; Response resp_;