aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/memory.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2024-11-25 09:56:23 +0100
committerGitHub Enterprise <[email protected]>2024-11-25 09:56:23 +0100
commit8b8de92e51db4cc4c1727712c736dcba5f79d369 (patch)
tree1f58edaaad389837a7652daebab246125762240e /src/zencore/memory.cpp
parent5.5.13 (diff)
downloadzen-8b8de92e51db4cc4c1727712c736dcba5f79d369.tar.xz
zen-8b8de92e51db4cc4c1727712c736dcba5f79d369.zip
Insights-compatible memory tracking (#214)
This change introduces support for tracing of memory allocation activity. The code is ported from UE5, and Unreal Insights can be used to analyze the output. This is currently only fully supported on Windows, but will be extended to Mac/Linux in the near future. To activate full memory tracking, pass `--trace=memory` on the commandline alongside `--tracehost=<ip>` or `-tracefile=<path>`. For more control over how much detail is traced you can instead pass some combination of `callstack`, `memtag`, `memalloc` instead. In practice, `--trace=memory` is an alias for `--trace=callstack,memtag,memalloc`). For convenience we also support `--trace=memory_light` which omits call stacks. This change also introduces multiple memory allocators, which may be selected via command-line option `--malloc=<allocator>`: * `mimalloc` - mimalloc (default, same as before) * `rpmalloc` - rpmalloc is another high performance allocator for multithreaded applications which may be a better option than mimalloc (to be evaluated). Due to toolchain limitations this is currently only supported on Windows. * `stomp` - an allocator intended to be used during development/debugging to help track down memory issues such as use-after-free or out-of-bounds access. Currently only supported on Windows. * `ansi` - fallback to default system allocator
Diffstat (limited to 'src/zencore/memory.cpp')
-rw-r--r--src/zencore/memory.cpp55
1 files changed, 1 insertions, 54 deletions
diff --git a/src/zencore/memory.cpp b/src/zencore/memory.cpp
index a0d911786..a2fe02f3a 100644
--- a/src/zencore/memory.cpp
+++ b/src/zencore/memory.cpp
@@ -4,67 +4,14 @@
#include <zencore/fmtutils.h>
#include <zencore/intmath.h>
#include <zencore/memory.h>
+#include <zencore/memory/memory.h>
#include <zencore/testing.h>
#include <zencore/zencore.h>
#include <cstdlib>
-#if ZEN_USE_MIMALLOC
-ZEN_THIRD_PARTY_INCLUDES_START
-# include <mimalloc.h>
-ZEN_THIRD_PARTY_INCLUDES_END
-#endif
-
namespace zen {
-//////////////////////////////////////////////////////////////////////////
-
-static void*
-AlignedAllocImpl(size_t Size, size_t Alignment)
-{
- // aligned_alloc() states that size must be a multiple of alignment. Some
- // platforms return null if this requirement isn't met.
- Size = (Size + Alignment - 1) & ~(Alignment - 1);
-
-#if ZEN_USE_MIMALLOC
- return mi_aligned_alloc(Alignment, Size);
-#elif ZEN_PLATFORM_WINDOWS
- return _aligned_malloc(Size, Alignment);
-#else
- return std::aligned_alloc(Alignment, Size);
-#endif
-}
-
-void
-AlignedFreeImpl(void* ptr)
-{
- if (ptr == nullptr)
- return;
-
-#if ZEN_USE_MIMALLOC
- return mi_free(ptr);
-#elif ZEN_PLATFORM_WINDOWS
- _aligned_free(ptr);
-#else
- std::free(ptr);
-#endif
-}
-
-//////////////////////////////////////////////////////////////////////////
-
-void*
-Memory::Alloc(size_t Size, size_t Alignment)
-{
- return AlignedAllocImpl(Size, Alignment);
-}
-
-void
-Memory::Free(void* ptr)
-{
- AlignedFreeImpl(ptr);
-}
-
-//////////////////////////////////////////////////////////////////////////
//
// Unit tests
//