From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Vladislav Shpilevoy Subject: [PATCH 3/3] salad: fix heap reserve() behaviour Date: Fri, 22 Feb 2019 14:39:00 +0300 Message-Id: In-Reply-To: References: In-Reply-To: References: To: tarantool-patches@freelists.org Cc: vdavydov.dev@gmail.com List-ID: Reserve() function by definition should ensure that there is enough space to store something. But heap reserve() always just increases the capacity twice. Even if there was already enough memory. Check for not necessary realloc was done out of this function. This was wrong. The commit makes reserve() be real reserve(). The commit is motivated not by pursue of justice, but by forthcoming usage of reserve() in SWIM module code. --- src/lib/salad/heap.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/salad/heap.h b/src/lib/salad/heap.h index f267de3f1..31750216a 100644 --- a/src/lib/salad/heap.h +++ b/src/lib/salad/heap.h @@ -382,6 +382,8 @@ HEAP(sift_down)(heap_t *heap, struct heap_node *node) static inline int HEAP(reserve)(heap_t *heap) { + if (heap->capacity > heap->size) + return 0; heap_off_t capacity = heap->capacity == 0 ? HEAP_INITIAL_CAPACITY : heap->capacity << 1; void *harr = realloc(heap->harr, sizeof(struct heap_node *) * capacity); @@ -398,10 +400,8 @@ HEAP(reserve)(heap_t *heap) static inline int HEAP(insert)(heap_t *heap, heap_value_t *value) { - if (heap->size + 1 > heap->capacity) { - if (HEAP(reserve)(heap)) - return -1; - } + if (HEAP(reserve)(heap)) + return -1; struct heap_node *node = value_to_node(value); heap->harr[heap->size] = node; HEAP(update_link)(heap, heap->size++); -- 2.17.2 (Apple Git-113)