From 811e1c3a9660bbda6a031bd855dfd212620e77fa Mon Sep 17 00:00:00 2001 From: kobalicek Date: Tue, 27 May 2014 22:30:29 +0200 Subject: [PATCH] Removed sentinel from const-pool binary tree (NULL is used instead). --- src/asmjit/base/constpool.cpp | 43 +++++++++++++++++------------------ src/asmjit/base/constpool.h | 17 ++++++-------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/asmjit/base/constpool.cpp b/src/asmjit/base/constpool.cpp index 55ef26a..51ad806 100644 --- a/src/asmjit/base/constpool.cpp +++ b/src/asmjit/base/constpool.cpp @@ -24,22 +24,18 @@ namespace asmjit { // [asmjit::ConstPoolTree - Ops] // ============================================================================ -const ConstPoolNode ConstPoolTree::_sentinel = { { - const_cast(&ConstPoolTree::_sentinel), - const_cast(&ConstPoolTree::_sentinel) -}, 0, 0, 0 }; - //! \internal //! //! Remove left horizontal links. static ASMJIT_INLINE ConstPoolNode* ConstPoolTree_skewNode(ConstPoolNode* node) { - if (node->_link[0]->_level == node->_level && node->_level != 0) { - ConstPoolNode *save = node->_link[0]; + ConstPoolNode* link = node->_link[0]; + uint32_t level = node->_level; - node->_link[0] = save->_link[1]; - save->_link[1] = node; + if (level != 0 && link != NULL && link->_level == level) { + node->_link[0] = link->_link[1]; + link->_link[1] = node; - node = save; + node = link; } return node; @@ -49,13 +45,14 @@ static ASMJIT_INLINE ConstPoolNode* ConstPoolTree_skewNode(ConstPoolNode* node) //! //! Remove consecutive horizontal links. static ASMJIT_INLINE ConstPoolNode* ConstPoolTree_splitNode(ConstPoolNode* node) { - if (node->_link[1]->_link[1]->_level == node->_level && node->_level != 0) { - ConstPoolNode *save = node->_link[1]; + ConstPoolNode* link = node->_link[1]; + uint32_t level = node->_level; - node->_link[1] = save->_link[0]; - save->_link[0] = node; + if (level != 0 && link != NULL && link->_link[1] != NULL && link->_link[1]->_level == level) { + node->_link[1] = link->_link[0]; + link->_link[0] = node; - node = save; + node = link; node->_level++; } @@ -63,11 +60,10 @@ static ASMJIT_INLINE ConstPoolNode* ConstPoolTree_splitNode(ConstPoolNode* node) } ConstPoolNode* ConstPoolTree::get(const void* data) { - ConstPoolNode* sentinel = const_cast(&_sentinel); ConstPoolNode* node = _root; size_t dataSize = _dataSize; - while (node != sentinel) { + while (node != NULL) { int c = ::memcmp(node->getData(), data, dataSize); if (c == 0) return node; @@ -78,11 +74,10 @@ ConstPoolNode* ConstPoolTree::get(const void* data) { } void ConstPoolTree::put(ConstPoolNode* newNode) { - ConstPoolNode* sentinel = const_cast(&_sentinel); size_t dataSize = _dataSize; _length++; - if (_root == sentinel) { + if (_root == NULL) { _root = newNode; return; } @@ -97,9 +92,12 @@ void ConstPoolTree::put(ConstPoolNode* newNode) { for (;;) { stack[top++] = node; dir = ::memcmp(node->getData(), newNode->getData(), dataSize) < 0; - if (node->_link[dir] == sentinel) + + ConstPoolNode* link = node->_link[dir]; + if (link == NULL) break; - node = node->_link[dir]; + + node = link; } // Link and rebalance. @@ -109,8 +107,9 @@ void ConstPoolTree::put(ConstPoolNode* newNode) { // Which child? node = stack[--top]; - if (top != 0) + if (top != 0) { dir = stack[top - 1]->_link[1] == node; + } node = ConstPoolTree_skewNode(node); node = ConstPoolTree_splitNode(node); diff --git a/src/asmjit/base/constpool.h b/src/asmjit/base/constpool.h index 1d39039..c2c77c4 100644 --- a/src/asmjit/base/constpool.h +++ b/src/asmjit/base/constpool.h @@ -63,14 +63,12 @@ struct ConstPoolTree { kHeightLimit = 64 }; - ASMJIT_API static const ConstPoolNode _sentinel; - // -------------------------------------------------------------------------- // [Construction / Destruction] // -------------------------------------------------------------------------- ASMJIT_INLINE ConstPoolTree(size_t dataSize = 0) : - _root(const_cast(&_sentinel)), + _root(NULL), _length(0), _dataSize(dataSize) {} ASMJIT_INLINE ~ConstPoolTree() {} @@ -80,7 +78,7 @@ struct ConstPoolTree { // -------------------------------------------------------------------------- ASMJIT_INLINE void reset() { - _root = const_cast(&_sentinel); + _root = NULL; _length = 0; } @@ -118,9 +116,8 @@ struct ConstPoolTree { ConstPoolNode* link; ConstPoolNode* stack[kHeightLimit]; - ConstPoolNode* sentinel = const_cast(&_sentinel); - if (node == sentinel) + if (node == NULL) return; size_t top = 0; @@ -128,7 +125,7 @@ struct ConstPoolTree { for (;;) { link = node->_link[0]; - if (link != sentinel) { + if (link != NULL) { ASMJIT_ASSERT(top != kHeightLimit); stack[top++] = node; continue; @@ -137,7 +134,7 @@ struct ConstPoolTree { visitor.visit(node); link = node->_link[1]; - if (link != sentinel) { + if (link != NULL) { node = link; continue; } @@ -158,8 +155,8 @@ struct ConstPoolTree { if (node == NULL) return NULL; - node->_link[0] = const_cast(&_sentinel); - node->_link[1] = const_cast(&_sentinel); + node->_link[0] = NULL; + node->_link[1] = NULL; node->_level = 1; node->_shared = shared; node->_offset = static_cast(offset);