Isolate JKRHeap operator overloads

Fixes #25

This isolates the JKRHeap operator new/delete overloads. Every single new/delete site in the code has been replaced with a macro.

Sadly for new[] and delete[] we have to keep global operators. The global new[] just allocates into malloc() however, and delete[] goes into free() if it's not in a JKRHeap. So that's fine.
This commit is contained in:
PJB3005
2026-02-27 23:11:59 +01:00
parent 2204ad0813
commit 038ef4216f
634 changed files with 3451 additions and 3350 deletions
+5 -4
View File
@@ -1,13 +1,14 @@
#ifndef J2DANIMATION_H
#define J2DANIMATION_H
#include "JSystem/JMath/JMath.h"
#include "JSystem/J3DGraphAnimator/J3DAnimation.h"
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/JMath/JMath.h"
#include "JSystem/JUtility/JUTPalette.h"
typedef struct _GXColor GXColor;
typedef struct _GXColorS10 GXColorS10;
class J2DScreen;
class JUTPalette;
struct ResTIMG;
enum J2DAnmKind {
@@ -286,7 +287,7 @@ public:
mPalette = NULL;
}
~J2DAnmTexPatternTIMGPointer() {
delete mPalette;
JKR_DELETE(mPalette);
}
/* 0x0 */ ResTIMG* mRes;
@@ -306,7 +307,7 @@ public:
ResTIMG* getResTIMG(u16) const;
JUTPalette* getPalette(u16) const;
virtual ~J2DAnmTexPattern() { delete[] mTIMGPtrArray; }
virtual ~J2DAnmTexPattern() { JKR_DELETE_ARRAY(mTIMGPtrArray); }
virtual void searchUpdateMaterialID(J2DScreen*);
u16 getUpdateMaterialNum() const { return mUpdateMaterialNum; }
u16 getUpdateMaterialID(u16 i) const {
+2 -1
View File
@@ -1,6 +1,7 @@
#ifndef JAHPUBDEFINE_H
#define JAHPUBDEFINE_H
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/JUtility/JUTAssert.h"
template<class T>
@@ -12,7 +13,7 @@ public:
static T* newInstance() {
JUT_ASSERT(82, sInstance==NULL);
if (!sInstance) {
sInstance = new T();
sInstance = JKR_NEW T();
}
JUT_ASSERT(85, sInstance!=NULL);
return sInstance;
+29 -3
View File
@@ -209,12 +209,12 @@ public:
return true;
}
MemoryChunk* pMVar4 = field_0x18;
field_0x18 = new (JASKernel::getSystemHeap(), 0) MemoryChunk(pMVar4);
field_0x18 = JKR_NEW_ARGS (JASKernel::getSystemHeap(), 0) MemoryChunk(pMVar4);
if (field_0x18 != NULL) {
return true;
}
JUT_WARN(428, "%s", "Not enough JASSystemHeap");
field_0x18 = new (JKRHeap::getSystemHeap(), 0) MemoryChunk(pMVar4);
field_0x18 = JKR_NEW_ARGS (JKRHeap::getSystemHeap(), 0) MemoryChunk(pMVar4);
if (field_0x18 != NULL) {
return true;
}
@@ -246,7 +246,7 @@ public:
bool r26 = false;
if (chunk != field_0x18 && chunk->isEmpty()) {
MemoryChunk* nextChunk = chunk->getNextChunk();
delete chunk;
JKR_DELETE(chunk);
prevChunk->setNextChunk(nextChunk);
}
return;
@@ -283,6 +283,12 @@ namespace JASKernel {
template <typename T>
class JASPoolAllocObject {
public:
#if TARGET_PC
static void* operator new(size_t n, JKRHeapToken) {
return operator new(n);
}
#endif
static void* operator new(size_t n) {
#if PLATFORM_GCN
JASMemPool<T>& memPool_ = getMemPool_();
@@ -292,6 +298,13 @@ public:
static void* operator new(size_t n, void* ptr) {
return ptr;
}
#if TARGET_PC
static void operator delete(void* ptr, size_t n, JKRHeapToken) {
operator delete(ptr, n);
}
#endif
static void operator delete(void* ptr, size_t n) {
#if PLATFORM_GCN
JASMemPool<T>& memPool_ = getMemPool_();
@@ -362,6 +375,12 @@ public:
template <typename T>
class JASPoolAllocObject_MultiThreaded {
public:
#if TARGET_PC
static void* operator new(size_t n, JKRHeapToken) {
return operator new(n);
}
#endif
static void* operator new(size_t n) {
#if PLATFORM_GCN
JASMemPool_MultiThreaded<T>& memPool_ = getMemPool();
@@ -371,6 +390,13 @@ public:
static void* operator new(size_t n, void* ptr) {
return ptr;
}
#if TARGET_PC
static void operator delete(void* ptr, size_t n, JKRHeapToken) {
return operator delete(ptr, n);
}
#endif
static void operator delete(void* ptr, size_t n) {
#if PLATFORM_GCN
JASMemPool_MultiThreaded<T>& memPool_ = getMemPool();
+4 -2
View File
@@ -1,6 +1,8 @@
#ifndef POINTER_H
#define POINTER_H
#include "JSystem/JKernel/JKRHeap.h"
namespace JGadget {
template<class T>
@@ -18,12 +20,12 @@ public:
#ifdef __MWERKS__
TPointer_delete(T* ptr) : TPointer(ptr) {}
~TPointer_delete() {
delete mPtr;
JKR_DELETE(mPtr);
}
#else
TPointer_delete(T* ptr) : TPointer<T>(ptr) {}
~TPointer_delete() {
delete this->mPtr;
JKR_DELETE(this->mPtr);
}
#endif
};
+3 -2
View File
@@ -2,6 +2,7 @@
#define STD_MEMORY_H
#include "JSystem/JUtility/JUTAssert.h"
#include "JSystem/JKernel/JKRHeap.h"
namespace JGadget {
template <typename T>
@@ -19,12 +20,12 @@ struct TAllocator {
}
void DeallocateRaw(void* mem) {
delete mem;
JKR_DELETE(mem);
}
void construct(T* p, const T& other) {
JUT_ASSERT(67, p!=NULL);
new(p) T(other);
JKR_NEW_ARGS(p) T(other);
}
void destroy(T* p) {
+4 -2
View File
@@ -4,7 +4,9 @@
#include "JSystem/JHostIO/JHIComm.h"
#include <dolphin/os.h>
template<typename T, int I>
#include "JSystem/JKernel/JKRHeap.h"
template <typename T, int I>
class JHIpvector {
public:
JHIpvector() { m_size = 0; }
@@ -89,7 +91,7 @@ public:
static JHIComPortManager<T>* create() {
if (instance == NULL) {
instance = new JHIComPortManager<T>();
instance = JKR_NEW JHIComPortManager<T>();
}
return instance;
+4 -2
View File
@@ -5,9 +5,11 @@
#include "JSystem/JKernel/JKRDisposer.h"
#include <dolphin/os.h>
#include "JKRHeap.h"
/**
* @ingroup jsystem-jkernel
*
*
*/
class JKRAramHeap : public JKRDisposer {
public:
@@ -30,7 +32,7 @@ public:
u32 getUsedSize(u8);
void dump();
void free(JKRAramBlock *block) {
delete block;
JKR_DELETE(block);
}
u8 getCurrentGroupID() const { return mGroupId; }
+37 -7
View File
@@ -210,15 +210,45 @@ public:
static JKRErrorHandler mErrorHandler;
};
void* operator new(size_t size);
void* operator new(size_t size, int alignment);
void* operator new(size_t size, JKRHeap* heap, int alignment);
#if TARGET_PC
enum class JKRHeapToken {
Dummy
};
void* operator new[](size_t size);
void* operator new[](size_t size, int alignment);
void* operator new[](size_t size, JKRHeap* heap, int alignment);
inline void* operator new(size_t, JKRHeapToken, void* where) {
return where;
}
void operator delete(void* ptr);
template<typename T>
void jkrDelete(T* ptr) {
ptr->~T();
operator delete(ptr, JKRHeapToken::Dummy);
}
#define JKR_NEW new (JKRHeapToken::Dummy)
#define JKR_NEW_ARGS(...) new (JKRHeapToken::Dummy, __VA_ARGS__ )
#define JKR_DELETE(expr) jkrDelete(expr)
#define JKR_DELETE_ARRAY(expr) delete[] (expr)
#define JKR_HEAP_TOKEN , JKRHeapToken::Dummy
#define JKR_HEAP_TOKEN_PARAM , JKRHeapToken
#else
#define JKR_NEW new
#define JKR_NEW_ARGS(...) new (__VA_ARGS__ )
#define JKR_DELETE(expr) delete (expr)
#define JKR_DELETE_ARRAY(expr) delete[] (expr)
#define JKR_HEAP_TOKEN
#define JKR_HEAP_TOKEN_PARAM
#endif
void* operator new(size_t size JKR_HEAP_TOKEN_PARAM);
void* operator new(size_t size JKR_HEAP_TOKEN_PARAM, int alignment);
void* operator new(size_t size JKR_HEAP_TOKEN_PARAM, JKRHeap* heap, int alignment);
void* operator new[](size_t size JKR_HEAP_TOKEN_PARAM);
void* operator new[](size_t size JKR_HEAP_TOKEN_PARAM, int alignment);
void* operator new[](size_t size JKR_HEAP_TOKEN_PARAM, JKRHeap* heap, int alignment);
void operator delete(void* ptr JKR_HEAP_TOKEN_PARAM);
void operator delete[](void* ptr);
void JKRDefaultMemoryErrorRoutine(void* heap, u32 size, int alignment);
@@ -16,7 +16,7 @@ struct TCreateObject {
template<class AdaptorT>
static typename AdaptorT::ObjectType* createFromAdaptor(JStudio::stb::data::TParse_TBlock_object const& param_1, AdaptorT* param_2) {
typename AdaptorT::ObjectType* rv = new typename AdaptorT::ObjectType(param_1, param_2);
typename AdaptorT::ObjectType* rv = JKR_NEW typename AdaptorT::ObjectType(param_1, param_2);
if (rv == NULL) {
return NULL;
}
@@ -133,7 +133,7 @@ public:
template<class T>
T* createFromAdaptor(const stb::data::TParse_TBlock_object& param_0, T* param_1) {
T* n = new T(param_0, param_1);
T* n = JKR_NEW T(param_0, param_1);
if (n == NULL) {
return NULL;
@@ -321,7 +321,7 @@ template <class AdaptorT, class ObjectT>
JStudio::TObject* createObject_JSG_(const JStudio::stb::data::TParse_TBlock_object& param_1,
JStage::TObject* param_2, const JStage::TSystem* param_3) {
ObjectT* objt = (ObjectT*)param_2;
AdaptorT* pAdaptor = new AdaptorT(param_3, objt);
AdaptorT* pAdaptor = JKR_NEW AdaptorT(param_3, objt);
if (pAdaptor == NULL) {
return NULL;
}
+6 -4
View File
@@ -3,10 +3,12 @@
#include <dolphin/gx.h>
#include "JSystem/JKernel/JKRHeap.h"
/**
* @ingroup jsystem-jutility
*
*/
* @ingroup jsystem-jutility
*
*/
class JUTGraphFifo {
public:
JUTGraphFifo(u32);
@@ -36,6 +38,6 @@ private:
/* 0x10 */ u8 field_0x10[0xC];
};
inline JUTGraphFifo* JUTCreateFifo(u32 bufSize) { return new JUTGraphFifo(bufSize); }
inline JUTGraphFifo* JUTCreateFifo(u32 bufSize) { return JKR_NEW JUTGraphFifo(bufSize); }
#endif /* JUTGRAPHFIFO_H */
+2 -2
View File
@@ -146,8 +146,8 @@ struct daFmtMng_c : public fopAc_ac_c {
for (int i = 0; i < mFormationLine * mFormationRow; i++, member++) {
fopAcM_delete(member->mNpcId);
}
delete [] mPos;
delete [] mMember;
JKR_DELETE_ARRAY(mPos);
JKR_DELETE_ARRAY(mMember);
}
int getTimeHour() {
+1 -1
View File
@@ -814,7 +814,7 @@ STATIC_ASSERT(sizeof(daNpcT_c) == 0xE40);
#define daNpcT_ct(ptr, ClassName, faceMotionAnmData, motionAnmData, faceMotionSequenceData, faceMotionStepNum, motionSequenceData, motionStepNum, evtData, arcNames) \
if (!fopAcM_CheckCondition(ptr, fopAcCnd_INIT_e)) { \
new (ptr) ClassName(faceMotionAnmData, motionAnmData, faceMotionSequenceData, faceMotionStepNum, motionSequenceData, motionStepNum, evtData, arcNames); \
JKR_NEW_ARGS (ptr) ClassName(faceMotionAnmData, motionAnmData, faceMotionSequenceData, faceMotionStepNum, motionSequenceData, motionStepNum, evtData, arcNames); \
fopAcM_OnCondition(ptr, fopAcCnd_INIT_e); \
}
+1 -1
View File
@@ -51,7 +51,7 @@ public:
}
~_Fairy_Feather_c() {
if (mpMorf != NULL) {
delete mpMorf;
JKR_DELETE(mpMorf);
mpMorf = NULL;
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ public:
int getLuggageParamHigh(u32);
int create();
void create_init();
~daPasserMng_c() { delete [] childProcIds; }
~daPasserMng_c() { JKR_DELETE_ARRAY(childProcIds); }
u8 getDetailLevel() { return argument; }
u8 getPathID() { return fopAcM_GetParam(this); }
+7 -7
View File
@@ -15,8 +15,8 @@ class dDlst_FileSel_c : public dDlst_base_c {
public:
void draw();
virtual ~dDlst_FileSel_c() {
delete Scr;
delete mMessageString;
JKR_DELETE(Scr);
JKR_DELETE(mMessageString);
}
dDlst_FileSel_c() {
@@ -26,7 +26,7 @@ public:
font[1] = mDoExt_getSubFont();
JUT_ASSERT(107, font[1] != NULL);
mMessageString = new dMsgString_c();
mMessageString = JKR_NEW dMsgString_c();
JUT_ASSERT(110, mMessageString != NULL);
}
@@ -78,7 +78,7 @@ public:
class dDlst_FileSelYn_c : public dDlst_base_c {
public:
void draw();
virtual ~dDlst_FileSelYn_c() { delete ScrYn; }
virtual ~dDlst_FileSelYn_c() { JKR_DELETE(ScrYn); }
/* 0x04 */ J2DScreen* ScrYn;
/* 0x08 */ u8 field_0x08[4];
@@ -87,7 +87,7 @@ public:
class dDlst_FileSelDt_c : public dDlst_base_c {
public:
void draw();
virtual ~dDlst_FileSelDt_c() { delete ScrDt; }
virtual ~dDlst_FileSelDt_c() { JKR_DELETE(ScrDt); }
/* 0x04 */ J2DScreen* ScrDt;
/* 0x08 */ J2DPane* mpPane;
@@ -97,7 +97,7 @@ public:
class dDlst_FileSelCp_c : public dDlst_base_c {
public:
void draw();
virtual ~dDlst_FileSelCp_c() { delete Scr; }
virtual ~dDlst_FileSelCp_c() { JKR_DELETE(Scr); }
/* 0x04 */ J2DScreen* Scr;
/* 0x08 */ bool isShow;
@@ -108,7 +108,7 @@ public:
class dDlst_FileSel3m_c : public dDlst_base_c {
public:
void draw();
virtual ~dDlst_FileSel3m_c() { delete Scr3m; }
virtual ~dDlst_FileSel3m_c() { JKR_DELETE(Scr3m); }
/* 0x04 */ J2DScreen* Scr3m;
};
+3 -3
View File
@@ -14,7 +14,7 @@ public:
~fmpTresTypeGroupData_c() {
if (mpNext != NULL) {
delete mpNext;
JKR_DELETE(mpNext);
}
}
@@ -34,7 +34,7 @@ public:
void addTypeGroupData(u8, dTres_c::data_s const*);
~fmpTresTypeGroupDataList_c() {
if (mpTypeGroupDataHead != NULL) {
delete mpTypeGroupDataHead;
JKR_DELETE(mpTypeGroupDataHead);
}
}
fmpTresTypeGroupDataList_c() {
@@ -106,7 +106,7 @@ public:
~dMenu_Fmap_room_data_c() {
if (mp_fmpTresTypeGroupDataListAll != NULL) {
delete mp_fmpTresTypeGroupDataListAll;
JKR_DELETE(mp_fmpTresTypeGroupDataListAll);
}
}
+3 -1
View File
@@ -8,6 +8,8 @@
#include "CaptureScreen.h"
#include <string>
#include "JSystem/JKernel/JKRHeap.h"
void fapGm_After();
void fapGm_Create();
void fapGm_Execute();
@@ -28,7 +30,7 @@ public:
static void executeCaptureScreen() {}
static void createCaptureScreen() {
mCaptureScreen = new CaptureScreen(JFWDisplay::getManager());
mCaptureScreen = JKR_NEW CaptureScreen(JFWDisplay::getManager());
JUT_ASSERT(46, mCaptureScreen != NULL);
}
+1 -1
View File
@@ -14,7 +14,7 @@
#define fopAcM_ct(ptr, ClassName) \
if (!fopAcM_CheckCondition(ptr, fopAcCnd_INIT_e)) { \
new (ptr) ClassName(); \
JKR_NEW_ARGS (ptr) ClassName(); \
fopAcM_OnCondition(ptr, fopAcCnd_INIT_e); \
}
+3 -3
View File
@@ -1,9 +1,9 @@
#ifndef M_DO_M_DO_DVD_THREAD_H
#define M_DO_M_DO_DVD_THREAD_H
#include <dolphin/os.h>
#include "JSystem/JKernel/JKRArchive.h"
#include <dolphin/os.h>
#include <dolphin/os.h>
#include "JSystem/JKernel/JKRHeap.h"
#include "f_pc/f_pc_node.h"
#define mDoDvd_MOUNT_DIRECTION_HEAD 0
@@ -23,7 +23,7 @@ public:
virtual ~mDoDvdThd_command_c();
mDoDvdThd_command_c();
inline s32 sync() { return mIsDone; }
inline void destroy() { delete this; }
inline void destroy() { JKR_DELETE(this); }
virtual s32 execute() = 0;
}; // Size = 0x14