1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 13:29:56 +00:00
openmw/components/detournavigator/recastglobalallocator.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2.1 KiB
C++
Raw Normal View History

#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_RECASTGLOBALALLOCATOR_H
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_RECASTGLOBALALLOCATOR_H
#include "recasttempallocator.hpp"
2023-12-23 17:37:48 +00:00
#include <DetourAlloc.h>
2023-04-21 00:13:40 +00:00
#include <RecastAlloc.h>
#include <cstdlib>
namespace DetourNavigator
{
class RecastGlobalAllocator
{
public:
static void init() { instance(); }
2023-12-23 17:37:48 +00:00
static void* recastAlloc(size_t size, rcAllocHint hint) { return alloc(size, hint == RC_ALLOC_TEMP); }
static void* detourAlloc(size_t size, dtAllocHint hint) { return alloc(size, hint == DT_ALLOC_TEMP); }
static void* alloc(size_t size, bool temp)
{
void* result = nullptr;
2023-12-23 17:37:48 +00:00
if (rcLikely(temp))
result = tempAllocator().alloc(size);
if (rcUnlikely(!result))
result = allocPerm(size);
return result;
}
static void free(void* ptr)
{
if (rcUnlikely(!ptr))
return;
if (rcLikely(BufferType_temp == getDataPtrBufferType(ptr)))
tempAllocator().free(ptr);
else
{
assert(BufferType_perm == getDataPtrBufferType(ptr));
std::free(getPermDataPtrHeapPtr(ptr));
}
}
private:
2023-12-23 17:37:48 +00:00
RecastGlobalAllocator()
{
rcAllocSetCustom(&RecastGlobalAllocator::recastAlloc, &RecastGlobalAllocator::free);
dtAllocSetCustom(&RecastGlobalAllocator::detourAlloc, &RecastGlobalAllocator::free);
}
static RecastGlobalAllocator& instance()
{
static RecastGlobalAllocator value;
return value;
}
static RecastTempAllocator& tempAllocator()
{
static thread_local RecastTempAllocator value(1024ul * 1024ul);
return value;
}
static void* allocPerm(size_t size)
{
const auto ptr = std::malloc(size + sizeof(std::size_t));
if (rcUnlikely(!ptr))
return ptr;
setPermPtrBufferType(ptr, BufferType_perm);
return getPermPtrDataPtr(ptr);
}
};
}
#endif