Skip to content

Fixes #1: added custom allocator support and removed exit(1) on allocation error - #2

Open
RaphGL wants to merge 7 commits into
zfletch:masterfrom
RaphGL:master
Open

Fixes #1: added custom allocator support and removed exit(1) on allocation error#2
RaphGL wants to merge 7 commits into
zfletch:masterfrom
RaphGL:master

Conversation

@RaphGL

@RaphGL RaphGL commented Jun 28, 2026

Copy link
Copy Markdown

Fixes #1
I've updated the API to support adding custom allocators via a struct ZAllocator type. I've also ran the test script provided.
I've updated the examples and readme to reflect the changes and I also went in and added -std=c99 to gcc to ensure that no gcc extensions were being used.

@zfletch zfletch left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this PR! The underlying structure of the code looks good and I think we should be able to merge this with some minor modifications. I do have a few questions and some change requests.

Comment thread src/zhash.c
Comment thread src/zhash.c
Comment thread src/zhash.c
static size_t znext_size_index(size_t size_index);
static size_t zprevious_size_index(size_t size_index);
static struct ZHashTable *zcreate_hash_table_with_size(size_t size_index);
static void *zmalloc(size_t size);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for removing zmalloc instead of rewriting it to work with the allocator? I think we should treat zmalloc, zfree and zcalloc the same: either they should all be static functions or they should all be inline.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You had a zmalloc so that you could just ignore the return value. Since the return is propagated the zmalloc doesn't do anything it would just end up being this:

void *zmalloc(struct ZAllocator allocator, size_t size) {
    return allocator.alloc(size);
}

Or it would just return malloc(size). I removed all instances of allocator use to be based on the alloc and free functions. This means all your allocator has to support is alloc and free. You could technically also just give a no op function as free and then externally call myalloc_free_all(alloc) or something.

So I wrapped zcalloc so that we could still do zero initialized memory allocation without relying on any external API.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that zcalloc exists but zmalloc and zfree have been removed, which still works from a technical perspective, but is inconsistent.

How about adding zalloc and zfree as static functions that take struct ZAllocator allocator, like zcalloc?

@RaphGL RaphGL Jul 6, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding zcalloc, zmalloc and zfree wouldn't do anything for code clarity here and they would just be wrapping the equivalent functions in struct ZAllocator with no added benefit.

zcalloc was kept only because I did not want to write memset every time I called allocator.alloc. Personally I don't see a problem with it, but if you want to I could change it to a clearer name like zalloc_zeroed or something just to be clearer that it's just a helper function or I could remove the helper function altogether and call `memset every time I need it (zcalloc is only called twice in this codebase).

Either way, let me know what you sounds better to you and I'll make the changes.

Comment thread test/zhash_test.sh
Comment thread examples/hello.c
Comment thread README.md
Comment thread README.md
Comment thread src/zhash.c
Comment thread src/zhash.c
static size_t znext_size_index(size_t size_index);
static size_t zprevious_size_index(size_t size_index);
static struct ZHashTable *zcreate_hash_table_with_size(size_t size_index);
static void *zmalloc(size_t size);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that zcalloc exists but zmalloc and zfree have been removed, which still works from a technical perspective, but is inconsistent.

How about adding zalloc and zfree as static functions that take struct ZAllocator allocator, like zcalloc?

Comment thread test/zhash_test.sh
Comment thread src/zhash.c
@@ -2,21 +2,16 @@
#include <stdlib.h>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this now that we're no longer referring to malloc and free in this file?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ZDEFAULT_ALLOCATOR in the header still is defined as:

#define ZDEFAULT_ALLOCATOR ((struct ZAllocator) { .alloc = malloc, .free = free })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory allocation suggestions

2 participants