1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-08-08 09:01:48 +02:00

Add rpmalloc as a thread-safe alternative.

This commit is contained in:
Sandu Liviu Catalin
2022-06-24 22:51:20 +03:00
parent eca11b73ba
commit 7afc05e52b
8 changed files with 4021 additions and 3 deletions

View File

@@ -133,7 +133,7 @@ if(WIN32 OR MINGW)
target_link_libraries(SqModule wsock32 ws2_32 shlwapi)
endif()
# Link to base libraries
target_link_libraries(SqModule Squirrel fmt::fmt SimpleINI TinyDir xxHash ConcurrentQueue SAJSON CPR UTF8Lib PUGIXML CivetWeb maxminddb libzmq-static)
target_link_libraries(SqModule RPMalloc Squirrel fmt::fmt SimpleINI TinyDir xxHash ConcurrentQueue SAJSON CPR UTF8Lib PUGIXML CivetWeb maxminddb libzmq-static)
# Link to POCO libraries
target_link_libraries(SqModule Poco::Foundation Poco::Crypto Poco::Data Poco::Net)
# Does POCO have SQLite support?

View File

@@ -40,6 +40,7 @@
#include <sqratTable.h>
#include <sqratUtil.h>
#include <fmt/core.h>
#include <rpmalloc.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@@ -173,7 +174,7 @@ void OutputError(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string and throw it as a Sqrat exception.
*/
template < class... Args > void SqThrowF(Args &&... args)
template < class... Args > inline void SqThrowF(Args &&... args)
{
throw Sqrat::Exception(fmt::format(std::forward< Args >(args)...));
}
@@ -181,7 +182,7 @@ template < class... Args > void SqThrowF(Args &&... args)
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string and throw it as a squirrel exception.
*/
template < class... Args > SQRESULT SqThrowErrorF(HSQUIRRELVM vm, Args &&... args)
template < class... Args > inline SQRESULT SqThrowErrorF(HSQUIRRELVM vm, Args &&... args)
{
String msg;
try
@@ -266,4 +267,93 @@ SQMOD_NODISCARD SQFloat PopStackFloat(HSQUIRRELVM vm, SQInteger idx);
*/
SQMOD_NODISCARD bool SToB(const SQChar * str);
/* ------------------------------------------------------------------------------------------------
* RAII allocator initializer.
*/
struct RPMallocInit
{
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
RPMallocInit()
{
if (rpmalloc_initialize() != 0)
{
OutputError("Failed to initialize memory allocator");
}
}
/* --------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
RPMallocInit(const RPMallocInit &) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor (disabled).
*/
RPMallocInit(RPMallocInit &&) noexcept = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~RPMallocInit()
{
if (rpmalloc_is_thread_initialized()) rpmalloc_finalize();
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
RPMallocInit & operator = (const RPMallocInit &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
RPMallocInit & operator = (RPMallocInit &&) noexcept = delete;
};
/* ------------------------------------------------------------------------------------------------
* RAII allocator thread initializer.
*/
struct RPMallocThreadInit
{
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
RPMallocThreadInit()
{
rpmalloc_thread_initialize();
}
/* --------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
RPMallocThreadInit(const RPMallocThreadInit &) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor (disabled).
*/
RPMallocThreadInit(RPMallocThreadInit &&) noexcept = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~RPMallocThreadInit()
{
if (rpmalloc_is_thread_initialized()) rpmalloc_thread_finalize(1);
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
RPMallocThreadInit & operator = (const RPMallocThreadInit &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
RPMallocThreadInit & operator = (RPMallocThreadInit &&) noexcept = delete;
};
} // Namespace:: SqMod

View File

@@ -138,6 +138,8 @@ void ThreadPool::WorkerProc()
bool retry = false;
// Pointer to the dequeued item
Item item;
// Initialize third-party allocator for this thread
auto rpmallocinit = std::make_unique< RPMallocThreadInit >();
// Constantly process items from the queue
while (true)
{

View File

@@ -965,6 +965,11 @@ static void OnServerPerformanceReport(size_t /*entry_count*/, const char * * /*d
} // Namespace:: SqMod
/* ------------------------------------------------------------------------------------------------
* Automatically terminate the third-party allocator.
*/
static std::unique_ptr< SqMod::RPMallocInit > gsRPMallocInit;
/* ------------------------------------------------------------------------------------------------
* Plug-in initialization procedure.
*/
@@ -989,6 +994,8 @@ SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * funcs, PluginCallback
_Info->apiMinorVersion = PLUGIN_API_MINOR;
// Assign the plug-in name
std::snprintf(_Info->name, sizeof(_Info->name), "%s", SQMOD_HOST_NAME);
// Initialize third-party allocator
gsRPMallocInit = std::make_unique< RPMallocInit >();
// Attempt to initialize the logger before anything else
Logger::Get().Initialize(nullptr);
// Attempt to initialize the plug-in core