2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _CMEMORY
|
|
|
|
#define _CMEMORY
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-10-16 20:20:36 +00:00
|
|
|
#include "Kyoto/Alloc/CCallStack.hpp"
|
2022-10-21 01:32:04 +00:00
|
|
|
#include "Kyoto/Alloc/IAllocator.hpp"
|
|
|
|
#include "types.h"
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-10-12 08:07:35 +00:00
|
|
|
class COsContext;
|
|
|
|
class CMemory {
|
|
|
|
static IAllocator* mpAllocator;
|
|
|
|
static bool mInitialized;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void Startup(COsContext& ctx);
|
|
|
|
static void Shutdown();
|
|
|
|
static void SetAllocator(COsContext& ctx, IAllocator& allocator);
|
|
|
|
static IAllocator* GetAllocator();
|
2022-10-16 20:20:36 +00:00
|
|
|
static void* Alloc(size_t len, IAllocator::EHint hint = IAllocator::kHI_None,
|
2022-10-21 01:32:04 +00:00
|
|
|
IAllocator::EScope scope = IAllocator::kSC_Unk1,
|
|
|
|
IAllocator::EType type = IAllocator::kTP_Heap,
|
2022-10-16 20:20:36 +00:00
|
|
|
const CCallStack& callstack = CCallStack(-1, "??(??)"));
|
2022-10-12 08:07:35 +00:00
|
|
|
static void Free(const void* ptr);
|
|
|
|
static void SetOutOfMemoryCallback(IAllocator::FOutOfMemoryCb callback, const void* context);
|
|
|
|
static void OffsetFakeStatics(int);
|
|
|
|
};
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-10-01 06:19:09 +00:00
|
|
|
void* operator new(size_t sz, const char*, const char*);
|
|
|
|
void* operator new[](size_t sz, const char*, const char*);
|
2022-10-21 00:26:38 +00:00
|
|
|
// TODO remove
|
2022-10-21 01:32:04 +00:00
|
|
|
#ifdef __MWERKS__
|
2022-10-01 06:19:09 +00:00
|
|
|
inline void* operator new(size_t sz) { return operator new(sz, "??(??)", nullptr); }
|
|
|
|
inline void* operator new[](size_t sz) { return operator new[](sz, "??(??)", nullptr); }
|
2022-10-21 01:32:04 +00:00
|
|
|
#else
|
|
|
|
__attribute__((weak)) void* operator new(size_t sz) { return operator new(sz, "??(??)", nullptr); }
|
|
|
|
__attribute__((weak)) void* operator new[](size_t sz) {
|
|
|
|
return operator new[](sz, "??(??)", nullptr);
|
|
|
|
}
|
|
|
|
#endif
|
2022-04-10 00:17:06 +00:00
|
|
|
// placement new
|
2022-10-01 06:19:09 +00:00
|
|
|
inline void* operator new(size_t n, void* ptr) { return ptr; };
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-10-21 01:32:04 +00:00
|
|
|
#ifdef __MWERKS__
|
2022-04-10 00:17:06 +00:00
|
|
|
inline void operator delete(void* ptr) { CMemory::Free(ptr); }
|
2022-10-01 06:19:09 +00:00
|
|
|
inline void operator delete[](void* ptr) { CMemory::Free(ptr); }
|
2022-12-22 04:26:49 +00:00
|
|
|
#define NEW new ("??(??)", nullptr)
|
2022-10-21 01:32:04 +00:00
|
|
|
#else
|
|
|
|
__attribute__((weak)) void operator delete(void* ptr) { CMemory::Free(ptr); }
|
|
|
|
__attribute__((weak)) void operator delete[](void* ptr) { CMemory::Free(ptr); }
|
2022-12-22 04:26:49 +00:00
|
|
|
#define NEW new
|
2022-10-21 01:32:04 +00:00
|
|
|
#endif
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _CMEMORY
|