Skip to content

Commit 67ac091

Browse files
committed
Removed never used MallocAllocator and use common FB_NEW_POOL and delete instead of allocate/deallocate.
Removed excess "friend class" declarations. NodePtr changed to avoid potential undefined behavior.
1 parent 6079111 commit 67ac091

File tree

1 file changed

+22
-50
lines changed

1 file changed

+22
-50
lines changed

src/common/classes/tree.h

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,6 @@ const int NODE_PAGE_SIZE = 3000;
6464
// should be more than enough. No checks are performed in code against overflow of this value
6565
const int MAX_TREE_LEVEL = 30;
6666

67-
class MallocAllocator
68-
{
69-
public:
70-
void *allocate(size_t size ALLOC_PARAMS)
71-
{
72-
return malloc(size);
73-
}
74-
void deallocate(void *p)
75-
{
76-
free(p);
77-
}
78-
};
79-
8067
enum LocType { locEqual, locLess, locGreat, locGreatEqual, locLessEqual };
8168

8269
// Fast and simple B+ tree of simple types.
@@ -106,7 +93,7 @@ enum LocType { locEqual, locLess, locGreat, locGreatEqual, locLessEqual };
10693
// an indexed dynamic array without increase of algorithm calculation costs (this is one
10794
// more classical B+ tree feature). This is also not done to improve tree performance a little
10895
//
109-
template <typename Value, typename Key = Value, typename Allocator = MallocAllocator,
96+
template <typename Value, typename Key = Value, typename Allocator = Firebird::MemoryPool,
11097
typename KeyOfValue = DefaultKeyValue<Value>,
11198
typename Cmp = DefaultComparator<Key> >
11299
class BePlusTree
@@ -115,15 +102,15 @@ class BePlusTree
115102
static const FB_SIZE_T NODE_COUNT = NODE_PAGE_SIZE / sizeof(void*);
116103
public:
117104
explicit BePlusTree(Allocator *_pool)
118-
: pool(_pool), level(0), root(nullptr), defaultAccessor(this)
105+
: pool(_pool), level(0), defaultAccessor(this)
119106
{ }
120107

121108
explicit BePlusTree(Allocator& _pool)
122-
: pool(&_pool), level(0), root(nullptr), defaultAccessor(this)
109+
: pool(&_pool), level(0), defaultAccessor(this)
123110
{ }
124111

125112
BePlusTree(Allocator *_pool, const BePlusTree& from)
126-
: pool(_pool), level(0), root(nullptr), defaultAccessor(this)
113+
: pool(_pool), level(0), defaultAccessor(this)
127114
{
128115
append(from);
129116
}
@@ -159,8 +146,7 @@ class BePlusTree
159146
while (items)
160147
{
161148
ItemList *t = items->next;
162-
items->~ItemList();
163-
pool->deallocate(items);
149+
delete items;
164150
items = t;
165151
}
166152

@@ -172,21 +158,20 @@ class BePlusTree
172158
while (list)
173159
{
174160
NodeList *t = list->next;
175-
list->~NodeList();
176-
pool->deallocate(list);
161+
delete list;
177162
list = t;
178163
}
179164
}
180165

181166
// Initialize fields to make tree usable again
182-
root = nullptr;
167+
root.items = nullptr;
183168
level = 0;
184169
}
185170

186171
~BePlusTree()
187172
{
188173
clear();
189-
pool->deallocate(root.pointer);
174+
delete root.items;
190175
}
191176

192177
bool isEmpty() const
@@ -321,25 +306,18 @@ class BePlusTree
321306
}
322307
// Create first item in the linked list
323308
ItemList() : parent(nullptr), next(nullptr), prev(nullptr) {}
324-
325-
friend class BePlusTree;
326-
#ifndef _MSC_VER
327-
friend class BePlusTree::NodeList;
328-
friend class BePlusTree::Accessor;
329-
#endif
330309
};
331310

332311
union NodePtr
333312
{
334-
NodePtr(void* p = nullptr) : pointer(p) {}
313+
NodePtr() : items(nullptr) {}
314+
NodePtr(ItemList* p) : items(p) {}
315+
NodePtr(NodeList* p) : nodes(p) {}
335316

336-
operator bool() const { return pointer != nullptr; }
337-
338-
bool operator ==(const void* ptr) const { return pointer == ptr; }
317+
operator bool() const { return items != nullptr; }
339318

340319
ItemList* items;
341320
NodeList* nodes;
342-
void* pointer;
343321
};
344322

345323
class NodeList : public SortedVector<NodePtr, NODE_COUNT, Key, NodeList, Cmp>
@@ -660,8 +638,6 @@ class BePlusTree
660638

661639
private:
662640
BePlusTree* tree;
663-
664-
friend class BePlusTree;
665641
}; // class Accessor
666642

667643
private:
@@ -684,7 +660,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
684660
{
685661
// Finish initialization of the tree if necessary
686662
if (!root)
687-
root = new (pool->allocate(sizeof(ItemList) ALLOC_ARGS)) ItemList();
663+
root = FB_NEW_POOL(*pool) ItemList();
688664

689665
// Find leaf page for our item
690666
NodePtr vList = this->root;
@@ -766,7 +742,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
766742
// No re-enterance allowed !!!
767743
// Since we haven't done anything with tree yet, thus we don't need to recover
768744
// anything in case of error thrown at this allocation here
769-
ItemList *newLeaf = new(this->pool->allocate(sizeof(ItemList) ALLOC_ARGS)) ItemList(leaf);
745+
ItemList *newLeaf = FB_NEW_POOL(*pool) ItemList(leaf);
770746

771747
// Start building recovery map.
772748
// This array contains index of the element we try to add on page of each level
@@ -851,7 +827,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
851827
// No re-enterance allowed !!!
852828
// Exceptions from this point
853829
// are cleaned up lower
854-
NodeList *newList = new(this->pool->allocate(sizeof(NodeList) ALLOC_ARGS)) NodeList(nodeList);
830+
NodeList *newList = FB_NEW_POOL(*pool) NodeList(nodeList);
855831

856832
if (pos == NODE_COUNT)
857833
{
@@ -876,7 +852,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
876852

877853
// This is the worst case. We reached the top of tree but were not able to insert node
878854
// Allocate new root page and increase level of our tree
879-
nodeList = new(this->pool->allocate(sizeof(NodeList) ALLOC_ARGS)) NodeList();
855+
nodeList = FB_NEW_POOL(*pool) NodeList();
880856
nodeList->level = this->level;
881857
nodeList->insert(0, this->root);
882858
NodeList::setNodeParentAndLevel(newNode, this->level, nodeList);
@@ -902,8 +878,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
902878
itemL->prev->insert(itemL->prev->getCount(), (*itemL)[0]);
903879
NodeList::setNodeParent((*itemL)[0], curLevel - 1, itemL->prev);
904880
}
905-
itemL->~NodeList();
906-
this->pool->deallocate(newNode.pointer);
881+
delete itemL;
907882
newNode = lower;
908883
curLevel--;
909884
}
@@ -913,8 +888,7 @@ bool BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::add(const Value& item,
913888
itemL2->prev->remove(recovery_map[0]);
914889
itemL2->prev->insert(itemL2->prev->getCount(), (*itemL2)[0]);
915890
}
916-
itemL2->~ItemList();
917-
this->pool->deallocate(newNode.pointer);
891+
delete itemL2;
918892
throw;
919893
}
920894
return true;
@@ -985,16 +959,15 @@ void BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::_removePage(const int n
985959
#endif
986960
list->remove(pos);
987961

988-
if (root == list && list->getCount() == 1)
962+
if (root.nodes == list && list->getCount() == 1)
989963
{
990964
// We reached the top of the tree and were asked to modify root
991965
// page so only one node will be left in this case.
992966
// Reduce the level of the tree
993967
root = (*list)[0];
994968
level--;
995969
NodeList::setNodeParent(root, level, nullptr);
996-
list->~NodeList();
997-
pool->deallocate(list);
970+
delete list;
998971
}
999972
else
1000973
{
@@ -1020,10 +993,9 @@ void BePlusTree<Value, Key, Allocator, KeyOfValue, Cmp>::_removePage(const int n
1020993
}
1021994

1022995
if (nodeLevel)
1023-
node.nodes->~NodeList();
996+
delete node.nodes;
1024997
else
1025-
node.items->~ItemList();
1026-
pool->deallocate(node.pointer);
998+
delete node.items;
1027999
}
10281000

10291001
} // namespace Firebird

0 commit comments

Comments
 (0)