Skip to content

Just a dead simple set of c macros for working with ring buffers in a generic manner.

License

Notifications You must be signed in to change notification settings

neoyong/c-generic-ring-buffer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Philip Thrasher's Crazy Awesome Ring Buffer Macros!

In ringbuffer.h are some naughty macros for easily owning and manipulating generic ring buffers. Yes, they are slightly evil in readability, but they are really fast, and they work great. If you don't like them, don't use them. I use them because it's the only DRY and sane way to add generic ring buffers to your C application.

Example usage:

#include <stdio.h>

// So we can use this in any method, this gives us a typedef named 'intBuffer'.
// Really, this is the only *truely* naughty portion of these macros as it
// creates a complete type definition for you. This is something people will
// have to read the header in order to grok.
ringBuffer_typedef(int, intBuffer);

// The above macro translates exactly to:
typedef struct {
  int size;
  int start;
  int end;
  int* elems;
} intBuffer;


int main() {
  // Declare vars.
  intBuffer myBuffer;

  bufferInit(myBuffer,1024,int);

  // We must have the pointer. All of the macros deal with the pointer.
  // (except for init.)
  intBuffer* myBuffer_ptr;
  myBuffer_ptr = &myBuffer;

  // Write two values.
  bufferWrite(myBuffer_ptr,37);
  bufferWrite(myBuffer_ptr,72);

  // Read a value into a local variable.
  int first;
  bufferRead(myBuffer_ptr,first);
  assert(first == 37); // true

  int second;
  bufferRead(myBuffer_ptr,second);
  assert(second == 72); // true

  return 0;
}

License

The MIT License (MIT)

Copyright (c) 2013 Philip Thrasher

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Just a dead simple set of c macros for working with ring buffers in a generic manner.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 82.9%
  • Shell 17.1%