make cached instance

This commit is contained in:
altalk23 2023-01-30 11:36:31 +03:00
parent 63e9b4c5f7
commit e44a4bb513

View file

@ -29,7 +29,9 @@ namespace geode::addresser {
template <class Class> template <class Class>
Class* friendCreate(typename std::void_t<decltype(static_cast<Class* (*)()>(&Class::create))>*) { Class* friendCreate(typename std::void_t<decltype(static_cast<Class* (*)()>(&Class::create))>*) {
return Class::create(); auto ret = Class::create();
ret->retain();
return ret;
} }
template <class Class> template <class Class>
@ -94,14 +96,10 @@ namespace geode::addresser {
return ins; return ins;
} }
template <HasCreate Class>
static void releaseInstance(Class* ins) {}
template <class Class> template <class Class>
static void releaseInstance(Class* ins) { static Class* cachedInstance() {
// And we delete the new instance because we are good girls static auto ret = generateInstance<Class>();
// and we don't leak memories return ret;
operator delete(ins);
} }
/** /**
@ -115,7 +113,7 @@ namespace geode::addresser {
auto index = indexOf(func); auto index = indexOf(func);
auto thunk = thunkOf(func); auto thunk = thunkOf(func);
auto ins = generateInstance<T>(); auto ins = cachedInstance<T>();
// log::debug("[[" + utils::intToHex((void*)ins) + " + " + utils::intToHex(thunk) + "] + // log::debug("[[" + utils::intToHex((void*)ins) + " + " + utils::intToHex(thunk) + "] +
// " + utils::intToHex(index) + "]"); // " + utils::intToHex(index) + "]");
@ -136,7 +134,6 @@ namespace geode::addresser {
address = *reinterpret_cast<uintptr_t*>(address); address = *reinterpret_cast<uintptr_t*>(address);
} }
#endif #endif
releaseInstance<T>(ins);
return address; return address;
} }