Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #ifdef so that using custom malloc() is streamlined #39

Open
dtromb opened this issue Jan 19, 2024 · 1 comment
Open

Add #ifdef so that using custom malloc() is streamlined #39

dtromb opened this issue Jan 19, 2024 · 1 comment

Comments

@dtromb
Copy link

dtromb commented Jan 19, 2024

Right now the source must by modified to use a custom malloc. From dict.c:

void* (*dict_malloc_func)(size_t) = malloc;
void (*dict_free_func)(void*) = free;

These functions cannot be changed after compilation, so we must edit dict.c to customize, adding CM complexity.
I propose the simple change:

#ifndef LIBDICT_USE_CUSTOM_MALLOC
void* (*dict_malloc_func)(size_t) = malloc;
void (*dict_free_func)(void*) = free;
#endif

In this way, the object can be compiled without those symbols defined, so that they can be linked in later.
The Makefile must also be changed to support this somehow, to pass the define through on CFLAGS, and also so that tests / pothe binaries built be the Makefile will either get linked with a version of dict.o with those symbols, or their own copies of the symbol definitions if the custom malloc is requested.

@sorc1
Copy link
Contributor

sorc1 commented Jan 19, 2024

dict_malloc_func and dict_free_func are global modifyable variables. You may change them in main(), for example:

int main(void) {
    dict_malloc_func = my_malloc;
    dict_free_func = my_free;
    do_something_with_libdict();
    return 0;
}

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

No branches or pull requests

2 participants