From ae87b251c8a87640a2bd1483049c893784f21c7f Mon Sep 17 00:00:00 2001 From: John Esmet Date: Tue, 17 Sep 2013 23:55:37 -0400 Subject: [PATCH] fixed memory allocation error in range_buffer #70 fixes #70 --- locktree/range_buffer.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/locktree/range_buffer.cc b/locktree/range_buffer.cc index 7ac00562e57..0173f5c2fa2 100644 --- a/locktree/range_buffer.cc +++ b/locktree/range_buffer.cc @@ -272,13 +272,19 @@ void range_buffer::append_point(const DBT *key) { } void range_buffer::maybe_grow(size_t size) { + static const size_t initial_size = 4096; static const size_t aggressive_growth_threshold = 128 * 1024; - if (m_buf_current + size > m_buf_size) { + const size_t needed = m_buf_current + size; + if (m_buf_size < needed) { + if (m_buf_size == 0) { + m_buf_size = initial_size; + } // aggressively grow the range buffer to the threshold, // but only additivately increase the size after that. - if (m_buf_size < aggressive_growth_threshold) { - m_buf_size = m_buf_size ? m_buf_size * 2 : get_initial_size(size); - } else { + while (m_buf_size < needed && m_buf_size < aggressive_growth_threshold) { + m_buf_size <<= 1; + } + while (m_buf_size < needed) { m_buf_size += aggressive_growth_threshold; } XREALLOC(m_buf, m_buf_size);