aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2023-05-23 10:47:28 +0200
committerGitHub <[email protected]>2023-05-23 10:47:28 +0200
commit45aad3d98ee23e7f0e5873962a55c15a53d6b5c7 (patch)
tree8fbf9907a934c69aeea3e2fdf16802ae4b8c9ad0 /src
parentstreaming decompression support (#142) (diff)
downloadzen-45aad3d98ee23e7f0e5873962a55c15a53d6b5c7.tar.xz
zen-45aad3d98ee23e7f0e5873962a55c15a53d6b5c7.zip
use exception when allocations fail rather than asserts (#319)
* use exception when allocations fail rather than asserts * changelog
Diffstat (limited to 'src')
-rw-r--r--src/zencore/except.cpp20
-rw-r--r--src/zencore/include/zencore/except.h5
-rw-r--r--src/zencore/iobuffer.cpp14
3 files changed, 34 insertions, 5 deletions
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
diff --git a/src/zencore/include/zencore/except.h b/src/zencore/include/zencore/except.h
index 114f98d77..464852f88 100644
--- a/src/zencore/include/zencore/except.h
+++ b/src/zencore/include/zencore/except.h
@@ -23,9 +23,12 @@ ZENCORE_API void ThrowSystemException [[noreturn]] (HRESULT hRes, std::string_vi
#if defined(__cpp_lib_source_location)
ZENCORE_API void ThrowLastErrorImpl [[noreturn]] (std::string_view Message, const std::source_location& Location);
-# define ThrowLastError(Message) ThrowLastErrorImpl(Message, std::source_location::current())
+ZENCORE_API void ThrowOutOfMemoryImpl [[noreturn]] (std::string_view Message, const std::source_location& Location);
+# define ThrowLastError(Message) ThrowLastErrorImpl(Message, std::source_location::current())
+# define ThrowOutOfMemory(Message) ThrowOutOfMemoryImpl(Message, std::source_location::current())
#else
ZENCORE_API void ThrowLastError [[noreturn]] (std::string_view Message);
+ZENCORE_API void ThrowOutOfMemory [[noreturn]] (std::string_view Message);
#endif
ZENCORE_API void ThrowSystemError [[noreturn]] (uint32_t ErrorCode, std::string_view Message);
diff --git a/src/zencore/iobuffer.cpp b/src/zencore/iobuffer.cpp
index f0eba80ed..f53d80778 100644
--- a/src/zencore/iobuffer.cpp
+++ b/src/zencore/iobuffer.cpp
@@ -40,8 +40,12 @@ IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment) const
if (((InSize & 0xffFF) == 0) && (Alignment == 0x10000))
{
m_Flags.fetch_or(kLowLevelAlloc, std::memory_order_relaxed);
- m_DataPtr = VirtualAlloc(nullptr, InSize, MEM_COMMIT, PAGE_READWRITE);
-
+ void* Ptr = VirtualAlloc(nullptr, InSize, MEM_COMMIT, PAGE_READWRITE);
+ if (!Ptr)
+ {
+ ThrowLastError(fmt::format("VirtualAlloc failed for {:#x} bytes aligned to {:#x}", InSize, Alignment));
+ }
+ m_DataPtr = Ptr;
return;
}
#endif // ZEN_PLATFORM_WINDOWS
@@ -53,8 +57,10 @@ IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment) const
void* Ptr = Memory::Alloc(InSize, Alignment);
#endif
- ZEN_ASSERT(Ptr);
-
+ if (!Ptr)
+ {
+ ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", InSize, Alignment));
+ }
m_DataPtr = Ptr;
}