diff --git a/Detour/Include/DetourCommon.h b/Detour/Include/DetourCommon.h index 3cee3f6..ccae126 100644 --- a/Detour/Include/DetourCommon.h +++ b/Detour/Include/DetourCommon.h @@ -182,6 +182,17 @@ inline unsigned int dtIlog2(unsigned int v) inline int dtAlign4(int x) { return (x+3) & ~3; } +inline unsigned int dtHashInt(unsigned int a) +{ + a += ~(a<<15); + a ^= (a>>10); + a += (a<<3); + a ^= (a>>6); + a += ~(a<<11); + a ^= (a>>16); + return a; +} + inline int dtOppositeTile(int side) { return (side+4) & 0x7; } inline float dtVdot2D(const float* u, const float* v) diff --git a/Detour/Include/DetourNode.h b/Detour/Include/DetourNode.h index af9515f..30d1e76 100644 --- a/Detour/Include/DetourNode.h +++ b/Detour/Include/DetourNode.h @@ -80,16 +80,6 @@ public: inline unsigned short getNext(int i) const { return m_next[i]; } private: - inline unsigned int hashint(unsigned int a) const - { - a += ~(a<<15); - a ^= (a>>10); - a += (a<<3); - a ^= (a>>6); - a += ~(a<<11); - a ^= (a>>16); - return a; - } dtNode* m_nodes; unsigned short* m_first; diff --git a/Detour/Source/DetourNode.cpp b/Detour/Source/DetourNode.cpp index 2e8ee50..3cd1c50 100644 --- a/Detour/Source/DetourNode.cpp +++ b/Detour/Source/DetourNode.cpp @@ -19,6 +19,7 @@ #include "DetourNode.h" #include "DetourAlloc.h" #include "DetourAssert.h" +#include "DetourCommon.h" #include ////////////////////////////////////////////////////////////////////////////////////////// @@ -30,6 +31,9 @@ dtNodePool::dtNodePool(int maxNodes, int hashSize) : m_hashSize(hashSize), m_nodeCount(0) { + dtAssert(dtNextPow2(m_hashSize) == (unsigned int)m_hashSize); + dtAssert(m_maxNodes > 0); + m_nodes = (dtNode*)dtAlloc(sizeof(dtNode)*m_maxNodes, DT_ALLOC_PERM); m_next = (unsigned short*)dtAlloc(sizeof(unsigned short)*m_maxNodes, DT_ALLOC_PERM); m_first = (unsigned short*)dtAlloc(sizeof(unsigned short)*hashSize, DT_ALLOC_PERM); @@ -57,7 +61,7 @@ void dtNodePool::clear() const dtNode* dtNodePool::findNode(unsigned int id) const { - unsigned int bucket = hashint(id) & (m_hashSize-1); + unsigned int bucket = dtHashInt(id) & (m_hashSize-1); unsigned short i = m_first[bucket]; while (i != DT_NULL_IDX) { @@ -70,7 +74,7 @@ const dtNode* dtNodePool::findNode(unsigned int id) const dtNode* dtNodePool::getNode(unsigned int id) { - unsigned int bucket = hashint(id) & (m_hashSize-1); + unsigned int bucket = dtHashInt(id) & (m_hashSize-1); unsigned short i = m_first[bucket]; dtNode* node = 0; while (i != DT_NULL_IDX) @@ -107,6 +111,8 @@ dtNodeQueue::dtNodeQueue(int n) : m_capacity(n), m_size(0) { + dtAssert(m_capacity > 0); + m_heap = (dtNode**)dtAlloc(sizeof(dtNode*)*(m_capacity+1), DT_ALLOC_PERM); dtAssert(m_heap); }