mirror of
https://github.com/Mauler125/r5sdk.git
synced 2025-02-09 19:15:03 +01:00
Add memory callbacks similar to the game's implementation
Required for overriding memalloc callbacks in libraries to feature the game's implementation instead. Named using the 'R_' prefix, as the 'V_' versions were already used.
This commit is contained in:
parent
c8648b9289
commit
982fe66570
@ -22,6 +22,7 @@ add_sources( SOURCE_GROUP "Runtime"
|
||||
"frametask.cpp"
|
||||
"jobthread.cpp"
|
||||
"memaddr.cpp"
|
||||
"memstd.cpp"
|
||||
"memstd.h"
|
||||
"module.cpp"
|
||||
"platform.cpp"
|
||||
|
67
r5dev/tier0/memstd.cpp
Normal file
67
r5dev/tier0/memstd.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
//=============================================================================//
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
//=============================================================================//
|
||||
#include "memstd.h"
|
||||
|
||||
//=============================================================================//
|
||||
// reimplementation of standard C functions for callbacks
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Ideally this didn't exist, but since 'CreateGlobalMemAlloc' is part of the
|
||||
// monolithic game executable, it couldn't be imported early enough to bind
|
||||
// the C functions to feature the internal memalloc system instead. This code
|
||||
// basically replicates the compiled code in the executable, and can be used
|
||||
// to set callbacks up to allow hooking external code with internal without
|
||||
// having to change the source code.
|
||||
//
|
||||
//=============================================================================//
|
||||
extern "C" void* R_malloc(size_t nSize)
|
||||
{
|
||||
Assert(nSize);
|
||||
return MemAllocSingleton()->Alloc<void>(nSize);
|
||||
}
|
||||
|
||||
extern "C" void R_free(void* pBlock)
|
||||
{
|
||||
Assert(pBlock);
|
||||
MemAllocSingleton()->Free(pBlock);
|
||||
}
|
||||
|
||||
extern "C" void* R_realloc(void* pBlock, size_t nSize)
|
||||
{
|
||||
Assert(pBlock && nSize);
|
||||
|
||||
if (nSize)
|
||||
return MemAllocSingleton()->Realloc<void>(pBlock, nSize);
|
||||
else
|
||||
{
|
||||
MemAllocSingleton()->FreeDbg(pBlock, "tier0_static128", 0);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" char* R_strdup(const char* pString)
|
||||
{
|
||||
Assert(pString);
|
||||
|
||||
const size_t nLen = strlen(pString) + 1;
|
||||
void* pNew = MemAllocSingleton()->Alloc<char>(nLen);
|
||||
|
||||
if (!pNew)
|
||||
return nullptr;
|
||||
|
||||
return reinterpret_cast<char*>(memcpy(pNew, pString, nLen));
|
||||
}
|
||||
|
||||
extern "C" void* R_calloc(size_t nCount, size_t nSize)
|
||||
{
|
||||
Assert(nCount && nSize);
|
||||
|
||||
const size_t nTotal = nCount * nSize;
|
||||
void* pNew = MemAllocSingleton()->Alloc<void>(nTotal);
|
||||
|
||||
memset(pNew, NULL, nTotal);
|
||||
return pNew;
|
||||
}
|
@ -1,13 +1,19 @@
|
||||
#ifndef MEMSTD_H
|
||||
#define MEMSTD_H
|
||||
|
||||
void* R_malloc(size_t nSize);
|
||||
void R_free(void* pBlock);
|
||||
void* R_realloc(void* pBlock, size_t nSize);
|
||||
char* R_strdup(const char* pString);
|
||||
void* R_calloc(size_t nCount, size_t nSize);
|
||||
|
||||
class IMemAlloc
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
T* Alloc(size_t nSize)
|
||||
{
|
||||
const static int index = 0;
|
||||
const static int index = 1;
|
||||
return CallVFunc<T*>(index, this, nSize);
|
||||
}
|
||||
template<typename T>
|
||||
@ -17,6 +23,12 @@ public:
|
||||
return CallVFunc<T*>(index, this, pMem, nSize);
|
||||
}
|
||||
template<typename T>
|
||||
void FreeDbg(T* pMem, const char* pFileName, int nLine)
|
||||
{
|
||||
const static int index = 4; // Same as free, but takes debug parameters.
|
||||
CallVFunc<void>(index, this, pMem, pFileName, nLine);
|
||||
}
|
||||
template<typename T>
|
||||
void Free(T* pMem)
|
||||
{
|
||||
const static int index = 5;
|
||||
@ -41,7 +53,7 @@ inline auto v_CreateGlobalMemAlloc = p_CreateGlobalMemAlloc.RCast<CStdMemAlloc*
|
||||
inline CStdMemAlloc** g_pMemAllocSingleton = nullptr;
|
||||
|
||||
|
||||
inline CStdMemAlloc* MemAllocSingleton()
|
||||
inline IMemAlloc* MemAllocSingleton()
|
||||
{
|
||||
if (!(*g_pMemAllocSingleton))
|
||||
{
|
||||
@ -65,7 +77,8 @@ class VMemStd : public IDetour
|
||||
}
|
||||
virtual void GetVar(void) const
|
||||
{
|
||||
g_pMemAllocSingleton = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 57 48 81 EC ?? ?? ?? ?? 41 8B D8").OffsetSelf(0x5A).FindPatternSelf("48 8B", CMemory::Direction::DOWN, 100).ResolveRelativeAddressSelf(0x3, 0x7).RCast<CStdMemAlloc**>();
|
||||
g_pMemAllocSingleton = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 57 48 81 EC ?? ?? ?? ?? 41 8B D8")
|
||||
.OffsetSelf(0x5A).FindPatternSelf("48 8B", CMemory::Direction::DOWN, 100).ResolveRelativeAddressSelf(0x3, 0x7).RCast<CStdMemAlloc**>();
|
||||
}
|
||||
virtual void GetCon(void) const { }
|
||||
virtual void Attach(void) const { }
|
||||
|
Loading…
x
Reference in New Issue
Block a user