rodecomp/Source/Utility/Async/NRecycleObject.h
ragnarok-clanker a47d20f8be Reconstruct readable C++ and organize cohesive source subsystems
Administrative source import: native x86 compilation passed for 423 authored units. Source placement and submission states are migrated separately; this commit does not imply peer acceptance.

Previous head: 35d2a8cb36
Payload SHA256: 592f016d2730c5593bb653aa054c1e5f76a6df72c8f5a09e66f74876e2e33dee
2026-10-25 07:42:41 +00:00

102 lines
2.4 KiB
C++

#ifndef RAGNAROK_SOURCE_UTILITY_ASYNC_NRECYCLEOBJECT_H
#define RAGNAROK_SOURCE_UTILITY_ASYNC_NRECYCLEOBJECT_H
#include "Utility/Threading/NSyncObject.h"
#include <algorithm>
#include <cstdint>
#include <deque>
#include <list>
#include <new>
#include <string>
#include <utility>
#include <windows.h>
class NRecycleObject {
public:
explicit NRecycleObject(std::string poolName);
virtual void Reset() = 0;
virtual ~NRecycleObject();
// OFFSET: 0x00731C60
// FUNCTION: fn_07563
std::string PoolName() const {
return this->m_poolName;
}
protected:
/* 0004 */ std::string m_poolName;
};
class NRecycleObjectPoolInter {
public:
virtual NRecycleObject* Acquire() = 0;
virtual void Release(NRecycleObject* object) = 0;
virtual ~NRecycleObjectPoolInter();
};
template <class T> class NRecycleObjectPool : public NRecycleObjectPoolInter {
public:
// OFFSET: 0x00575AC0
// FUNCTION: fn_02529
explicit NRecycleObjectPool(std::uint32_t minimumFree) : m_enabled(false), m_minimumFree(minimumFree) {
InitializeCriticalSection(&this->m_criticalSection);
this->m_enabled = true;
}
// OFFSET: 0x005775F0
// FUNCTION: fn_02536
~NRecycleObjectPool() override {
for (T* object : this->m_objects) {
delete object;
}
DeleteCriticalSection(&this->m_criticalSection);
}
// OFFSET: 0x005782F0
// FUNCTION: fn_02552
T* Acquire() override {
NSyncObject held(&this->m_criticalSection);
T* object;
if (this->m_free.size() >= this->m_minimumFree) {
object = this->m_free.front();
this->m_free.pop_front();
} else {
object = new (std::nothrow) T;
if (object) {
this->m_objects.push_back(object);
}
}
if (object) {
object->Reset();
}
return object;
}
// OFFSET: 0x0057A6F0
// FUNCTION: fn_02567
void Release(NRecycleObject* object) override {
NSyncObject held(&this->m_criticalSection);
if (object) {
auto owned = std::find(this->m_objects.begin(), this->m_objects.end(), object);
if (owned != this->m_objects.end()) {
this->m_free.push_back(*owned);
}
}
}
protected:
/* 0004 */ bool m_enabled;
/* 0008 */ std::deque<T*> m_free;
/* 001C */ std::list<T*> m_objects;
/* 0024 */ std::uint32_t m_minimumFree;
/* 0028 */ CRITICAL_SECTION m_criticalSection;
};
#if defined(_WIN32) && !defined(_WIN64)
static_assert(sizeof(NRecycleObject) == 28);
static_assert(sizeof(NRecycleObjectPoolInter) == 4);
#endif
#endif