2ce0f5185b
This adds an alternative callback for use in the overload of basic_stacktrace::current that takes a max_depth parameter. The new callback will not allow the container to grow past the initial capacity, which is set to the specified maximum depth. This avoids allocating memory for hundreds of frames only to discard them again because of a small maximum depth limit. For larger maximum depths the normal callback is used, with a smaller initial capacity that can grow as needed. The container will be resized to the given max depth after the entire backtrace has been produced (relying on the fact that std::stacktrace_entry objects are trivially destructible to elide their destruction). Currently the value for "larger" limits is 128, so a max depth <= 128 will allocate capacity for exactly that many frames. A larger max depth (or an unspecified max depth) will use an initial capacity of 64 frames and grow as needed. Since each frame is only a uintptr_t value it might be reasonable to increase the first value so that memory usage can be capped for larger maximum depths. This change also delays the creation of the libbacktrace state until we actually need it, so that the state is not created if allocation fails. libstdc++-v3/ChangeLog: * include/std/stacktrace (basic_stacktrace::current): Replace calls to _M_reserve and _S_curr_cb with call to _M_prepare. Check return value of backtrace_simple when max depth given. (basic_stacktrace::_M_reserve): Remove. (basic_stacktrace::_S_curr_cb): Remove. (basic_stacktrace::_M_prepare(size_type)): New function to reserve initial capacity and return callback. (basic_stacktrace::_Impl::_M_allocate): Remove check for 0 < n and remove redundant zeroing of _M_frames and _M_capacity. (basic_stacktrace::_Impl::_M_push_back): Add [[unlikely]] attribute. Assign _Impl instead of swapping. * testsuite/19_diagnostics/stacktrace/current.cc: New test. |
||
---|---|---|
.. | ||
error_category | ||
error_code | ||
error_condition | ||
headers | ||
logic_error | ||
runtime_error | ||
stacktrace | ||
system_error | ||
stdexcept.cc |