Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: vdavydov.dev@gmail.com
Subject: [PATCH 3/3] salad: fix heap reserve() behaviour
Date: Fri, 22 Feb 2019 14:39:00 +0300	[thread overview]
Message-ID: <ab171778ca8a6c4eba05446a29d671a1f7f122fd.1550835301.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1550835301.git.v.shpilevoy@tarantool.org>
In-Reply-To: <cover.1550835301.git.v.shpilevoy@tarantool.org>

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)

  parent reply	other threads:[~2019-02-22 11:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-22 11:38 [PATCH 0/3] Make heap API more friendly Vladislav Shpilevoy
2019-02-22 11:38 ` [PATCH 1/3] salad: make heap struct more friendly to use Vladislav Shpilevoy
2019-02-22 18:26   ` [tarantool-patches] " Konstantin Osipov
2019-02-22 18:38     ` [tarantool-patches] " Vladislav Shpilevoy
2019-02-22 11:38 ` [PATCH 2/3] salad: do not touch struct heap_node.pos in user's code Vladislav Shpilevoy
2019-02-25 12:46   ` Vladimir Davydov
2019-02-22 11:39 ` Vladislav Shpilevoy [this message]
2019-02-22 18:31   ` [tarantool-patches] [PATCH 3/3] salad: fix heap reserve() behaviour Konstantin Osipov
2019-02-22 18:38     ` [tarantool-patches] " Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ab171778ca8a6c4eba05446a29d671a1f7f122fd.1550835301.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH 3/3] salad: fix heap reserve() behaviour' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox