aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-30 10:38:18 +0200
committerStefan Boberg <[email protected]>2021-09-30 10:38:39 +0200
commitd5f0bd35394198c8fd3452c29c16109e05209ae8 (patch)
tree8691977fba045a097c0e04d2bf6203368c69fa65
parenttimer: Added Stopwatch::GetElapsedTIcks() and functions to convert ticks so M... (diff)
downloadzen-d5f0bd35394198c8fd3452c29c16109e05209ae8.tar.xz
zen-d5f0bd35394198c8fd3452c29c16109e05209ae8.zip
memory: Added experimental mimalloc path to AlignedAllocImpl/AlignedFreeImpl
Also added similar path to IoBuffer Cannot be enabled at the moment as we end up passing pointers to std::free via some path so more work will be necessary
-rw-r--r--zencore/iobuffer.cpp37
-rw-r--r--zencore/memory.cpp15
2 files changed, 39 insertions, 13 deletions
diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp
index a730a316f..244425761 100644
--- a/zencore/iobuffer.cpp
+++ b/zencore/iobuffer.cpp
@@ -14,6 +14,10 @@
#include <memory.h>
#include <system_error>
+#if ZEN_USE_MIMALLOC
+# include <mimalloc.h>
+#endif
+
#if ZEN_PLATFORM_WINDOWS
# include <atlfile.h>
#else
@@ -36,26 +40,41 @@ IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment)
m_Flags |= kLowLevelAlloc;
return VirtualAlloc(nullptr, InSize, MEM_COMMIT, PAGE_READWRITE);
}
- else
#endif // ZEN_PLATFORM_WINDOWS
- {
- return Memory::Alloc(InSize, Alignment);
- }
+
+#if ZEN_USE_MIMALLOC && 0
+ void* Ptr = mi_aligned_alloc(Alignment, RoundUp(InSize, Alignment));
+#else
+ void* Ptr = Memory::Alloc(InSize, Alignment);
+#endif
+
+ ZEN_ASSERT(Ptr);
+
+ return Ptr;
}
void
IoBufferCore::FreeBuffer()
{
+ if (!m_DataPtr)
+ {
+ return;
+ }
+
#if ZEN_PLATFORM_WINDOWS
if (m_Flags & kLowLevelAlloc)
{
VirtualFree(const_cast<void*>(m_DataPtr), 0, MEM_DECOMMIT);
+
+ return;
}
- else
#endif // ZEN_PLATFORM_WINDOWS
- {
- return Memory::Free(const_cast<void*>(m_DataPtr));
- }
+
+#if ZEN_USE_MIMALLOC && 0
+ return mi_free(const_cast<void*>(m_DataPtr));
+#else
+ return Memory::Free(const_cast<void*>(m_DataPtr));
+#endif
}
//////////////////////////////////////////////////////////////////////////
@@ -436,7 +455,7 @@ IoBufferBuilder::MakeFromTemporaryFile(const path_char_t* FileName)
Handle = DataFile.Detach();
#else
- int Fd = open(FileName, O_RDONLY);
+ int Fd = open(FileName, O_RDONLY);
if (Fd < 0)
{
return {};
diff --git a/zencore/memory.cpp b/zencore/memory.cpp
index 613b6ba67..da78ae3a8 100644
--- a/zencore/memory.cpp
+++ b/zencore/memory.cpp
@@ -6,6 +6,7 @@
#ifdef ZEN_PLATFORM_WINDOWS
# include <malloc.h>
+# include <mimalloc.h>
#else
# include <cstdlib>
#endif
@@ -18,8 +19,11 @@ static void*
AlignedAllocImpl(size_t size, size_t alignment)
{
#if ZEN_PLATFORM_WINDOWS
- // return _aligned_malloc(size, alignment); // MSVC alternative
- return _mm_malloc(size, alignment);
+# if ZEN_USE_MIMALLOC && 0 /* this path is not functional */
+ return mi_aligned_alloc(alignment, size);
+# else
+ return _aligned_malloc(size, alignment);
+# endif
#else
// posix_memalign(&Ret, Alignment, Size); // Apple, AndroidApi<28
return std::aligned_alloc(alignment, size);
@@ -33,8 +37,11 @@ AlignedFreeImpl(void* ptr)
return;
#if ZEN_PLATFORM_WINDOWS
- // _aligned_free(ptr); MSVC alternative
- _mm_free(ptr);
+# if ZEN_USE_MIMALLOC && 0 /* this path is not functional */
+ return mi_free(ptr);
+# else
+ _aligned_free(ptr);
+# endif
#else
// free(ptr) // Apple :)
std::free(ptr);