Commit 9f73e929 authored by Kenton Varda's avatar Kenton Varda Committed by GitHub

Merge pull request #444 from dnanexus-rnd/MallocMessageBuilder-segment-size

MallocMessageBuilder: don't allocate segments above the max serializable size
parents f6463dd6 e3f4c014
...@@ -242,6 +242,9 @@ MallocMessageBuilder::~MallocMessageBuilder() noexcept(false) { ...@@ -242,6 +242,9 @@ MallocMessageBuilder::~MallocMessageBuilder() noexcept(false) {
} }
kj::ArrayPtr<word> MallocMessageBuilder::allocateSegment(uint minimumSize) { kj::ArrayPtr<word> MallocMessageBuilder::allocateSegment(uint minimumSize) {
KJ_REQUIRE(minimumSize <= MAX_SEGMENT_WORDS, "MallocMessageBuilder asked to allocate segment above maximum serializable size.");
KJ_ASSERT(nextSize <= MAX_SEGMENT_WORDS, "MallocMessageBuilder nextSize out of bounds.");
if (!returnedFirstSegment && !ownFirstSegment) { if (!returnedFirstSegment && !ownFirstSegment) {
kj::ArrayPtr<word> result = kj::arrayPtr(reinterpret_cast<word*>(firstSegment), nextSize); kj::ArrayPtr<word> result = kj::arrayPtr(reinterpret_cast<word*>(firstSegment), nextSize);
if (result.size() >= minimumSize) { if (result.size() >= minimumSize) {
...@@ -277,7 +280,11 @@ kj::ArrayPtr<word> MallocMessageBuilder::allocateSegment(uint minimumSize) { ...@@ -277,7 +280,11 @@ kj::ArrayPtr<word> MallocMessageBuilder::allocateSegment(uint minimumSize) {
moreSegments = mv(newSegments); moreSegments = mv(newSegments);
} }
segments->segments.push_back(result); segments->segments.push_back(result);
if (allocationStrategy == AllocationStrategy::GROW_HEURISTICALLY) nextSize += size; if (allocationStrategy == AllocationStrategy::GROW_HEURISTICALLY) {
// set nextSize = min(nextSize+size, MAX_SEGMENT_WORDS)
// while protecting against possible overflow of (nextSize+size)
nextSize = (size <= MAX_SEGMENT_WORDS-nextSize) ? nextSize+size : MAX_SEGMENT_WORDS;
}
} }
return kj::arrayPtr(reinterpret_cast<word*>(result), size); return kj::arrayPtr(reinterpret_cast<word*>(result), size);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment