r/C_Programming • u/ankush2324235 • Jul 28 '26
Review circular buffer in c
Hi guy I wrote a fixed size circular buffer in C. Please tell me what you think of this and please tell me what i can improve and make it more production grade. I know there may be memory leaks !!!
One thing thats a bit different from the usual approach is how I handle errors. Instead of returning NULL from cirbuf_create(), the library returns a pointer to a thread-local error object (e_buffer). This lets the API return a valid cirbuf * in both success and failure cases, and users can check the result with cirbuf_is_ok() or cirbuf_is_err().
Its not written by AI. like AI reviewed it and did some minor changes may be !! 98% is written by me !!! I think HUMAN check is needed here thats why I am here to you guys!!
Repo: https://github.com/ankushT369/cirbuf
If you like you can give a star (its you choice)
Thank you guys
View all comments
Show parent comment
3
u/sidewaysEntangled Jul 28 '26
To be fair, it can be useful when the API is to put and get arbitrary sizes things (byte strings) rather than slots of known sized objects.
Say you want to push a 100byte object, and your buffer at some point in time has 120b available , but it's 60b at the end, then wrap and 60 more.
The choices are:
It's maybe a bit niche, but good to know it exists; tools in the toolbox so to speak.. I think Casey Muratori presented it once somewhere so I wouldn't be surprised if it crops up in certain Handmade circles.