diff --git a/expression_node.h b/expression_node.h index 35826ec1b..61e4d2ea0 100644 --- a/expression_node.h +++ b/expression_node.h @@ -22,11 +22,6 @@ public: Pool()->dealloc(ptr); } - static TreePool * Pool() { - static TreePool pool; - return &pool; - } - virtual float approximate() = 0; int numberOfOperands() { return numberOfChildren(); } ExpressionNode * operand(int i) { return static_cast(childAtIndex(i)); } diff --git a/tree_node.cpp b/tree_node.cpp index edbd382b2..3b1c4018e 100644 --- a/tree_node.cpp +++ b/tree_node.cpp @@ -1,6 +1,12 @@ #include "tree_node.h" +#include "tree_pool.h" #include "expression_node.h" +TreePool * TreeNode::Pool() { + static TreePool pool; + return &pool; +} + void TreeNode::release() { printf("Releasing of %d(%p)\n", m_identifier, this); m_referenceCounter--; @@ -19,8 +25,9 @@ void TreeNode::release() { } } while (child->identifier() != lastIdentifier); } - printf("DELETE %d(%p)\n", m_identifier, this); + int identifier = m_identifier; delete this; + Pool()->freeIdentifier(identifier); } } diff --git a/tree_node.h b/tree_node.h index de27920e4..c41549453 100644 --- a/tree_node.h +++ b/tree_node.h @@ -15,6 +15,8 @@ * - a reference counter */ +class TreePool; + class TreeNode { //friend class TreeReference; // friend class TreePool; @@ -22,6 +24,9 @@ public: virtual ~TreeNode() { } + // Pool + static TreePool * Pool(); + // Iterators class Iterator { diff --git a/tree_pool.cpp b/tree_pool.cpp index d2443e1e9..878c4345b 100644 --- a/tree_pool.cpp +++ b/tree_pool.cpp @@ -1,5 +1,4 @@ #include "tree_pool.h" -#include "tree_node.h" #include void * TreePool::alloc(size_t size) { @@ -27,8 +26,8 @@ void TreePool::dealloc(void * ptr) { // Step 2 - Update m_nodeForIdentifier for (int i = 0; i < MaxNumberOfNodes; i++) { - if (m_nodeForIdentifier[i] > node) { - m_nodeForIdentifier[i] -= size; + if (m_nodeForIdentifier[i] != nullptr && m_nodeForIdentifier[i] > node) { + m_nodeForIdentifier[i] = reinterpret_cast(reinterpret_cast(m_nodeForIdentifier[i]) - size); } } } @@ -53,7 +52,7 @@ static void memmove32(uint32_t * dst, uint32_t * src, size_t len) { } void TreePool::insert(char * destination, char * source, size_t length) { - if (source == destination || destination < source + length) { + if (source == destination || (destination > source && destination < source + length)) { return; } @@ -76,31 +75,50 @@ void TreePool::insert(char * destination, char * source, size_t length) { } } +void TreePool::logNodeForIdentifierArray() { + printf("\n\n"); + for (int i = 0; i < MaxNumberOfNodes; i++) { + if (m_nodeForIdentifier[i] != nullptr) { + printf("Identifier %d, node %p\n", i, m_nodeForIdentifier[i]); + } + } + printf("\n\n"); +} + void TreePool::move(TreeNode * source, TreeNode * destination) { if (source == destination) { return; } + + log(); + // Move the Node size_t srcDeepSize = source->deepSize(); - insert(reinterpret_cast(destination), reinterpret_cast(source), srcDeepSize); + printf("SourceDeepSize %zu\n", srcDeepSize); + char * destinationAddress = reinterpret_cast(destination); + char * sourceAddress = reinterpret_cast(source); + insert(destinationAddress, sourceAddress, srcDeepSize); // Update the nodeForIdentifier array for (int i = 0; i < MaxNumberOfNodes; i++) { - void * nodeAddress = m_nodeForIdentifier[i]; + char * nodeAddress = reinterpret_cast(m_nodeForIdentifier[i]); if (nodeAddress == nullptr) { continue; - } else if (nodeAddress >= source && nodeAddress < source + srcDeepSize) { - if (destination < source) { - m_nodeForIdentifier[i] -= (source - destination); + } else if (nodeAddress >= sourceAddress && nodeAddress < sourceAddress + srcDeepSize) { + printf("Source %p, dest %p, currentNode identifier %d, pointer %p\n", sourceAddress, destinationAddress, i, m_nodeForIdentifier[i]); + if (destinationAddress < sourceAddress) { + printf("former pointer %p, pointer difference %ld", m_nodeForIdentifier[i], sourceAddress - destinationAddress); + m_nodeForIdentifier[i] = reinterpret_cast(nodeAddress - (sourceAddress - destinationAddress)); } else { - m_nodeForIdentifier[i] += destination - (source + srcDeepSize); + m_nodeForIdentifier[i] = reinterpret_cast(nodeAddress + (destinationAddress - (sourceAddress + srcDeepSize))); } - } else if (nodeAddress > source && nodeAddress < destination) { - m_nodeForIdentifier[i] -= srcDeepSize; - } else if (nodeAddress < source && nodeAddress > destination) { - m_nodeForIdentifier[i] += srcDeepSize; + } else if (nodeAddress > sourceAddress && nodeAddress <= destinationAddress) { + m_nodeForIdentifier[i] = reinterpret_cast(nodeAddress - srcDeepSize); + } else if (nodeAddress < sourceAddress && nodeAddress >= destinationAddress) { + m_nodeForIdentifier[i] = reinterpret_cast(nodeAddress + srcDeepSize); } } + log(); } #if TREE_LOGGING @@ -112,6 +130,8 @@ void TreePool::log() { printf("|(%03d|%s|%03d|%p)", node->m_identifier, node->description(), node->retainCount(), node); } printf("|\n"); + + logNodeForIdentifierArray(); } #endif diff --git a/tree_pool.h b/tree_pool.h index 9aa8edf1e..9f0b4ffaa 100644 --- a/tree_pool.h +++ b/tree_pool.h @@ -2,7 +2,6 @@ #define TREE_POOL_H #include - #include "tree_node.h" class TreePool { @@ -19,11 +18,13 @@ public: } } printf("Generating identifier %d\n", newIdentifier); + assert(newIdentifier != -1); // TODO error handling return newIdentifier; } void freeIdentifier(int identifier) { assert(identifier >= 0 && identifier < MaxNumberOfNodes); + printf("DELETE IDENTIFIER %d\n", identifier); m_nodeForIdentifier[identifier] = nullptr; } @@ -48,9 +49,12 @@ public: TreeNode * first() const { return reinterpret_cast(const_cast(m_buffer)); } TreeNode * last() const { return reinterpret_cast(const_cast(m_cursor)); } + void logNodeForIdentifierArray(); + void move(TreeNode * source, TreeNode * destination); void registerNode(TreeNode * node) { + printf("Registering identifier %d with node %p\n", node->identifier(), node); m_nodeForIdentifier[node->identifier()] = node; } diff --git a/tree_reference.h b/tree_reference.h index 7bedd292a..079a60f9c 100644 --- a/tree_reference.h +++ b/tree_reference.h @@ -19,6 +19,9 @@ public: ~TreeReference() { printf("TreeRef destroy of %d\n", m_identifier); + assert(node()); + printf("TreeRef's node %p\n", node()); + printf("TreeRef's node identifier %d and node identifier %d\n", node()->m_identifier, m_identifier); assert(node()->m_identifier == m_identifier); node()->release(); }