From 5cbf227236afa227e172a3ea7d5127a9ab0770dc Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Sun, 20 Oct 2013 23:33:55 +0200 Subject: [PATCH] kernel/util: Allocate only one array in MinMaxHeap --- headers/private/kernel/util/MinMaxHeap.h | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/headers/private/kernel/util/MinMaxHeap.h b/headers/private/kernel/util/MinMaxHeap.h index 0727a85cd0..01a48d1be9 100644 --- a/headers/private/kernel/util/MinMaxHeap.h +++ b/headers/private/kernel/util/MinMaxHeap.h @@ -84,9 +84,8 @@ public: inline status_t Insert(Element* element, Key key); + status_t GrowHeap(int minimalSize = 0); private: - status_t _GrowHeap(); - void _MoveUp(MinMaxHeapLink* link); void _MoveDown(MinMaxHeapLink* link); bool _ChangeTree(MinMaxHeapLink* link); @@ -171,7 +170,6 @@ MIN_MAX_HEAP_TEMPLATE_LIST MIN_MAX_HEAP_CLASS_NAME::~MinMaxHeap() { free(fMinElements); - free(fMaxElements); } @@ -273,10 +271,9 @@ MIN_MAX_HEAP_TEMPLATE_LIST status_t MIN_MAX_HEAP_CLASS_NAME::Insert(Element* element, Key key) { - if (min_c(fMinLastElement, fMaxLastElement) == fSize) { ASSERT(max_c(fMinLastElement, fMaxLastElement) == fSize); - status_t result = _GrowHeap(); + status_t result = GrowHeap(); if (result != B_OK) return result; } @@ -303,9 +300,10 @@ MIN_MAX_HEAP_CLASS_NAME::Insert(Element* element, Key key) MIN_MAX_HEAP_TEMPLATE_LIST status_t -MIN_MAX_HEAP_CLASS_NAME::_GrowHeap() +MIN_MAX_HEAP_CLASS_NAME::GrowHeap(int minimalSize) { - int newSize = max_c(fSize * 2, 4); + minimalSize = minimalSize % 2 ? minimalSize : minimalSize + 1; + int newSize = max_c(max_c(fSize * 4, 4), minimalSize); size_t arraySize = newSize * sizeof(Element*); Element** newBuffer @@ -313,14 +311,9 @@ MIN_MAX_HEAP_CLASS_NAME::_GrowHeap() if (newBuffer == NULL) return B_NO_MEMORY; fMinElements = newBuffer; + fMaxElements = newBuffer + (newSize / 2); - newBuffer - = reinterpret_cast(realloc(fMaxElements, arraySize)); - if (newBuffer == NULL) - return B_NO_MEMORY; - fMaxElements = newBuffer; - - fSize = newSize; + fSize = newSize / 2; return B_OK; }