From 45aad3d98ee23e7f0e5873962a55c15a53d6b5c7 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 23 May 2023 10:47:28 +0200 Subject: use exception when allocations fail rather than asserts (#319) * use exception when allocations fail rather than asserts * changelog --- src/zencore/except.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/zencore/except.cpp') diff --git a/src/zencore/except.cpp b/src/zencore/except.cpp index 2749d1984..65f5ebc62 100644 --- a/src/zencore/except.cpp +++ b/src/zencore/except.cpp @@ -90,4 +90,24 @@ ThrowLastError(std::string_view Message) } #endif +#if ZEN_PLATFORM_WINDOWS +static const std::error_code OutOfMemoryErrorCode(ERROR_NOT_ENOUGH_MEMORY, std::system_category()); +#else +static const std::error_code OutOfMemoryErrorCode(ENOMEM, std::system_category()); +#endif + +#if defined(__cpp_lib_source_location) +void +ThrowOutOfMemoryImpl(std::string_view Message, const std::source_location& Location) +{ + throw std::system_error(OutOfMemoryErrorCode, fmt::format("{}({}): {}", Location.file_name(), Location.line(), Message)); +} +#else +void +ThrowOutOfMemory(std::string_view Message) +{ + throw std::system_error(OutOfMemoryErrorCode, std::string(Message)); +} +#endif + } // namespace zen -- cgit v1.2.3