r5sdk/r5dev/tier0/memstd.h
Kawe Mazidjatari bb28e160cf Override debug malloc functions and fix bug
* Override debug malloc functions, doesn't seem to be properly supported by the visual studio libraries, but it has to be done, else we crash trying to initialize the DLL (must use the same allocator as the game), a linker flag '/FORCE:MULTIPLE' had to be set to make this work, this should be set for only DEBUG builds in the future!.
* Fixed a bug in '_recalloc_base' where the allocation size was not calculated properly. Only the size was taken into account, but it should had been multiplied by the count.
* Stubbed additional CRT debug only memory debugging code, it will crash on our implementation in debug.
2023-06-29 00:24:22 +02:00

43 lines
1.2 KiB
C++

#ifndef MEMSTD_H
#define MEMSTD_H
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class IMemAlloc
{
public:
// Same functions internally.
virtual void* InternalAlloc(size_t nSize, const char* pFileName, int nLine) = 0;
virtual void* Alloc(size_t nSize) = 0;
// Same functions internally.
virtual void* InternalRealloc(void* pMem, size_t nSize, const char* pFileName, int nLine) = 0;
virtual void* Realloc(void* pMem, size_t nSize) = 0;
// Same as Free, but takes debug parameters.
virtual void InternalFree(void* pMem, const char* pFileName, int nLine) = 0;
virtual void Free(void* pMem) = 0;
virtual size_t GetSize(void* pMem) = 0;
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CStdMemAlloc : public IMemAlloc{};
inline CStdMemAlloc* (*CreateGlobalMemAlloc)() = nullptr;
inline CStdMemAlloc* g_pMemAllocSingleton = nullptr;
inline IMemAlloc* MemAllocSingleton()
{
if (!g_pMemAllocSingleton)
{
g_pMemAllocSingleton = CreateGlobalMemAlloc();
}
return g_pMemAllocSingleton;
}
#endif // MEMSTD_H