Skip to content

Commit

Permalink
Merge pull request #870 from edge-classic/ec-optional-mimalloc
Browse files Browse the repository at this point in the history
Adding optional mimalloc config and fixing a memory stomp in deh code
  • Loading branch information
pbdot authored Jan 30, 2025
2 parents 197f549 + 7a9dfff commit 308a692
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
19 changes: 13 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ option(EDGE_CLASSIC "Enable default features for EDGE-Classic" ON)
option(EDGE_SANITIZE "Enable code sanitizing" OFF)
option(EDGE_PROFILING "Enable Profiling" OFF)

# memory allocator
option(EDGE_MIMALLOC "Enable mimalloc" ON)
# Enables exhaustive memory checks, see mimalloc CMakeLists.txt for what is enabled
# Memory error logging goes to debug.txt
option(EDGE_MEMORY_CHECK "Enable Memory Checks" OFF)
Expand All @@ -74,10 +76,12 @@ if (WIN32 OR MINGW)
endif()

if(MSVC)
# Override memory allocations to use mimalloc
add_definitions(/FI"${CMAKE_SOURCE_DIR}/libraries/mimalloc/include/mimalloc-override.h")
# Use static C runtime, necessary for mimalloc and also means matching C runtime doesn't need to be on users box
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:Debug>")
if (EDGE_MIMALLOC)
# Override memory allocations to use mimalloc
add_definitions(/FI"${CMAKE_SOURCE_DIR}/libraries/mimalloc/include/mimalloc-override.h")
endif()
endif()

if (MSVC)
Expand Down Expand Up @@ -202,10 +206,13 @@ if (EDGE_PROFILING)
add_compile_definitions(EDGE_PROFILING TRACY_ENABLE)
endif()

if (EDGE_MEMORY_CHECK)
add_compile_definitions(EDGE_MEMORY_CHECK)
if (EDGE_MEMORY_CHECK_FATAL)
add_compile_definitions(EDGE_MEMORY_CHECK_FATAL)
if (EDGE_MEMORY_MIMALLOC)
add_compile_definitions(EDGE_MIMALLOC)
if (EDGE_MEMORY_CHECK)
add_compile_definitions(EDGE_MEMORY_CHECK)
if (EDGE_MEMORY_CHECK_FATAL)
add_compile_definitions(EDGE_MEMORY_CHECK_FATAL)
endif()
endif()
endif()

Expand Down
2 changes: 2 additions & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ add_subdirectory(minivorbis)
add_subdirectory(stb)
if (NOT EMSCRIPTEN)
add_subdirectory(thread)
if (EDGE_MIMALLOC)
add_subdirectory(mimalloc)
endif()
endif()
add_subdirectory(tracy)
add_subdirectory(xxHash)

Expand Down
7 changes: 6 additions & 1 deletion source_files/dehacked/deh_things.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static constexpr char kExtraFlagNoRaise = 'R';
static constexpr char kExtraFlagNoGrudge = 'G';
static constexpr char kExtraFlagNoItemBk = 'I';

static constexpr uint8_t kCastMaximum = 17;
static constexpr uint8_t kCastMaximum = 18;

extern DehackedMapObjectDefinition mobjinfo[kTotalDehackedMapObjectTypesPortCompatibility];

Expand Down Expand Up @@ -1149,6 +1149,11 @@ void CollectTheCast()
continue;
}

if (order >= kCastMaximum)
{
FatalError("CollectTheCast() - Overflow");
}

cast_mobjs[order] = mt_num;
}
}
Expand Down
6 changes: 6 additions & 0 deletions source_files/edge/con_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "con_main.h"

#ifdef EDGE_MIMALLOC
#include <mimalloc.h>
#endif
#include <stdarg.h>
#include <string.h>

Expand Down Expand Up @@ -537,19 +539,23 @@ int ConsoleCommandClear(char **argv, int argc)
return 0;
}

#ifdef EDGE_MIMALLOC
static void MemoryPrint(const char *msg, void *arg)
{
EPI_UNUSED(arg);
LogPrint("%s", msg);
}
#endif

static int ConsoleCommandMemory(char **argv, int argc)
{
EPI_UNUSED(argv);
EPI_UNUSED(argc);

LogPrint("---- mimalloc memory stats ---\n\n");
#ifdef EDGE_MIMALLOC
mi_stats_print_out(MemoryPrint, nullptr);
#endif
return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions source_files/edge/i_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
//
//----------------------------------------------------------------------------

#ifdef EDGE_MIMALLOC
#include <mimalloc.h>
#endif

#include "dm_defs.h"
#include "e_main.h"
Expand All @@ -33,8 +35,10 @@ extern "C"
{
int main(int argc, char *argv[])
{
#ifdef EDGE_MIMALLOC
if (SDL_SetMemoryFunctions(mi_malloc, mi_calloc, mi_realloc, mi_free) != 0)
FatalError("Couldn't init SDL memory functions!!\n%s\n", SDL_GetError());
#endif

if (SDL_Init(0) < 0)
FatalError("Couldn't init SDL!!\n%s\n", SDL_GetError());
Expand Down
2 changes: 1 addition & 1 deletion source_files/epi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ add_library(

set (EPI_LIBRARIES almostequals HandmadeMath stb xxhash)

if (NOT EMSCRIPTEN)
if (NOT EMSCRIPTEN AND EDGE_MIMALLOC)
list(APPEND EPI_LIBRARIES mimalloc)
endif()

Expand Down

0 comments on commit 308a692

Please sign in to comment.