variable table init in mxomni, some reshuffling

This commit is contained in:
disinvite 2023-07-15 13:50:30 -04:00
parent 6a0bb6ca1c
commit 957cb20164
4 changed files with 140 additions and 86 deletions

View File

@ -29,94 +29,57 @@ class MxHashTableNode
MxHashTableNode *m_next; MxHashTableNode *m_next;
}; };
// See MxOmni::Create
// VTABLE 0x100dc1b0
template <class T> template <class T>
class MxHashTable : public MxCore class HashTableParent : public MxCore
{
public:
HashTableParent() {
m_numKeys = 0;
m_customDestructor = Destroy;
}
// OFFSET: LEGO1 0x100afd30
static void Destroy(T*) {};
// OFFSET: LEGO1 0x100afcd0
virtual MxS8 Compare(T*, T*) = 0;
protected:
MxU32 m_numKeys; // +0x8
void (*m_customDestructor)(T*); // +0xc
};
// VTABLE 0x100dc1e8
template <class T>
class MxHashTable : protected HashTableParent<T>
{ {
public: public:
MxHashTable() MxHashTable()
{ {
m_numSlots = HASH_TABLE_INIT_SIZE; m_numSlots = HASH_TABLE_INIT_SIZE;
m_slots = new MxHashTableNode<T>*[HASH_TABLE_INIT_SIZE]; m_slots = new MxHashTableNode<T>*[HASH_TABLE_INIT_SIZE];
memset(m_slots, 0, sizeof(MxHashTableNode<T> *) * m_numSlots);
m_resizeOption = HASH_TABLE_OPT_NO_EXPAND; m_resizeOption = HASH_TABLE_OPT_NO_EXPAND;
} }
~MxHashTable() virtual ~MxHashTable();
{
// TODO: Walk table to delete nodes?
delete[] m_slots;
}
// for convenience void Resize();
inline int GetBucket(int hash) { void MxHashTable::Add(T* );
return hash % m_numSlots;
}
// OFFSET: LEGO1 0x100b7ab0 virtual MxS8 Compare(T*, T*) = 0;
void MxHashTable::Resize()
{
// Save a reference to the current table
// so we can walk nodes and re-insert
MxU32 old_size = m_numSlots;
MxHashTableNode<T> **old_table = m_slots;
switch (m_resizeOption) { // OFFSET: LEGO1 0x100afdc0
case HASH_TABLE_OPT_EXPAND_ADD: virtual MxU32 Hash(T*) = 0;
m_numSlots = old_size + m_increaseAmount;
break;
case HASH_TABLE_OPT_EXPAND_MULTIPLY:
m_numSlots = old_size * m_increaseFactor;
break;
}
MxHashTableNode<T> **new_table = new MxHashTableNode<T>*[m_numSlots]; // FIXME: use of friend here?
// FIXME: order? m_numKeys set after `rep stosd` friend class MxHashTableCursor<T>;
m_slots = new_table;
memset(m_slots, 0, sizeof(MxHashTableNode<T> *) * m_numSlots);
m_numKeys = 0;
for (int i = 0; i != old_size; i++) { protected:
MxHashTableNode<T> *t = old_table[i]; void _NodeInsert(MxHashTableNode<T> *);
while (t) {
MxHashTableNode<T> *next = t->m_next;
_NodeInsert(t);
t = next;
}
}
delete[] old_table;
}
// OFFSET: LEGO1 0x100b7b80
void MxHashTable::_NodeInsert(MxHashTableNode<T> *p_node) {
int bucket = GetBucket(p_node->m_hash);
p_node->m_next = m_slots[bucket];
if (m_slots[bucket])
m_slots[bucket]->m_prev = p_node;
m_slots[bucket] = p_node;
m_numKeys++;
}
void MxHashTable::Add(T* p_newobj)
{
if (m_resizeOption && ((m_numKeys + 1) / m_numSlots) > m_autoResizeRatio)
MxHashTable<T>::Resize();
MxU32 hash = Hash(p_newobj);
MxHashTableNode<T> *node = new MxHashTableNode<T>(p_newobj, hash);
MxHashTable<T>::_NodeInsert(node);
}
virtual MxS8 Compare(T*, T*);
virtual MxU32 Hash(T*);
//private:
MxU32 m_numKeys; // +0x8
void (*m_customDestructor)(T*); // +0xc
MxHashTableNode<T> **m_slots; // +0x10 MxHashTableNode<T> **m_slots; // +0x10
MxU32 m_numSlots; // +0x14 MxU32 m_numSlots; // +0x14
MxU32 m_autoResizeRatio; MxU32 m_autoResizeRatio;
@ -125,7 +88,7 @@ class MxHashTable : public MxCore
// on the value of m_resizeOption. Hard to say whether this is how the devs // on the value of m_resizeOption. Hard to say whether this is how the devs
// did it, but a simple cast in either direction doesn't match. // did it, but a simple cast in either direction doesn't match.
union { union {
MxS64 m_increaseAmount; MxU32 m_increaseAmount;
double m_increaseFactor; double m_increaseFactor;
}; };
}; };
@ -143,7 +106,7 @@ class MxHashTableCursor : public MxCore
MxBool Find(T *p_obj) MxBool Find(T *p_obj)
{ {
MxU32 hash = m_table->Hash(p_obj); MxU32 hash = m_table->Hash(p_obj);
int bucket = m_table->GetBucket(hash); int bucket = hash % m_table->m_numSlots;
MxHashTableNode<T> *t = m_table->m_slots[bucket]; MxHashTableNode<T> *t = m_table->m_slots[bucket];
@ -172,7 +135,7 @@ class MxHashTableCursor : public MxCore
m_match->m_prev->m_next = m_match->m_next; m_match->m_prev->m_next = m_match->m_next;
} else { } else {
// No "prev" node, so move "next" to the head of the list. // No "prev" node, so move "next" to the head of the list.
int bucket = m_table->GetBucket(m_match->m_hash); int bucket = m_match->m_hash % m_table->m_numSlots;
m_table->m_slots[bucket] = m_match->m_next; m_table->m_slots[bucket] = m_match->m_next;
} }
@ -189,4 +152,90 @@ class MxHashTableCursor : public MxCore
MxHashTableNode<T> *m_match; MxHashTableNode<T> *m_match;
}; };
template <class T>
// OFFSET: LEGO1 0x100b0bd0
MxHashTable<T>::~MxHashTable()
{
for (int i = 0; i < m_numSlots; i++) {
MxHashTableNode<T> *t = m_slots[i];
while (t) {
MxHashTableNode<T> *next = t->m_next;
m_customDestructor(t->m_obj);
delete t;
t = next;
}
}
m_numKeys = 0;
memset(m_slots, 0, sizeof(MxHashTableNode<T> *) * m_numSlots);
delete[] m_slots;
}
template <class T>
// OFFSET: LEGO1 0x100b7ab0
inline void MxHashTable<T>::Resize()
{
// Save a reference to the current table
// so we can walk nodes and re-insert
MxU32 old_size = m_numSlots;
MxHashTableNode<T> **old_table = m_slots;
switch (m_resizeOption) {
case HASH_TABLE_OPT_EXPAND_ADD:
m_numSlots = old_size + m_increaseAmount;
break;
case HASH_TABLE_OPT_EXPAND_MULTIPLY:
m_numSlots = old_size * m_increaseFactor;
break;
}
MxHashTableNode<T> **new_table = new MxHashTableNode<T>*[m_numSlots];
// FIXME: order? m_numKeys set after `rep stosd`
m_slots = new_table;
memset(m_slots, 0, sizeof(MxHashTableNode<T> *) * m_numSlots);
m_numKeys = 0;
for (int i = 0; i != old_size; i++) {
MxHashTableNode<T> *t = old_table[i];
while (t) {
MxHashTableNode<T> *next = t->m_next;
_NodeInsert(t);
t = next;
}
}
delete[] old_table;
}
template <class T>
// OFFSET: LEGO1 0x100b7b80
inline void MxHashTable<T>::_NodeInsert(MxHashTableNode<T> *p_node)
{
int bucket = p_node->m_hash % m_numSlots;
p_node->m_next = m_slots[bucket];
if (m_slots[bucket])
m_slots[bucket]->m_prev = p_node;
m_slots[bucket] = p_node;
m_numKeys++;
}
template <class T>
inline void MxHashTable<T>::Add(T* p_newobj)
{
if (m_resizeOption && ((m_numKeys + 1) / m_numSlots) > m_autoResizeRatio)
MxHashTable<T>::Resize();
MxU32 hash = Hash(p_newobj);
MxHashTableNode<T> *node = new MxHashTableNode<T>(p_newobj, hash);
MxHashTable<T>::_NodeInsert(node);
}
#endif // MXHASHTABLE_H #endif // MXHASHTABLE_H

View File

@ -104,6 +104,15 @@ void MxOmni::SetInstance(MxOmni *instance)
// OFFSET: LEGO1 0x100af0c0 // OFFSET: LEGO1 0x100af0c0
MxResult MxOmni::Create(MxOmniCreateParam &p) MxResult MxOmni::Create(MxOmniCreateParam &p)
{ {
if (p.CreateFlags().CreateVariableTable())
{
MxVariableTable *variableTable = new MxVariableTable();
this->m_variableTable = variableTable;
if (variableTable == NULL)
return FAILURE;
}
if (p.CreateFlags().CreateTimer()) if (p.CreateFlags().CreateTimer())
{ {
MxTimer *timer = new MxTimer(); MxTimer *timer = new MxTimer();

View File

@ -33,11 +33,6 @@ void MxVariableTable::SetVariable(const char *p_key, const char *p_value)
} else { } else {
MxHashTable<MxVariable>::Add(var); MxHashTable<MxVariable>::Add(var);
} }
// create new var object here?
// Resize inlined
// Add not
} }
// OFFSET: LEGO1 0x100b7740 // OFFSET: LEGO1 0x100b7740
@ -50,11 +45,6 @@ void MxVariableTable::SetVariable(MxVariable *var)
cursor.DeleteMatch(); cursor.DeleteMatch();
MxHashTable<MxVariable>::Add(var); MxHashTable<MxVariable>::Add(var);
// we already have the new variable to add.
// Resize not
// Add inlined
} }
// OFFSET: LEGO1 0x100b78f0 // OFFSET: LEGO1 0x100b78f0

View File

@ -8,13 +8,19 @@
// VTABLE 0x100dc1c8 // VTABLE 0x100dc1c8
// SIZE 0x28 // SIZE 0x28
class MxVariableTable : protected MxHashTable<MxVariable> class MxVariableTable : public MxHashTable<MxVariable>
{ {
public: public:
MxVariableTable() {
m_customDestructor = Destroy;
}
__declspec(dllexport) void SetVariable(const char *key, const char *value); __declspec(dllexport) void SetVariable(const char *key, const char *value);
__declspec(dllexport) void SetVariable(MxVariable *var); __declspec(dllexport) void SetVariable(MxVariable *var);
__declspec(dllexport) const char * GetVariable(const char *key); __declspec(dllexport) const char * GetVariable(const char *key);
// OFFSET: LEGO1 0x100afdb0
static void Destroy(MxVariable *p_obj) { p_obj->Destroy(); }
virtual MxS8 Compare(MxVariable *, MxVariable *); // +0x14 virtual MxS8 Compare(MxVariable *, MxVariable *); // +0x14
virtual MxU32 Hash(MxVariable *); // +0x18 virtual MxU32 Hash(MxVariable *); // +0x18
}; };