Use RTL memory functions by default

Use Real-time library memory functions by default which improves overall performance. Minor cleanups.
Introduce new CMake option to enable/disable RTL memory functions (enabled by default).
This commit is contained in:
Azamat H. Hackimov 2024-06-12 23:09:57 +03:00
parent b4afcc2ec8
commit c8fa74550a
4 changed files with 33 additions and 22 deletions

View File

@ -13,6 +13,7 @@ project(Descent3
option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with ninja)." OFF)
option(FORCE_PORTABLE_INSTALL "Install all files into local directory defined by CMAKE_INSTALL_PREFIX" ON)
option(ENABLE_LOGGER "Enable logging to the terminal" OFF)
option(ENABLE_MEM_RTL "Enable Real-time library memory management functions (disable to verbose memory allocations)" ON)
option(BUILD_TESTING "Enable testing. Requires GTest." OFF)
set(CMAKE_CXX_STANDARD 17)

View File

@ -296,6 +296,9 @@ add_executable(Descent3
WIN32
${HEADERS} ${CPPS} ${PLATFORM_CPPS} ${INCS}
)
target_compile_definitions(Descent3 PUBLIC
$<$<BOOL:${ENABLE_MEM_RTL}>:MEM_USE_RTL>
)
target_link_libraries(Descent3
2dlib AudioEncode bitmap cfile czip d3music dd_video ddebug ddio libmve libacm
fix grtext manage mem misc model module movie stream_audio

View File

@ -74,14 +74,12 @@
#ifndef MEM_H
#define MEM_H
// #define MEM_USE_RTL 1
// Memory management debugging
#ifdef MEM_USE_RTL
#define mem_malloc(d) malloc(d) // Use this if your going to run BoundsChecker
#define mem_free(d) free(d)
#define mem_strdup(d) strdup(d)
#define mem_size(d) _msize(d)
#define mem_size(d) mem_size_sub(d)
#define mem_realloc(d, e) realloc(d, e)
#else
// Use this if your going to NOT run BoundsChecker
@ -102,7 +100,7 @@ extern bool Mem_superlow_memory_mode; // DAJ
void mem_Init();
// shutsdown memory
void mem_shutdown(void);
void mem_shutdown();
// Returns the number of dynamically allocated bytes
int mem_GetTotalMemoryUsed();
@ -124,6 +122,6 @@ int mem_size_sub(void *memblock);
bool mem_dumpmallocstofile(char *filename);
void mem_heapcheck(void);
void mem_heapcheck();
#endif

View File

@ -208,13 +208,9 @@
#endif
#include <cstdint>
#include "init.h"
#include "mem.h"
#include "pserror.h"
#include "pstypes.h"
// #include "args.h"
// #include "ddio.h"
//
// #define MEM_DEBUG
#ifdef MEM_USE_RTL
@ -244,28 +240,34 @@ struct mem_alloc_info {
char file[17];
};
static void *Mem_failsafe_block = NULL;
;
static void *Mem_failsafe_block = nullptr;
bool Mem_low_memory_mode = false;
bool Mem_superlow_memory_mode = false;
// If this is set, the mem library ignores mem_free() calls. All the memory is then freed at once oon exit.
bool Mem_quick_exit = 0;
// If this is set, the mem library ignores mem_free() calls. All the memory is then freed at once on exit.
bool Mem_quick_exit = false;
#if defined(__LINUX__)
// Linux memory management
int LnxTotalMemUsed;
void mem_shutdown(void) {}
void mem_Init(void) { LnxTotalMemUsed = 0; }
int mem_GetTotalMemoryUsed(void) { return LnxTotalMemUsed; }
void mem_shutdown() {}
void mem_Init() { LnxTotalMemUsed = 0; }
int mem_GetTotalMemoryUsed() { return LnxTotalMemUsed; }
void *mem_malloc_sub(int size, const char *file, int line) {
void *new_mem = malloc(size);
if (!new_mem) {
mprintf(0, "Out of memory allocating %d bytes: line %d in %s\n", size, line, file);
Int3();
return NULL;
return nullptr;
}
LnxTotalMemUsed += size;
return new_mem;
}
void mem_free_sub(void *memblock) {
if (memblock) {
#if defined(MACOSX)
@ -276,30 +278,37 @@ void mem_free_sub(void *memblock) {
free(memblock);
}
}
void mem_error_msg(const char *file, int line, int size) {
mprintf(0, "Memory error (size=%d) line %d in %s\n", size, line, file);
Int3();
}
char *mem_strdup_sub(const char *string, const char *file, int line) {
char *ret = strdup(string);
if (!ret) {
mprintf(0, "Out of memory allocating %d bytes: line %d in %s\n", strlen(string) + 1, line, file);
Int3();
return NULL;
return nullptr;
}
return ret;
}
void *mem_realloc_sub(void *mem, int size) { return realloc(mem, size); }
int mem_size_sub(void *memblock) {
#if defined(MACOSX)
#ifdef MACOSX
return malloc_size(memblock);
#else
return malloc_usable_size(memblock);
#endif
}
bool mem_dumpmallocstofile(char *filename) { return false; }
#pragma mark -
#else // defined (WIN32)
#else // defined(__LINUX__)
// Windows memory management
// Uncomment this to detect memory leaks and memory overwrites. Slows down mallocs and frees a little.