GithubHelp home page GithubHelp logo

willemt / cbuffer Goto Github PK

View Code? Open in Web Editor NEW
248.0 18.0 42.0 168 KB

A circular buffer written in C using Posix calls to create a contiguously mapped memory space. BSD Licensed.

License: Other

C 90.94% Shell 4.04% Makefile 5.02%

cbuffer's Introduction

Circular buffers are tricky. The caller needs to know if the polled item crosses the maximum size of the underlying array.

This circular buffer uses a mmap magic trick to make the caller's life easier.

This is how we do it:

  1. mmap is used to mirror the buffer like below:
doc/circular_buffer_mmap.png
  1. the "mirrored" buffer is then placed beside the buffer. When the user polls the item it doesn't matter if the item crosses the buffer's boundary:
doc/circular_buffer_mmap_portal.png

Compatibility

This only works on UNIX systems.

Windows support existed in d10238600a82de32fe1cb01bec8667d3a67e382c but was removed. This is because Windows doesn't actually have mmap and trying to get the same behaviour is painful. Maybe you should check this out instead: https://github.com/willemt/bipbuffer

cbuffer's People

Contributors

willemt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cbuffer's Issues

Achieving this buffer in Python

Hi there Willem, I was taking a look at your repo on github and have been looking to do something similar in python. 

However, I cannot seem to figure out how to do this due to not having the pointer arithmetic
available within the mmap call to python. 
https://github.com/willemt/cbuffer/blob/master/cbuffer.c#L52

Line 52 is what I cannot mimic, because the offset param here does not help us with the window. 
Any help would be greatly appreciated!   

Regards 

Update me->tail with a single assignment operation

In the original code, me->tail is updated through a sum and then a modulo operation. This creates a race condition if another thread reads the value of tail at the same time (for instance in the usedspace function). So doing the assignment in a single instruction should be safer. It's no guarantee though, so using atomic primitives would be safer.

I created pull request #4.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.