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
•
u/github-guard Jul 28 '26
🔍 GitHub Guard: Trust Report
⚠️ This project scored 1/6 — below this subreddit's threshold of 3.
Audit Breakdown:
15
u/expertisimus Jul 28 '26
It seems rather overcomplicated and overengineered to me. Why not just create a static array of n* sizeof(some struct)? All you then need is an index that wraps around the max boundary, maybe also item counter if you plan to support pop operations. If the size of the buffer is a power of two then the wrapping can be done by binary arithmetic, storing and retrieving is as simple as offset into an array by index items. Super fast, conserve heap memory, low overhead and works perfectly in my project. 65 lines including comments and includes.
9
u/HalifaxRoad Jul 28 '26
you can drastically speed up circular buffer performance by making the size base 2. This gets rid of the modulo and replaces it with bitwise operations
4
u/timrprobocom Jul 28 '26
What's the advantage of using two mappings? That seems odd.
Why have the buffer be thread-local? One of the big use cases for circular buffers is to communicate data across threads.
You should note that your functions are not thread-safe. You update tail before copying data in.
Routines to ask "how much data is there?" and "how much room is left?" are important.
If head points to the first item and tail to the last, how can we distinguish "empty" from "contains 1 thing"? This is why circular buffers usually set tail to "last+1".
3
u/lucasxi Jul 28 '26
I think the error handling here is leading to much more complicated code for both the library and for the end user using the API. cirbuf_create should return a valid pre if it succeeded or null. cirbuf_destroy should free the memory and it is down to the user to acknowledge the memory has been freed and to throw away the ptr. Adding a status field is adding fluff that isn't needed.
1
u/AutoModerator Jul 28 '26
Hi /u/ankush2324235,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/ZachVorhies Jul 30 '26
Skimmed it.
Style is good
0
u/[deleted] Jul 29 '26
[deleted] — view deleted comment
1
u/[deleted] Jul 29 '26
[removed] — view removed comment