mirror of
https://github.com/halpz/re3.git
synced 2025-07-16 20:48:11 +00:00
moved some stuff to MemoryMgr
This commit is contained in:
@ -494,129 +494,4 @@ CommonSize::Init(uint32 size)
|
||||
m_remaining = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void *pMemoryTop;
|
||||
|
||||
void
|
||||
InitMemoryMgr(void)
|
||||
{
|
||||
#ifdef GTA_PS2
|
||||
#error "finish this"
|
||||
#else
|
||||
// randomly allocate 128mb
|
||||
gMainHeap.Init(128*1024*1024);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
RwMemoryFunctions memFuncs = {
|
||||
MemoryMgrMalloc,
|
||||
MemoryMgrFree,
|
||||
MemoryMgrRealloc,
|
||||
MemoryMgrCalloc
|
||||
};
|
||||
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
// game seems to be using heap directly here, but this is nicer
|
||||
void *operator new(size_t sz) { return MemoryMgrMalloc(sz); }
|
||||
void *operator new[](size_t sz) { return MemoryMgrMalloc(sz); }
|
||||
void operator delete(void *ptr) noexcept { MemoryMgrFree(ptr); }
|
||||
void operator delete[](void *ptr) noexcept { MemoryMgrFree(ptr); }
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void*
|
||||
MemoryMgrMalloc(uint32 size)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
void *mem = gMainHeap.Malloc(size);
|
||||
#else
|
||||
void *mem = malloc(size);
|
||||
#endif
|
||||
if(mem > pMemoryTop)
|
||||
pMemoryTop = mem;
|
||||
return mem;
|
||||
}
|
||||
|
||||
void*
|
||||
MemoryMgrRealloc(void *ptr, uint32 size)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
void *mem = gMainHeap.Realloc(ptr, size);
|
||||
#else
|
||||
void *mem = realloc(ptr, size);
|
||||
#endif
|
||||
if(mem > pMemoryTop)
|
||||
pMemoryTop = mem;
|
||||
return mem;
|
||||
}
|
||||
|
||||
void*
|
||||
MemoryMgrCalloc(uint32 num, uint32 size)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
void *mem = gMainHeap.Malloc(num*size);
|
||||
#else
|
||||
void *mem = calloc(num, size);
|
||||
#endif
|
||||
if(mem > pMemoryTop)
|
||||
pMemoryTop = mem;
|
||||
#ifdef FIX_BUGS
|
||||
memset(mem, 0, num*size);
|
||||
#endif
|
||||
return mem;
|
||||
}
|
||||
|
||||
void
|
||||
MemoryMgrFree(void *ptr)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
#ifdef FIX_BUGS
|
||||
// i don't suppose this is handled by RW?
|
||||
if(ptr == nil) return;
|
||||
#endif
|
||||
gMainHeap.Free(ptr);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
void *
|
||||
RwMallocAlign(RwUInt32 size, RwUInt32 align)
|
||||
{
|
||||
#ifdef FIX_BUGS
|
||||
uintptr ptralign = align-1;
|
||||
void *mem = (void *)MemoryMgrMalloc(size + sizeof(uintptr) + ptralign);
|
||||
|
||||
ASSERT(mem != nil);
|
||||
|
||||
void *addr = (void *)((((uintptr)mem) + sizeof(uintptr) + ptralign) & ~ptralign);
|
||||
|
||||
ASSERT(addr != nil);
|
||||
#else
|
||||
void *mem = (void *)MemoryMgrMalloc(size + align);
|
||||
|
||||
ASSERT(mem != nil);
|
||||
|
||||
void *addr = (void *)((((uintptr)mem) + align) & ~(align - 1));
|
||||
|
||||
ASSERT(addr != nil);
|
||||
#endif
|
||||
|
||||
*(((void **)addr) - 1) = mem;
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
void
|
||||
RwFreeAlign(void *mem)
|
||||
{
|
||||
ASSERT(mem != nil);
|
||||
|
||||
void *addr = *(((void **)mem) - 1);
|
||||
|
||||
ASSERT(addr != nil);
|
||||
|
||||
MemoryMgrFree(addr);
|
||||
}
|
||||
|
@ -62,18 +62,6 @@ enum {
|
||||
NUM_FIXED_MEMBLOCKS = 6
|
||||
};
|
||||
|
||||
extern RwMemoryFunctions memFuncs;
|
||||
void InitMemoryMgr(void);
|
||||
|
||||
void *MemoryMgrMalloc(uint32 size);
|
||||
void *MemoryMgrRealloc(void *ptr, uint32 size);
|
||||
void *MemoryMgrCalloc(uint32 num, uint32 size);
|
||||
void MemoryMgrFree(void *ptr);
|
||||
|
||||
void *RwMallocAlign(RwUInt32 size, RwUInt32 align);
|
||||
void RwFreeAlign(void *mem);
|
||||
|
||||
|
||||
template<typename T, uint32 N>
|
||||
class CStack
|
||||
{
|
||||
|
130
src/rw/MemoryMgr.cpp
Normal file
130
src/rw/MemoryMgr.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
#include "common.h"
|
||||
#include "MemoryHeap.h"
|
||||
#include "MemoryMgr.h"
|
||||
|
||||
|
||||
void *pMemoryTop;
|
||||
|
||||
void
|
||||
InitMemoryMgr(void)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
#ifdef GTA_PS2
|
||||
#error "finish this"
|
||||
#else
|
||||
// randomly allocate 128mb
|
||||
gMainHeap.Init(128*1024*1024);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
RwMemoryFunctions memFuncs = {
|
||||
MemoryMgrMalloc,
|
||||
MemoryMgrFree,
|
||||
MemoryMgrRealloc,
|
||||
MemoryMgrCalloc
|
||||
};
|
||||
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
// game seems to be using heap directly here, but this is nicer
|
||||
void *operator new(size_t sz) { return MemoryMgrMalloc(sz); }
|
||||
void *operator new[](size_t sz) { return MemoryMgrMalloc(sz); }
|
||||
void operator delete(void *ptr) noexcept { MemoryMgrFree(ptr); }
|
||||
void operator delete[](void *ptr) noexcept { MemoryMgrFree(ptr); }
|
||||
#endif
|
||||
|
||||
void*
|
||||
MemoryMgrMalloc(size_t size)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
void *mem = gMainHeap.Malloc(size);
|
||||
#else
|
||||
void *mem = malloc(size);
|
||||
#endif
|
||||
if(mem > pMemoryTop)
|
||||
pMemoryTop = mem;
|
||||
return mem;
|
||||
}
|
||||
|
||||
void*
|
||||
MemoryMgrRealloc(void *ptr, size_t size)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
void *mem = gMainHeap.Realloc(ptr, size);
|
||||
#else
|
||||
void *mem = realloc(ptr, size);
|
||||
#endif
|
||||
if(mem > pMemoryTop)
|
||||
pMemoryTop = mem;
|
||||
return mem;
|
||||
}
|
||||
|
||||
void*
|
||||
MemoryMgrCalloc(size_t num, size_t size)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
void *mem = gMainHeap.Malloc(num*size);
|
||||
#else
|
||||
void *mem = calloc(num, size);
|
||||
#endif
|
||||
if(mem > pMemoryTop)
|
||||
pMemoryTop = mem;
|
||||
#ifdef FIX_BUGS
|
||||
memset(mem, 0, num*size);
|
||||
#endif
|
||||
return mem;
|
||||
}
|
||||
|
||||
void
|
||||
MemoryMgrFree(void *ptr)
|
||||
{
|
||||
#ifdef USE_CUSTOM_ALLOCATOR
|
||||
#ifdef FIX_BUGS
|
||||
// i don't suppose this is handled by RW?
|
||||
if(ptr == nil) return;
|
||||
#endif
|
||||
gMainHeap.Free(ptr);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
void *
|
||||
RwMallocAlign(RwUInt32 size, RwUInt32 align)
|
||||
{
|
||||
#ifdef FIX_BUGS
|
||||
uintptr ptralign = align-1;
|
||||
void *mem = (void *)MemoryMgrMalloc(size + sizeof(uintptr) + ptralign);
|
||||
|
||||
ASSERT(mem != nil);
|
||||
|
||||
void *addr = (void *)((((uintptr)mem) + sizeof(uintptr) + ptralign) & ~ptralign);
|
||||
|
||||
ASSERT(addr != nil);
|
||||
#else
|
||||
void *mem = (void *)MemoryMgrMalloc(size + align);
|
||||
|
||||
ASSERT(mem != nil);
|
||||
|
||||
void *addr = (void *)((((uintptr)mem) + align) & ~(align - 1));
|
||||
|
||||
ASSERT(addr != nil);
|
||||
#endif
|
||||
|
||||
*(((void **)addr) - 1) = mem;
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
void
|
||||
RwFreeAlign(void *mem)
|
||||
{
|
||||
ASSERT(mem != nil);
|
||||
|
||||
void *addr = *(((void **)mem) - 1);
|
||||
|
||||
ASSERT(addr != nil);
|
||||
|
||||
MemoryMgrFree(addr);
|
||||
}
|
12
src/rw/MemoryMgr.h
Normal file
12
src/rw/MemoryMgr.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
extern RwMemoryFunctions memFuncs;
|
||||
void InitMemoryMgr(void);
|
||||
|
||||
void *MemoryMgrMalloc(size_t size);
|
||||
void *MemoryMgrRealloc(void *ptr, size_t size);
|
||||
void *MemoryMgrCalloc(size_t num, size_t size);
|
||||
void MemoryMgrFree(void *ptr);
|
||||
|
||||
void *RwMallocAlign(RwUInt32 size, RwUInt32 align);
|
||||
void RwFreeAlign(void *mem);
|
Reference in New Issue
Block a user