diff options
author | David Robillard <d@drobilla.net> | 2021-09-10 20:11:46 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-09-10 20:54:28 -0400 |
commit | 904c1b4d699aeb1ce170f0cd996a01d2d06812e3 (patch) | |
tree | 35a4a9f75a395a958ab0ea5fffeca81ce733bbc1 /src | |
parent | 1f8c8118f2d42f495dbe6e3adb2a95c87a92e8e0 (diff) | |
download | zix-904c1b4d699aeb1ce170f0cd996a01d2d06812e3.tar.gz zix-904c1b4d699aeb1ce170f0cd996a01d2d06812e3.tar.bz2 zix-904c1b4d699aeb1ce170f0cd996a01d2d06812e3.zip |
Add nullability annotations
This allows clang to issue warnings at compile time when null is passed to a
non-null parameter. For public entry points, also add assertions to catch such
issues when the compiler does not support this.
Diffstat (limited to 'src')
-rw-r--r-- | src/btree.c | 17 | ||||
-rw-r--r-- | src/hash.c | 35 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/btree.c b/src/btree.c index 4a8c4f6..7d967b8 100644 --- a/src/btree.c +++ b/src/btree.c @@ -97,6 +97,8 @@ zix_btree_child(const ZixBTreeNode* const node, const unsigned i) ZixBTree* zix_btree_new(const ZixComparator cmp, const void* const cmp_data) { + assert(cmp); + ZixBTree* const t = (ZixBTree*)malloc(sizeof(ZixBTree)); if (!t) { return NULL; @@ -168,6 +170,7 @@ zix_btree_clear(ZixBTree* const t, size_t zix_btree_size(const ZixBTree* const t) { + assert(t); return t->size; } @@ -422,6 +425,8 @@ zix_btree_grow_up(ZixBTree* const t) ZixStatus zix_btree_insert(ZixBTree* const t, void* const e) { + assert(t); + ZixStatus st = ZIX_STATUS_SUCCESS; // Grow up if necessary to ensure the root is not full @@ -723,6 +728,9 @@ zix_btree_remove(ZixBTree* const t, void** const out, ZixBTreeIter* const next) { + assert(t); + assert(out); + ZixBTreeNode* n = t->root; ZixBTreeIter* ti = next; ZixStatus st = ZIX_STATUS_SUCCESS; @@ -806,6 +814,9 @@ zix_btree_find(const ZixBTree* const t, const void* const e, ZixBTreeIter* const ti) { + assert(t); + assert(ti); + ZixBTreeNode* n = t->root; *ti = zix_btree_end_iter; @@ -842,6 +853,9 @@ zix_btree_lower_bound(const ZixBTree* const t, const void* const key, ZixBTreeIter* const ti) { + assert(t); + assert(ti); + *ti = zix_btree_end_iter; ZixBTreeNode* n = t->root; // Current node @@ -911,6 +925,8 @@ zix_btree_get(const ZixBTreeIter ti) ZixBTreeIter zix_btree_begin(const ZixBTree* const t) { + assert(t); + ZixBTreeIter iter = zix_btree_end_iter; if (t->size > 0u) { @@ -946,6 +962,7 @@ zix_btree_iter_equals(const ZixBTreeIter lhs, const ZixBTreeIter rhs) ZixStatus zix_btree_iter_increment(ZixBTreeIter* const i) { + assert(i); assert(!zix_btree_iter_is_end(*i)); // Move to the next value in the current node @@ -44,6 +44,10 @@ zix_hash_new(const ZixKeyFunc key_func, const ZixHashFunc hash_func, const ZixKeyEqualFunc equal_func) { + assert(key_func); + assert(hash_func); + assert(equal_func); + ZixHash* const hash = (ZixHash*)malloc(sizeof(ZixHash)); if (!hash) { return NULL; @@ -77,18 +81,21 @@ zix_hash_free(ZixHash* const hash) ZixHashIter zix_hash_begin(const ZixHash* const hash) { + assert(hash); return hash->entries[0u].value ? 0u : zix_hash_next(hash, 0u); } ZixHashIter zix_hash_end(const ZixHash* const hash) { + assert(hash); return hash->n_entries; } ZixHashRecord* zix_hash_get(const ZixHash* hash, const ZixHashIter i) { + assert(hash); assert(i < hash->n_entries); return hash->entries[i].value; @@ -97,6 +104,7 @@ zix_hash_get(const ZixHash* hash, const ZixHashIter i) ZixHashIter zix_hash_next(const ZixHash* const hash, ZixHashIter i) { + assert(hash); do { ++i; } while (i < hash->n_entries && !hash->entries[i].value); @@ -107,6 +115,7 @@ zix_hash_next(const ZixHash* const hash, ZixHashIter i) size_t zix_hash_size(const ZixHash* const hash) { + assert(hash); return hash->count; } @@ -215,6 +224,9 @@ shrink(ZixHash* const hash) ZixHashIter zix_hash_find(const ZixHash* const hash, const ZixHashKey* const key) { + assert(hash); + assert(key); + const ZixHashCode h_nomod = hash->hash_func(key); const size_t h = fold_hash(h_nomod, hash->mask); const ZixHashIter i = find_entry(hash, key, h, h_nomod); @@ -225,6 +237,9 @@ zix_hash_find(const ZixHash* const hash, const ZixHashKey* const key) ZixHashRecord* zix_hash_find_record(const ZixHash* const hash, const ZixHashKey* const key) { + assert(hash); + assert(key); + const ZixHashCode h_nomod = hash->hash_func(key); const size_t h = fold_hash(h_nomod, hash->mask); @@ -237,6 +252,9 @@ zix_hash_plan_insert_prehashed(const ZixHash* const hash, const ZixKeyMatchFunc predicate, const void* const user_data) { + assert(hash); + assert(predicate); + // Calculate an ideal initial position ZixHashInsertPlan pos = {code, fold_hash(code, hash->mask)}; @@ -269,6 +287,9 @@ zix_hash_plan_insert_prehashed(const ZixHash* const hash, ZixHashInsertPlan zix_hash_plan_insert(const ZixHash* const hash, const ZixHashKey* const key) { + assert(hash); + assert(key); + return zix_hash_plan_insert_prehashed( hash, hash->hash_func(key), hash->equal_func, key); } @@ -276,6 +297,7 @@ zix_hash_plan_insert(const ZixHash* const hash, const ZixHashKey* const key) ZixHashRecord* zix_hash_record_at(const ZixHash* const hash, const ZixHashInsertPlan position) { + assert(hash); return hash->entries[position.index].value; } @@ -284,6 +306,9 @@ zix_hash_insert_at(ZixHash* const hash, const ZixHashInsertPlan position, ZixHashRecord* const record) { + assert(hash); + assert(record); + if (hash->entries[position.index].value) { return ZIX_STATUS_EXISTS; } @@ -306,6 +331,9 @@ zix_hash_insert_at(ZixHash* const hash, ZixStatus zix_hash_insert(ZixHash* const hash, ZixHashRecord* const record) { + assert(hash); + assert(record); + const ZixHashKey* const key = hash->key_func(record); const ZixHashInsertPlan position = zix_hash_plan_insert(hash, key); @@ -317,6 +345,9 @@ zix_hash_erase(ZixHash* const hash, const ZixHashIter i, ZixHashRecord** const removed) { + assert(hash); + assert(removed); + // Replace entry with a tombstone *removed = hash->entries[i].value; hash->entries[i].hash = tombstone; @@ -336,6 +367,10 @@ zix_hash_remove(ZixHash* const hash, const ZixHashKey* const key, ZixHashRecord** const removed) { + assert(hash); + assert(key); + assert(removed); + const ZixHashIter i = zix_hash_find(hash, key); return i == hash->n_entries ? ZIX_STATUS_NOT_FOUND |