Skip to content

Commit

Permalink
recursion_sentry: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Mar 13, 2024
1 parent 71cef1d commit dbcf7a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
6 changes: 3 additions & 3 deletions asteria/recursion_sentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace asteria {

void
Recursion_Sentry::
do_throw_stack_overflow(ptrdiff_t usage) const
do_throw_stack_overflow(ptrdiff_t usage, int limit) const
{
::rocket::sprintf_and_throw<::std::invalid_argument>(
"asteria::Recursion_Sentry: stack overflow averted (`%td` > `%ld`)",
::std::abs(usage), 1L << stack_mask_bits);
"do_throw_stack_overflow: stack overflow averted (`%td` > `%d`)",
usage, limit);
}

} // namespace asteria
17 changes: 7 additions & 10 deletions asteria/recursion_sentry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ namespace asteria {

class Recursion_Sentry
{
public:
enum : uint32_t
{
stack_mask_bits = 20, // 1 MiB
};

private:
const void* m_base;

Expand Down Expand Up @@ -45,21 +39,24 @@ class Recursion_Sentry
private:
[[noreturn]]
void
do_throw_stack_overflow(ptrdiff_t usage) const;
do_throw_stack_overflow(ptrdiff_t usage, int limit) const;

void
do_validate_stack_usage() const
{
ptrdiff_t usage = (char*) this->m_base - (char*) this;
if(ROCKET_UNEXPECT(((usage >> stack_mask_bits) + 1) >> 1 != 0))
this->do_throw_stack_overflow(usage);
constexpr int mask_bits = 20; // 1 MiB
constexpr int ptr_bits = ::std::numeric_limits<ptrdiff_t>::digits;

if(ROCKET_UNEXPECT(usage >> mask_bits != usage >> ptr_bits))
this->do_throw_stack_overflow(usage, 1 << mask_bits);
}

public:
// Make this class non-trivial.
~Recursion_Sentry()
{
__asm__ volatile ("" :);
__asm__ ("");
}

const void*
Expand Down

0 comments on commit dbcf7a0

Please sign in to comment.