#Overriding C library’s allocations

1 messages · Page 1 of 1 (latest)

jade laurel
#

If I’m creating pure Zig bindings for a C library, and that library exposes preprocessor defines wrapping malloc, calloc, realloc, and free, how would I go about injecting Zig allocation functions?

spark charm
jade laurel
spark charm
#

Do they have a void* user data argument?

jade laurel
#

They’re defined like so

#ifndef RL_MALLOC
    #define RL_MALLOC(sz)       malloc(sz)
#endif
#ifndef RL_CALLOC
    #define RL_CALLOC(n,sz)     calloc(n,sz)
#endif
#ifndef RL_REALLOC
    #define RL_REALLOC(ptr,sz)  realloc(ptr,sz)
#endif
#ifndef RL_FREE
    #define RL_FREE(ptr)        free(ptr)
#endif

spark charm
jade laurel
spark charm
jade laurel
spark charm
somber raven
#

write a wrapper header

#

that takes care of the first two points

jade laurel
# somber raven write a wrapper header

I’m wondering if I need to create a separate static library in Zig land that exports some allocation functions. Then I would link that to the C library so it “sees” them

somber raven
#

sure, or you can just export them in your executable

jade laurel
somber raven
#

you'll still need the wrapper header either way tho, to declare the functions you're defining in zig, and to define the macros for configuring the library

#

tbh tho, it's normally better to do this integration the other way around - use std.heap.c_allocator in your Zig program, and let your C dependencies just call malloc like normal

jade laurel
somber raven
#

yea

#

some can do it

#

but not this one by the looks of it

#

and even with the ones that can, it's typically much less useful than in zig

jade laurel
#

I got inspired by the Zig SDL3 bindings

#

So in using std.heap.c_allocator there wouldn’t be any interaction between the C library’s allocations and the user of the Zig bindings using c_allocator?

#

I wouldn’t think there would be

somber raven
#

wdym by "interaction"?

jade laurel
somber raven
#

i mean they're both calling malloc

#

i don't really understand what you mean by "tying into the usage"

jade laurel
#

I’m not doing a good job articulating - I think I understand what you’re saying though

#

It’s just good practice when using a C library that uses malloc, to also use c_allocator in the calling Zig program

somber raven
#

yeah, reduces memory usage and heap fragmentation

#

because malloc can manage all the pages, rather than two different allocators managing their own pages without talking to each other

jade laurel
#

Even though they’re not actually talking to each other

#

As you can tell, I muddy the waters a lot 😁 thanks for the great explanation