summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-08-18 16:44:16 -0400
committerDavid Robillard <d@drobilla.net>2022-08-18 16:44:16 -0400
commitf16da4ef66665cff0f10bdf04b0437cf5e397522 (patch)
treeaf9aea8d62355165861d0b37b04554930922286f
parent13b8d53bdc47ee7e8ba6f9bcf4d787901599b5c0 (diff)
downloadzix-f16da4ef66665cff0f10bdf04b0437cf5e397522.tar.gz
zix-f16da4ef66665cff0f10bdf04b0437cf5e397522.tar.bz2
zix-f16da4ef66665cff0f10bdf04b0437cf5e397522.zip
Reduce variable scope and mutability
-rw-r--r--src/tree.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/tree.c b/src/tree.c
index 0af7b4e..f745fa6 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -6,7 +6,6 @@
#include "zix/common.h"
#include <assert.h>
-#include <string.h>
typedef struct ZixTreeNodeImpl ZixTreeNode;
@@ -324,11 +323,10 @@ ZixStatus
zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti)
{
int cmp = 0;
- ZixTreeNode* n = t->root;
ZixTreeNode* p = NULL;
// Find the parent p of e
- while (n) {
+ for (ZixTreeNode* n = t->root; n;) {
p = n;
cmp = t->cmp(e, n->data, t->cmp_data);
if (cmp < 0) {
@@ -344,12 +342,13 @@ zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti)
}
// Allocate a new node n
- if (!(n = (ZixTreeNode*)zix_malloc(t->allocator, sizeof(ZixTreeNode)))) {
+ ZixTreeNode* const n =
+ (ZixTreeNode*)zix_calloc(t->allocator, 1, sizeof(ZixTreeNode));
+ if (!n) {
return ZIX_STATUS_NO_MEM;
}
- memset(n, '\0', sizeof(ZixTreeNode));
- n->data = e;
- n->balance = 0;
+
+ n->data = e;
if (ti) {
*ti = n;
}