From 19235b7127bcf5597fb0436deb45c2e6af5843c6 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 14 Sep 2021 17:19:12 -0400 Subject: Make ZixAllocator a single flat struct I can never decide between these two patterns for polymorphic objects in C, but this one seems more appropriate here since it's more conducive to inheritance. --- src/allocator.c | 29 ++++++++++++++--------------- src/btree.c | 26 +++++++++++++------------- src/hash.c | 24 ++++++++++++------------ src/ring.c | 14 +++++++------- src/tree.c | 28 ++++++++++++++-------------- 5 files changed, 60 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/allocator.c b/src/allocator.c index e4a9f83..c998463 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -7,43 +7,42 @@ ZIX_MALLOC_FUNC static void* -zix_default_malloc(ZixAllocatorHandle* const handle, const size_t size) +zix_default_malloc(ZixAllocator* const allocator, const size_t size) { - (void)handle; + (void)allocator; return malloc(size); } ZIX_MALLOC_FUNC static void* -zix_default_calloc(ZixAllocatorHandle* const handle, - const size_t nmemb, - const size_t size) +zix_default_calloc(ZixAllocator* const allocator, + const size_t nmemb, + const size_t size) { - (void)handle; + (void)allocator; return calloc(nmemb, size); } static void* -zix_default_realloc(ZixAllocatorHandle* const handle, - void* const ptr, - const size_t size) +zix_default_realloc(ZixAllocator* const allocator, + void* const ptr, + const size_t size) { - (void)handle; + (void)allocator; return realloc(ptr, size); } static void -zix_default_free(ZixAllocatorHandle* const handle, void* const ptr) +zix_default_free(ZixAllocator* const allocator, void* const ptr) { - (void)handle; + (void)allocator; free(ptr); } -const ZixAllocator* +ZixAllocator* zix_default_allocator(void) { - static const ZixAllocator default_allocator = { - NULL, + static ZixAllocator default_allocator = { zix_default_malloc, zix_default_calloc, zix_default_realloc, diff --git a/src/btree.c b/src/btree.c index a73a2d0..869b98a 100644 --- a/src/btree.c +++ b/src/btree.c @@ -25,11 +25,11 @@ typedef uint16_t ZixShort; #define ZIX_BTREE_INODE_VALS (ZIX_BTREE_LEAF_VALS / 2u) struct ZixBTreeImpl { - const ZixAllocator* allocator; - ZixBTreeNode* root; - ZixComparator cmp; - const void* cmp_data; - size_t size; + ZixAllocator* allocator; + ZixBTreeNode* root; + ZixComparator cmp; + const void* cmp_data; + size_t size; }; struct ZixBTreeNodeImpl { @@ -57,7 +57,7 @@ static_assert(sizeof(ZixBTreeNode) >= #endif static ZixBTreeNode* -zix_btree_node_new(const ZixAllocator* const allocator, const bool leaf) +zix_btree_node_new(ZixAllocator* const allocator, const bool leaf) { #if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112l) || \ (defined(__cplusplus) && __cplusplus >= 201103L)) @@ -87,9 +87,9 @@ zix_btree_child(const ZixBTreeNode* const node, const unsigned i) } ZixBTree* -zix_btree_new(const ZixAllocator* const allocator, - const ZixComparator cmp, - const void* const cmp_data) +zix_btree_new(ZixAllocator* const allocator, + const ZixComparator cmp, + const void* const cmp_data) { assert(cmp); @@ -203,10 +203,10 @@ zix_btree_aerase(void** const array, const unsigned n, const unsigned i) /// Split lhs, the i'th child of `n`, into two nodes static ZixBTreeNode* -zix_btree_split_child(const ZixAllocator* const allocator, - ZixBTreeNode* const n, - const unsigned i, - ZixBTreeNode* const lhs) +zix_btree_split_child(ZixAllocator* const allocator, + ZixBTreeNode* const n, + const unsigned i, + ZixBTreeNode* const lhs) { assert(lhs->n_vals == zix_btree_max_vals(lhs)); assert(n->n_vals < ZIX_BTREE_INODE_VALS); diff --git a/src/hash.c b/src/hash.c index 65f9570..fa920b0 100644 --- a/src/hash.c +++ b/src/hash.c @@ -13,24 +13,24 @@ typedef struct ZixHashEntry { } ZixHashEntry; struct ZixHashImpl { - const ZixAllocator* allocator; ///< User allocator - ZixKeyFunc key_func; ///< User key accessor - ZixHashFunc hash_func; ///< User hashing function - ZixKeyEqualFunc equal_func; ///< User equality comparison function - size_t count; ///< Number of records stored in the table - size_t mask; ///< Bit mask for fast modulo (n_entries - 1) - size_t n_entries; ///< Power of two table size - ZixHashEntry* entries; ///< Pointer to dynamically allocated table + ZixAllocator* allocator; ///< User allocator + ZixKeyFunc key_func; ///< User key accessor + ZixHashFunc hash_func; ///< User hashing function + ZixKeyEqualFunc equal_func; ///< User equality comparison function + size_t count; ///< Number of records stored in the table + size_t mask; ///< Bit mask for fast modulo (n_entries - 1) + size_t n_entries; ///< Power of two table size + ZixHashEntry* entries; ///< Pointer to dynamically allocated table }; static const size_t min_n_entries = 4u; static const size_t tombstone = 0xDEADu; ZixHash* -zix_hash_new(const ZixAllocator* const allocator, - const ZixKeyFunc key_func, - const ZixHashFunc hash_func, - const ZixKeyEqualFunc equal_func) +zix_hash_new(ZixAllocator* const allocator, + const ZixKeyFunc key_func, + const ZixHashFunc hash_func, + const ZixKeyEqualFunc equal_func) { assert(key_func); assert(hash_func); diff --git a/src/ring.c b/src/ring.c index 14a55e3..a464428 100644 --- a/src/ring.c +++ b/src/ring.c @@ -31,12 +31,12 @@ #endif struct ZixRingImpl { - const ZixAllocator* allocator; ///< User allocator - uint32_t write_head; ///< Read index into buf - uint32_t read_head; ///< Write index into buf - uint32_t size; ///< Size (capacity) in bytes - uint32_t size_mask; ///< Mask for fast modulo - char* buf; ///< Contents + ZixAllocator* allocator; ///< User allocator + uint32_t write_head; ///< Read index into buf + uint32_t read_head; ///< Write index into buf + uint32_t size; ///< Size (capacity) in bytes + uint32_t size_mask; ///< Mask for fast modulo + char* buf; ///< Contents }; static inline uint32_t @@ -54,7 +54,7 @@ next_power_of_two(uint32_t size) } ZixRing* -zix_ring_new(const ZixAllocator* const allocator, uint32_t size) +zix_ring_new(ZixAllocator* const allocator, uint32_t size) { ZixRing* ring = (ZixRing*)zix_malloc(allocator, sizeof(ZixRing)); diff --git a/src/tree.c b/src/tree.c index ccf60a6..9fa32c4 100644 --- a/src/tree.c +++ b/src/tree.c @@ -11,14 +11,14 @@ typedef struct ZixTreeNodeImpl ZixTreeNode; struct ZixTreeImpl { - const ZixAllocator* allocator; - ZixTreeNode* root; - ZixDestroyFunc destroy; - const void* destroy_user_data; - ZixComparator cmp; - void* cmp_data; - size_t size; - bool allow_duplicates; + ZixAllocator* allocator; + ZixTreeNode* root; + ZixDestroyFunc destroy; + const void* destroy_user_data; + ZixComparator cmp; + void* cmp_data; + size_t size; + bool allow_duplicates; }; struct ZixTreeNodeImpl { @@ -54,12 +54,12 @@ struct ZixTreeNodeImpl { #endif ZixTree* -zix_tree_new(const ZixAllocator* const allocator, - bool allow_duplicates, - ZixComparator cmp, - void* cmp_data, - ZixDestroyFunc destroy, - const void* destroy_user_data) +zix_tree_new(ZixAllocator* const allocator, + bool allow_duplicates, + ZixComparator cmp, + void* cmp_data, + ZixDestroyFunc destroy, + const void* destroy_user_data) { ZixTree* t = (ZixTree*)zix_malloc(allocator, sizeof(ZixTree)); -- cgit v1.2.1