4 ms·
This is another interesting ring buffer implementation that uses mmap. https://github.com/willemt/cbuffer https://github.com/willemt/cbuffer
by planckscnst 10y ago
This is another interesting ring buffer implementation that uses mmap. https://github.com/willemt/cbuffer https://github.com/willemt/cbuffer
- leni536 10y agoWith the additional benefit that one can have arbitrary slices between head and tail as a contiguous memory region.
- AndyKelley 10y agoHere's another implementation that works on Windows too: https://github.com/andrewrk/libsoundio/blob/master/src/ring_buffer.c https://github.com/andrewrk/libsoundio/blob/master/src/ring_...
- csl 10y agoThis seems to use modulus. The whole point of the mmap trick is to get the kernel/MMU to do the work for you, IIRC. EDIT: Oops, I see they use mirrored memory here as well.
- ohazi 10y agoThat's so cool. Unfortunately for me, the one time I could have used something like this, I was working on an embedded system with no mmap / virtual memory.
- lomnakkus 10y agoI was waiting for someone to mention this -- it seemed much more interesting to me. It's a real classic in the "what the hell, you can do that?" category. (Bonus points if you've done it in a language that requires "extra data" for strings, like storing the length somewhere.) I must admit that I never actually benchmarked my implementation properly -- it might be interesting to see if there are actual trade-offs between mmap vs. copying. (I'm guessing that nothing can beat MMU support, but I think the MMU also supports copy operations, so...?)
- jevinskie 10y agoMike Ash talks about an implementation for macOS/iOS: https://www.mikeash.com/pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html https://www.mikeash.com/pyblog/friday-qa-2012-02-03-ring-buf...