Skip to content

Commit

Permalink
dsadasdsad
Browse files Browse the repository at this point in the history
  • Loading branch information
WtzLAS committed Jan 8, 2024
1 parent e29c676 commit 0b6c251
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 184 deletions.
3 changes: 2 additions & 1 deletion NorthstarDLL/NorthstarDLL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ IF EXIST "$(SolutionDir)..\NorthstarCN_Binaries\Northstar.dll" del "$(SolutionDi
<ClInclude Include="server\r2server.h" />
<ClInclude Include="server\serverchathooks.h" />
<ClInclude Include="server\serverpresence.h" />
<ClInclude Include="shared\cmimalloc.h" />
<ClInclude Include="shared\ikcp.h" />
<ClInclude Include="shared\kcpintegration.h" />
<ClInclude Include="shared\keyvalues.h" />
Expand Down Expand Up @@ -365,7 +366,7 @@ IF EXIST "$(SolutionDir)..\NorthstarCN_Binaries\Northstar.dll" del "$(SolutionDi
<ClCompile Include="shared\kcpintegration.cpp" />
<ClCompile Include="shared\keyvalues.cpp" />
<ClCompile Include="shared\maxplayers.cpp" />
<ClCompile Include="shared\mimalloc.cpp" />
<ClCompile Include="shared\cmimalloc.cpp" />
<ClCompile Include="shared\misccommands.cpp" />
<ClCompile Include="shared\playlist.cpp" />
<ClCompile Include="shared\rs.cpp" />
Expand Down
5 changes: 4 additions & 1 deletion NorthstarDLL/NorthstarDLL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@
<ClInclude Include="client\igig\kiero.h">
<Filter>Header Files\client\igig</Filter>
</ClInclude>
<ClInclude Include="shared\cmimalloc.h">
<Filter>Header Files\shared</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="client\audio.cpp">
Expand Down Expand Up @@ -695,7 +698,7 @@
<ClCompile Include="client\igig\kiero.cpp">
<Filter>Source Files\client\igig</Filter>
</ClCompile>
<ClCompile Include="shared\mimalloc.cpp">
<ClCompile Include="shared\cmimalloc.cpp">
<Filter>Source Files\shared</Filter>
</ClCompile>
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions NorthstarDLL/core/tier0.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tier0.h"
#include "shared/cmimalloc.h"

// use the Tier0 namespace for tier0 funcs
namespace Tier0
Expand All @@ -14,6 +15,8 @@ namespace Tier0
typedef Tier0::IMemAlloc* (*CreateGlobalMemAllocType)();
CreateGlobalMemAllocType CreateGlobalMemAlloc;

std::mutex mmMutex;

// needs to be a seperate function, since memalloc.cpp calls it
void TryCreateGlobalMemAlloc()
{
Expand All @@ -23,8 +26,17 @@ void TryCreateGlobalMemAlloc()
Tier0::g_pMemAllocSingleton = CreateGlobalMemAlloc(); // if it already exists, this returns the preexisting IMemAlloc instance
}

AUTOHOOK_INIT()

AUTOHOOK(CGMA, tier0.dll + 0x17B60, void*, __fastcall, ())
{
return new CMiMalloc();
}

ON_DLL_LOAD("tier0.dll", Tier0GameFuncs, (CModule module))
{
AUTOHOOK_DISPATCH();

// shouldn't be necessary, but do this just in case
TryCreateGlobalMemAlloc();

Expand Down
10 changes: 5 additions & 5 deletions NorthstarDLL/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
#define _WINSOCK_DEPRECATED_NO_WARNINGS // temp because i'm very lazy and want to use inet_addr, remove later
#define RAPIDJSON_HAS_STDSTRING 1

// add headers that you want to pre-compile here
#include "core/memalloc.h"

#include <windows.h>
#include <psapi.h>
#include <set>
#include <map>
#include <filesystem>
#include <sstream>

namespace fs = std::filesystem;

#include "core/memory.h"

// add headers that you want to pre-compile here
#include "core/memalloc.h"

// clang-format off
#define assert_msg(exp, msg) assert((exp, msg))
//clang-format on
Expand All @@ -33,6 +34,5 @@ namespace fs = std::filesystem;
#include "MinHook.h"
#include "curl/curl.h"
#include "core/hooks.h"
#include "core/memory.h"

#endif
224 changes: 224 additions & 0 deletions NorthstarDLL/shared/cmimalloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
#include "cmimalloc.h"
#include <mimalloc.h>

void* CMiMalloc::AllocDebug(size_t nSize)
{
return mi_malloc(nSize);
}

void* CMiMalloc::Alloc(size_t nSize)
{
return mi_malloc(nSize);
}

void* CMiMalloc::ReallocDebug(void* pMem, size_t nSize)
{
return mi_realloc(pMem, nSize);
}

void* CMiMalloc::Realloc(void* pMem, size_t nSize)
{
return mi_realloc(pMem, nSize);
}

void CMiMalloc::FreeDebug(void* pMem)
{
return mi_free(pMem);
}

void CMiMalloc::Free(void* pMem)
{
return mi_free(pMem);
}

void* CMiMalloc::Expand_NoLongerSupportedDebug(void* pMem, size_t nSize)
{
return nullptr;
}

void* CMiMalloc::Expand_NoLongerSupported(void* pMem, size_t nSize)
{
return nullptr;
}

size_t CMiMalloc::GetSize(void* pMem)
{
return mi_usable_size(pMem);
}

void CMiMalloc::PushAllocDbgInfo(const char* pFileName, int nLine) {}

void CMiMalloc::PopAllocDbgInfo() {}

__int32 CMiMalloc::CrtSetBreakAlloc(__int32 lNewBreakAlloc)
{
return 0;
}

int CMiMalloc::CrtSetReportMode(int nReportType, int nReportMode)
{
return 0;
}

int CMiMalloc::CrtIsValidHeapPointer(const void* pMem)
{
return 1;
}

int CMiMalloc::CrtIsValidPointer(const void* pMem, unsigned int size, int access)
{
return 1;
}

int CMiMalloc::CrtCheckMemory()
{
return 1;
}

int CMiMalloc::CrtSetDbgFlag(int nNewFlag)
{
return 0;
}

void CMiMalloc::CrtMemCheckpoint(_CrtMemState* pState) {}

void CMiMalloc::DumpStats()
{
return mi_stats_print(NULL);
}

void PrintToFile(const char* msg, void* arg) {
FILE* fp = (FILE*)arg;
fprintf_s(fp, msg);
}

void CMiMalloc::DumpStatsFileBase(char const* pchFileBase)
{
std::string filename(pchFileBase);
filename.append(".txt");
FILE* fp;
errno_t err = fopen_s(&fp, filename.c_str(), "w+");
if (err != 0)
{
return;
}
mi_stats_print_out(PrintToFile, fp);
}

size_t CMiMalloc::ComputeMemoryUsedBy(char const* pchSubStr)
{
return 0;
}

__int64 CMiMalloc::nullsub_1()
{
return 0xDC00000;
}

void* CMiMalloc::CrtSetReportFile(int nRptType, void* hFile)
{
return nullptr;
}

void* CMiMalloc::CrtSetReportHook(void* pfnNewHook)
{
return nullptr;
}

int CMiMalloc::CrtDbgReport(int nRptType, const char* szFile, int nLine, const char* szModule, const char* pMsg)
{
return 0;
}

int CMiMalloc::heapchk()
{
return _HEAPOK;
}

bool CMiMalloc::IsDebugHeap()
{
return false;
}

void CMiMalloc::GetActualDbgInfo(const char*& pFileName, int& nLine) {}

void CMiMalloc::RegisterAllocation(const char* pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime) {}

void CMiMalloc::RegisterDeallocation(const char* pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime) {}

int CMiMalloc::GetVersion()
{
return 1;
}

void CMiMalloc::CompactHeap()
{
return mi_collect(false);
}

MemAllocFailHandler_t CMiMalloc::SetAllocFailHandler(MemAllocFailHandler_t pfnMemAllocFailHandler)
{
auto old = m_pfnFailHandler;
m_pfnFailHandler = pfnMemAllocFailHandler;
return old;
}

void CMiMalloc::DumpBlockStats(void* pMem) {}

void CMiMalloc::SetStatsExtraInfo(const char* pMapName, const char* pComment) {}

size_t CMiMalloc::MemoryAllocFailed()
{
return m_sMemoryAllocFailed;
}

void CMiMalloc::CompactIncremental() {}

void CMiMalloc::OutOfMemory(size_t nBytesAttempted) {}

void* CMiMalloc::RegionAllocDebug(int region, size_t nSize)
{
return mi_malloc(nSize);
}

void* CMiMalloc::RegionAlloc(int region, size_t nSize)
{
return mi_malloc(nSize);
}

void CMiMalloc::GlobalMemoryStatus(size_t* pUsedMemory, size_t* pFreeMemory)
{
if (pUsedMemory && pFreeMemory)
{
*pUsedMemory = 0;
*pFreeMemory = 0;
}
}

__int64 CMiMalloc::AllocateVirtualMemorySection(size_t numMaxBytes)
{
return 0;
}

int CMiMalloc::GetGenericMemoryStats(GenericMemoryStat_t** ppMemoryStats)
{
if (ppMemoryStats)
{
*ppMemoryStats = nullptr;
}

return 0;
}

CMiMalloc::~CMiMalloc() {}

CMiMalloc* CMiMalloc::instance()
{
return nullptr;
}






64 changes: 64 additions & 0 deletions NorthstarDLL/shared/cmimalloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

typedef size_t (*MemAllocFailHandler_t)(size_t);

struct GenericMemoryStat_t
{
const char* name;
int value;
};

class CMiMalloc
{
public:
virtual void* AllocDebug(size_t nSize);
virtual void* Alloc(size_t nSize);
virtual void* ReallocDebug(void* pMem, size_t nSize);
virtual void* Realloc(void* pMem, size_t nSize);
virtual void FreeDebug(void* pMem);
virtual void Free(void* pMem);
virtual void* Expand_NoLongerSupportedDebug(void* pMem, size_t nSize);
virtual void* Expand_NoLongerSupported(void* pMem, size_t nSize);
virtual size_t GetSize(void* pMem);
virtual void PushAllocDbgInfo(const char* pFileName, int nLine);
virtual void PopAllocDbgInfo();
virtual __int32 CrtSetBreakAlloc(__int32 lNewBreakAlloc);
virtual int CrtSetReportMode(int nReportType, int nReportMode);
virtual int CrtIsValidHeapPointer(const void* pMem);
virtual int CrtIsValidPointer(const void* pMem, unsigned int size, int access);
virtual int CrtCheckMemory();
virtual int CrtSetDbgFlag(int nNewFlag);
virtual void CrtMemCheckpoint(_CrtMemState* pState);
virtual void DumpStats();
virtual void DumpStatsFileBase(char const* pchFileBase);
virtual size_t ComputeMemoryUsedBy(char const* pchSubStr);
virtual __int64 nullsub_1();
virtual void* CrtSetReportFile(int nRptType, void* hFile);
virtual void* CrtSetReportHook(void* pfnNewHook);
virtual int CrtDbgReport(int nRptType, const char* szFile, int nLine, const char* szModule, const char* pMsg);
virtual int heapchk();
virtual bool IsDebugHeap();
virtual void GetActualDbgInfo(const char*& pFileName, int& nLine);
virtual void RegisterAllocation(const char* pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime);
virtual void RegisterDeallocation(const char* pFileName, int nLine, size_t nLogicalSize, size_t nActualSize, unsigned nTime);
virtual int GetVersion();
virtual void CompactHeap();
virtual MemAllocFailHandler_t SetAllocFailHandler(MemAllocFailHandler_t pfnMemAllocFailHandler);
virtual void DumpBlockStats(void* pMem);
virtual void SetStatsExtraInfo(const char* pMapName, const char* pComment);
virtual size_t MemoryAllocFailed();
virtual void CompactIncremental();
virtual void OutOfMemory(size_t nBytesAttempted = 0);
virtual void* RegionAllocDebug(int region, size_t nSize);
virtual void* RegionAlloc(int region, size_t nSize);
virtual void GlobalMemoryStatus(size_t* pUsedMemory, size_t* pFreeMemory);
virtual __int64 AllocateVirtualMemorySection(size_t numMaxBytes);
virtual int GetGenericMemoryStats(GenericMemoryStat_t** ppMemoryStats);
virtual ~CMiMalloc();

static CMiMalloc* instance();

private:
MemAllocFailHandler_t m_pfnFailHandler;
size_t m_sMemoryAllocFailed = 0;
};
Loading

0 comments on commit 0b6c251

Please sign in to comment.