Skip to content

Commit

Permalink
detail::stack grows by powers of 2
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Oct 23, 2023
1 parent c650363 commit 59afc41
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions include/boost/json/detail/impl/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ reserve_impl(std::size_t n)

constexpr std::size_t max_storage
= (std::numeric_limits<std::size_t>::max)();
if(BOOST_JSON_UNLIKELY( n > max_storage - n ))
std::size_t cap = cap_ ? cap_ : 32 ;
do
{
if(BOOST_JSON_UNLIKELY( cap > max_storage - cap ))
throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
cap *= 2;
}
while(cap < n);

if(BOOST_JSON_UNLIKELY( cap > max_storage - cap ))
throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );

auto const base = static_cast<unsigned char*>( sp_->allocate(n) );
auto const base = static_cast<unsigned char*>( sp_->allocate(cap) );
if(base_)
{
// copy trivials
Expand Down Expand Up @@ -77,7 +86,7 @@ reserve_impl(std::size_t n)
sp_->deallocate(base_, cap_);
}
base_ = base;
cap_ = n;
cap_ = cap;
}

template<class T>
Expand Down

0 comments on commit 59afc41

Please sign in to comment.