mirror of
https://github.com/k4zmu2a/SpaceCadetPinball.git
synced 2024-11-17 23:20:19 +01:00
d594f5fdb7
Fixed memory leaks in uninit. Fixed some of the code analysis warnings. Enabled /MP build. Cleaned up the code.
40 lines
718 B
C++
40 lines
718 B
C++
#include "pch.h"
|
|
#include "memory.h"
|
|
|
|
size_t memory::use_total;
|
|
int memory::critical_allocation;
|
|
void (*memory::critical_callback)();
|
|
std::map<void*, size_t> memory::alloc_map{};
|
|
|
|
void memory::init(void (*callback)())
|
|
{
|
|
critical_callback = callback;
|
|
}
|
|
|
|
char* memory::allocate(size_t size)
|
|
{
|
|
auto buf = static_cast<char*>(malloc(size));
|
|
if (!buf)
|
|
{
|
|
if (critical_allocation && critical_callback)
|
|
critical_callback();
|
|
return nullptr;
|
|
}
|
|
|
|
use_total += size;
|
|
alloc_map[buf] = size;
|
|
return buf;
|
|
}
|
|
|
|
void memory::free(void* buf)
|
|
{
|
|
auto alloc = alloc_map.find(buf);
|
|
if (alloc == alloc_map.end())
|
|
{
|
|
assertm(false, "Unknown memory type");
|
|
return;
|
|
}
|
|
|
|
use_total -= alloc->second;
|
|
std::free(alloc->first);
|
|
}
|