Skip to content

Commit

Permalink
Traktor: Using mimalloc instead of std malloc on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Mar 5, 2024
1 parent 66d7a15 commit b7dc899
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 3 deletions.
28 changes: 26 additions & 2 deletions code/Core/Memory/Alloc.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#if defined(_WIN32)
# define T_USE_MIMALLOC
#endif

#include <algorithm>
#include <atomic>
#include <cstdlib>
#include <iostream>
#include "Core/Memory/Alloc.h"
#include "Core/Misc/Align.h"

#if defined(T_USE_MIMALLOC)
# include "mimalloc.h"
#endif

namespace traktor
{
namespace
Expand All @@ -34,6 +43,9 @@ std::atomic< int64_t > s_allocated(0);

void* Alloc::acquire(size_t size, const char* tag)
{
#if defined(T_USE_MIMALLOC)
return mi_malloc(size + sizeof(Block));
#else
void* ptr = std::malloc(size + sizeof(Block));
if (!ptr)
T_FATAL_ERROR;
Expand All @@ -45,23 +57,30 @@ void* Alloc::acquire(size_t size, const char* tag)
s_count++;
s_allocated += size + sizeof(Block);
return block + 1;
#endif
}

void Alloc::free(void* ptr)
{
#if defined(T_USE_MIMALLOC)
mi_free(ptr);
#else
if (ptr)
{
Block* block = (Block*)ptr - 1;
T_FATAL_ASSERT_M(block->magic == c_magic, L"Invalid free");
s_allocated -= (int64_t)(block->size + sizeof(Block));
std::free(block);
}
#endif
}

void* Alloc::acquireAlign(size_t size, size_t align, const char* tag)
{
T_ASSERT(align >= 1);

#if defined(T_USE_MIMALLOC)
return mi_malloc_aligned(size, align);
#else
uint8_t* ptr = (uint8_t*)Alloc::acquire(size + sizeof(intptr_t) + align - 1, tag);
if (!ptr)
T_FATAL_ERROR;
Expand All @@ -70,15 +89,20 @@ void* Alloc::acquireAlign(size_t size, size_t align, const char* tag)
*(intptr_t*)(alignedPtr - sizeof(intptr_t)) = (intptr_t)ptr;

return alignedPtr;
#endif
}

void Alloc::freeAlign(void* ptr)
{
#if defined(T_USE_MIMALLOC)
mi_free(ptr);
#else
if (ptr)
{
intptr_t originalPtr = *((intptr_t*)(ptr) - 1);
Alloc::free((void*)originalPtr);
}
#endif
}

size_t Alloc::count()
Expand Down
119 changes: 119 additions & 0 deletions resources/build/ExternWin64.xms
Original file line number Diff line number Diff line change
Expand Up @@ -6518,5 +6518,124 @@
</items>
<dependencies/>
</item>
<item type="Project" version="1">
<enable>true</enable>
<name>Extern.mimalloc</name>
<sourcePath>$(MIMALLOC_SDK)/src</sourcePath>
<configurations>
<item type="Configuration" version="5">
<name>DebugShared</name>
<targetFormat>TfStaticLibrary</targetFormat>
<targetProfile>TpDebug</targetProfile>
<precompiledHeader/>
<includePaths>
<item>$(MIMALLOC_SDK)/include</item>
</includePaths>
<definitions/>
<libraryPaths/>
<libraries/>
<warningLevel>WlCompilerDefault</warningLevel>
<additionalCompilerOptions/>
<additionalLinkerOptions/>
<debugExecutable/>
<debugArguments/>
<debugEnvironment/>
<debugWorkingDirectory/>
<aggregationItems/>
<consumerLibraryPath/>
</item>
<item type="Configuration" version="5">
<name>ReleaseShared</name>
<targetFormat>TfStaticLibrary</targetFormat>
<targetProfile>TpRelease</targetProfile>
<precompiledHeader/>
<includePaths>
<item>$(MIMALLOC_SDK)/include</item>
</includePaths>
<definitions/>
<libraryPaths/>
<libraries/>
<warningLevel>WlCompilerDefault</warningLevel>
<additionalCompilerOptions/>
<additionalLinkerOptions/>
<debugExecutable/>
<debugArguments/>
<debugEnvironment/>
<debugWorkingDirectory/>
<aggregationItems/>
<consumerLibraryPath/>
</item>
<item type="Configuration" version="5">
<name>DebugStatic</name>
<targetFormat>TfStaticLibrary</targetFormat>
<targetProfile>TpDebug</targetProfile>
<precompiledHeader/>
<includePaths>
<item>$(MIMALLOC_SDK)/include</item>
</includePaths>
<definitions/>
<libraryPaths/>
<libraries/>
<warningLevel>WlCompilerDefault</warningLevel>
<additionalCompilerOptions/>
<additionalLinkerOptions/>
<debugExecutable/>
<debugArguments/>
<debugEnvironment/>
<debugWorkingDirectory/>
<aggregationItems/>
<consumerLibraryPath/>
</item>
<item type="Configuration" version="5">
<name>ReleaseStatic</name>
<targetFormat>TfStaticLibrary</targetFormat>
<targetProfile>TpRelease</targetProfile>
<precompiledHeader/>
<includePaths>
<item>$(MIMALLOC_SDK)/include</item>
</includePaths>
<definitions/>
<libraryPaths/>
<libraries/>
<warningLevel>WlCompilerDefault</warningLevel>
<additionalCompilerOptions/>
<additionalLinkerOptions/>
<debugExecutable/>
<debugArguments/>
<debugEnvironment/>
<debugWorkingDirectory/>
<aggregationItems/>
<consumerLibraryPath/>
</item>
</configurations>
<items>
<item type="File" version="1">
<fileName>*.*</fileName>
<excludeFilter>alloc-override.c;page-queue.c</excludeFilter>
<items/>
</item>
<item type="Filter">
<name>prim</name>
<items>
<item type="File" version="1">
<fileName>prim/*.c</fileName>
<excludeFilter/>
<items/>
</item>
<item type="Filter">
<name>windows</name>
<items>
<item type="File" version="1">
<fileName>prim/windows/*.c</fileName>
<excludeFilter/>
<items/>
</item>
</items>
</item>
</items>
</item>
</items>
<dependencies/>
</item>
</projects>
</object>
9 changes: 8 additions & 1 deletion resources/build/TraktorWin64.xms
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,14 @@
</items>
</item>
</items>
<dependencies/>
<dependencies>
<item type="ExternalDependency" version="3">
<inheritIncludePaths>true</inheritIncludePaths>
<link>LnkYes</link>
<solutionFileName>ExternWin64.xms</solutionFileName>
<projectName>Extern.mimalloc</projectName>
</item>
</dependencies>
</project>
</item>
<item type="ProjectDependency" version="3">
Expand Down
1 change: 1 addition & 0 deletions scripts/config.bat
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set ZLIB_SDK=%TRAKTOR_HOME%\3rdp\zlib
set DOXYGEN_SDK=%TRAKTOR_HOME%\3rdp\doxygen
set BC6H_ENC_SDK=%TRAKTOR_HOME%\3rdp\bc6h_enc
set MINIMP3_SDK=%TRAKTOR_HOME%\3rdp\minimp3
set MIMALLOC_SDK=%TRAKTOR_HOME%\3rdp\mimalloc

:: Vulkan SDK
set VULKAN_SDK=%TRAKTOR_HOME%\3rdp\vulkan-windows\vulkan-sdk
Expand Down
8 changes: 8 additions & 0 deletions scripts/misc/update-3rdp.run
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,14 @@ local PACKAGES =
url = "https://github.com/lieff/minimp3.git",
license = "CC0"
},

-- mimalloc
{
type = "GIT",
name = "mimalloc",
url = "https://github.com/microsoft/mimalloc.git",
license = "MIT"
},
}

--[[
Expand Down

0 comments on commit b7dc899

Please sign in to comment.