aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/zencore/except.cpp20
-rw-r--r--src/zencore/include/zencore/except.h5
-rw-r--r--src/zencore/iobuffer.cpp14
4 files changed, 37 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b4d6e30d3..8182df0fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,7 @@
##
+- Improvement: Throw exception with information on failed memory allocation instead of calling ZEN_ASSERT
+
+## 0.2.12
- Feature: zenserver/zen: Added zen command line command `scrub` which can be used to trigger a data scrubbing pass which traverses all stored data and verifies its integrity. If any checksum mismatches or structural errors are found the content is dropped. For now this does not provide much feedback in the console, but the zenserver logs will contain information about the operation
- Feature: zen: added zen `bench` command which has an option to empty Windows standby lists. This effectively empties the system (disk) cache, which can be useful when performing benchmarks since this puts the system in a more consistent state
- Feature: zen: added zen `copy` command which can be used to perform copy-on-write copies of files and directories on supported file systems (i.e ReFS on Windows). This is useful when working with test datasets where you want to avoid tests modifying the original test data
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;
}