Circular Buffer implementation

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Bidski
Experienced Solver
Experienced Solver
Posts: 66
Joined: Sat May 15, 2010 11:30 pm

Circular Buffer implementation

Post by Bidski » Wed Jan 12, 2011 9:26 am

Im not sure how much use anyone will find this, but thought I might post it anyways.

I doubt that this is anywhere near top of line (if there is such a thing as a top of line circular buffer), but it should do the trick.

The implementation and header files are attached, just add the following couple of lines to a suitable spot in your code.

Code: Select all

// Required definitions for CircBuffer
typedef int data_block;
#define BUFFER_SIZE 200
#include "CircBuffer.h"
You can modify the "typedef int data_block" to reflect whatever type of data you are going to be storing in the buffer. For example, if you are reading in bytes of data from a serial port you can change it to be "typedef wxByte data_block" (or whatever byte implementation you prefer to use).

BUFFER_SIZE can obviously be changed to be whatever size you want/need it to be.

There are only 3 functions in this class

Code: Select all

bool WriteData(data_block data);
Writes one "block" of data in to the buffer. Returns true on successful completion, false if the buffer overflows (no other action is taken in the event of a buffer overflow, it is up to the user to ensure the buffer is of sufficient size to prevent an overflow occurring).

Code: Select all

bool ReadData(data_block* data);
Reads one "block" of data in to the passed variable. False is returned if the buffer is empty and the passed variable is set to NULL, true on successful completion.

Code: Select all

int GetBufferStatus() const;
Returns the current size of the buffer (the number of blocks that haven't been read yet).
Attachments
CircBuffer.cpp
circular buffer implementation file
(1.41 KiB) Downloaded 191 times
CircBuffer.h
circular buffer header file
(926 Bytes) Downloaded 182 times

op
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Nov 11, 2011 5:42 pm

Re: Circular Buffer implementation

Post by op » Fri Nov 11, 2011 9:03 pm

Hello ,

in

Code: Select all

bool CircBuffer::ReadData(data_block* data)
there is no validation for NULL for "data" and "*data" output buffer parameter. Which could cause trouble at:

Code: Select all

	*data = CircBuf[CircBufTail];
Besides there is side effect there:

Code: Select all

	if (CircBufLen == 0)
	{
		data = NULL;
		return false;
	}
What if I meant to keep something in the buffer if the read operation fails ?

Regards

Bidski
Experienced Solver
Experienced Solver
Posts: 66
Joined: Sat May 15, 2010 11:30 pm

Re: Circular Buffer implementation

Post by Bidski » Fri Nov 11, 2011 9:49 pm

op wrote: Besides there is side effect there:

Code: Select all

	if (CircBufLen == 0)
	{
		data = NULL;
		return false;
	}
What if I meant to keep something in the buffer if the read operation fails ?
This is just supposed to be a simple circular buffer (mainly designed with a specific idea in mind, but still supposed to be fairly generic). If you need specific functionality then that is up to you to code.

op
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Nov 11, 2011 5:42 pm

Re: Circular Buffer implementation

Post by op » Sat Nov 12, 2011 9:48 am

Clear :)

Post Reply