Git Product home page Git Product logo

circularbuffer's Introduction

circularBuffer

Generic circular buffers in C

HOW TO USE:

  1. Create a circularBuffer_t pointer;
  2. Assign the pointer using the circularBuffer_create() function. -circularBuffer_create() takes 3 parameters:
    • the pointer created in step 1
    • the number of elements in the buffer
    • the size in bytes of each elements -circularBuffer_create() will handle the memory allocation and return a pointer to the buffer if successful (if there was an error, the function will return NULL)
  3. Insert new elements in the buffer using circularBuffer_insert().
  4. Get the value of the elements using circularBuffer_getElement().

************ EXAMPLE CODE ************

#include "circularBuffer.h"

int main()
{
  	/* Creation of the circularbuffer */
	static circularBuffer_t* cb = NULL;
	cb = circularBuffer_create(cb, 100, sizeof(float));
	
	/* Fill the buffer */
	float f = 0.0f;
	for (uint16_t i = 0; i < cb->num; i++)
	{
		circularBuffer_insert(cb, &f);
		f++;
	}
	
	/* Read the values from the buffer */
	float* temp;
	for (uint8_t i = 0; i < cb->num; i++)
	{
		temp = (float*)circularBuffer_getElement(cb, i);
		printf("%f \n", *temp);
	}
	
	/* Calculate size gain compared to a normal non-generic array */
	uint16_t originalBytes = cb->num * cb->size;
	uint16_t fullBytes = originalBytes + sizeof(circularBuffer_t);
	float gain = (fullBytes * 100.0) / originalBytes;

	printf("This array takes %d bytes \n", fullBytes);
	printf("A normal array would have taken %d bytes \n", originalBytes);
	printf("There is a %f%% size ratio \n", gain);
}

The code above returns:

0.000000  
1.000000  
2.000000  

...

98.000000  
99.000000  
This array takes 416 bytes  
A normal array would have taken 400 bytes  
There is a 104% size ratio  

circularbuffer's People

Contributors

raesangur avatar

Watchers

James Cloos avatar  avatar

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.