From 1fd86ffc2ed18671d54685b180ffc083b1c43bc9 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Fri, 3 Jun 2022 22:59:55 +0200 Subject: [PATCH] Update & fix --- m-tree.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/m-tree.h b/m-tree.h index 3f282eb0..1f93854f 100644 --- a/m-tree.h +++ b/m-tree.h @@ -242,7 +242,7 @@ typedef int32_t m_tr33_index_t; M_C(name, _reserve)(tree_t tree, size_t alloc) { \ M_TR33_CONTRACT(tree); \ /* Nothing to do if the request is lower than the current capacity. */ \ - if (alloc <= tree->capacity) { \ + if (alloc <= (size_t) tree->capacity) { \ return; \ } \ /* Realloc the array */ \ @@ -256,12 +256,20 @@ typedef int32_t m_tr33_index_t; M_MEMORY_FULL(sizeof (struct M_C(name, _node_s)) * alloc); \ return; \ } \ + /* Free the list */ \ + m_tr33_index_t *free_index = &tree->free_index; \ + if (*free_index != M_TR33_NO_NODE) { \ + while (ptr[*free_index].child != M_TR33_NO_NODE) { \ + free_index = &ptr[*free_index].child; \ + } \ + } \ + *free_index = tree->capacity; \ /* Construct the list of free node in the extra allocated pool */ \ - for(size_t i = tree->size ; i < alloc; i++) { \ + for(size_t i = tree->capacity ; i < alloc; i++) { \ ptr[i].parent = M_TR33_NO_NODE; \ ptr[i].left = M_TR33_NO_NODE; \ ptr[i].right = M_TR33_NO_NODE; \ - ptr[i].child = i + 1; \ + ptr[i].child = i+1; \ } \ /* The last node has no child in the free node list */ \ ptr[alloc-1].child = M_TR33_NO_NODE; \ @@ -283,7 +291,7 @@ typedef int32_t m_tr33_index_t; m_tr33_index_t ret = tree->free_index; \ if (M_UNLIKELY(ret < 0)) { \ /* No more enough space: realloc the array */ \ - size_t alloc = M_CALL_INC_ALLOC(oplist, tree->size); \ + size_t alloc = M_CALL_INC_ALLOC(oplist, tree->capacity); \ /* Take into account if realloc is allowed */ \ alloc <<= tree->allow_realloc; \ if (M_UNLIKELY (alloc >= INT32_MAX)) { \ @@ -297,7 +305,7 @@ typedef int32_t m_tr33_index_t; return M_TR33_NO_NODE; \ } \ /* Construct the list of free node in the extra allocated pool */ \ - for(size_t i = tree->size ; i < alloc; i++) { \ + for(size_t i = tree->capacity; i < alloc; i++) { \ ptr[i].parent = M_TR33_NO_NODE; \ ptr[i].left = M_TR33_NO_NODE; \ ptr[i].right = M_TR33_NO_NODE; \