diff options
author | David Robillard <d@drobilla.net> | 2022-08-18 16:44:16 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-08-18 16:44:16 -0400 |
commit | f16da4ef66665cff0f10bdf04b0437cf5e397522 (patch) | |
tree | af9aea8d62355165861d0b37b04554930922286f | |
parent | 13b8d53bdc47ee7e8ba6f9bcf4d787901599b5c0 (diff) | |
download | zix-f16da4ef66665cff0f10bdf04b0437cf5e397522.tar.gz zix-f16da4ef66665cff0f10bdf04b0437cf5e397522.tar.bz2 zix-f16da4ef66665cff0f10bdf04b0437cf5e397522.zip |
Reduce variable scope and mutability
-rw-r--r-- | src/tree.c | 13 |
1 files changed, 6 insertions, 7 deletions
@@ -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; } |