SlideShare a Scribd company logo
/*********************************************************************************************
*
* ringBufferSample.c
*
* This file contains code for a simple ring buffer. Ring buffers were commonly used in
* microcontroller architectures with limited memory, and are mostly deprecated in newer
* systems. Most commonly, these were used for catching serial character inputs and
* holding the data until a full data packet could arrive.
*
* Ring buffers are used primarily as statically allocated queues. The reason it uses
* statically allocated memory over dynamic memory is due to its primary use being in
* microcontrollers with very small amounts of memory, typically on the order of 64kB.
*
* The struct definition, RING_BUFFER_ERROR definition, and function declarations should
* be contained in a header file.
*
* This was written as sample code for my LinkedIn profile.
*
* This sample code is Copyright by Matt Kutschera, 2017
*
* It may viewed by anyone, and may be distributed by LinkedIn only on my personal
* LinkedIn profile. Any other method of distribution is available only by approval on
* a case by case basis.
*
* 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.
*
**********************************************************************************************/
/*
* START OF HEADER FILE -----------------------------------------------------------------------
*/
#define RING_BUFFER_ERROR 0x0100 //number larger than a character
/*
* The ring buffer contains an array to hold the data, and two array indeces
*/
struct ringBuffer
{
char buffer[SIZE];
size_t input;
size_t output;
};
/**
* initBuffer()
*
* Initializes a ring buffer
*
* @param *ring Pointer to an allocated ring buffer
* @return 0 upon successful completion, RING_BUFFER_ERROR if buffer is not allocated
*/
int initBuffer(struct ringBuffer *ring);
/**
*
* ringBufferEnQueue()
*
* Adds a value to the end of the ring buffer
*
* @param *ring Pointer to ring buffer to add value to
* @param value Value to add
* @return Value added, or RING_BUFFER_ERROR if either the buffer is not allocated or
* buffer is full
*/
int ringBufferEnQueue(struct ringBuffer *ring, char value);
/**
*
* ringBufferDeQueue()
*
* Reads a value from the front of the ring buffer, and removes it from the buffer
*
* @param *ring Pointer to ring buffer to get value from
* @return Value from ring buffer, or RING_BUFFER_ERROR if either the buffer is not
* allocated or buffer is empty
*/
int ringBufferDeQueue(struct ringBuffer *ring);
/**
*
* ringBufferSize()
*
* Gets the number of bytes in the ring buffer
*
* @param *ring Pointer to ring buffer to count elements of
* @return Number of elements in the buffer, or -1 if the buffer is not
* allocated
*/
int ringBufferSize(struct ringBuffer *ring);
/*
* END OF HEADER FILE / START OF C FILE -----------------------------------------------------
*/
#define SIZE 128 //arbitrary value for sample. in actual use, it should be
//large enough to hold at least two packets of data, but
//not so large that it restricts the available memory for
//other aspects of the program.
/*
* As this code is meant for small memory programs, it is possible that libraries containing
* the standard NULL macro are not included.
*/
#ifndef NULL
#define NULL (void *)0
#endif
/**
* initBuffer()
*
* Initializes a ring buffer
*
* @param *ring Pointer to an allocated ring buffer
* @return 0 upon successful completion, RING_BUFFER_ERROR if buffer is not allocated
*/
int initBuffer(struct ringBuffer *ring)
{
int retVal = RING_BUFFER_ERROR;
if (ring != NULL)
{
ring->input = 0;
ring->output = 0;
retVal = 0;
}
return retVal;
}
/**
*
* ringBufferEnQueue()
*
* Adds a value to the end of the ring buffer
*
* @param *ring Pointer to ring buffer to add value to
* @param value Value to add
* @return Value added, or RING_BUFFER_ERROR if either the buffer is not allocated or
* buffer is full
*/
int ringBufferEnQueue(struct ringBuffer *ring, char value)
{
int retVal = RING_BUFFER_ERROR;
if((ring != NULL)
&& (((ring->input +1) % SIZE) != ring->output)) //check to see if buffer is full
{
retVal = value;
ring->buffer[input];
input = (input++) % SIZE; //wraps around to first element
}
return retVal;
}
/**
*
* ringBufferDeQueue()
*
* Reads a value from the front of the ring buffer, and removes it from the buffer
*
* @param *ring Pointer to ring buffer to get value from
* @return Value from ring buffer, or RING_BUFFER_ERROR if either the buffer is not
* allocated or buffer is empty
*/
int ringBufferDeQueue(struct ringBuffer *ring)
{
int retVal = RING_BUFFER_ERROR;
if((ring != NULL)
&& (ring->input != ring->output)) //check to see if buffer is empty
{
retVal = ring->buffer[output];
output = (output++) % SIZE; //wraps around to first element
}
return retVal;
}
/**
*
* ringBufferSize()
*
* Gets the number of bytes in the ring buffer
*
* @param *ring Pointer to ring buffer to count elements of
* @return Number of elements in the buffer, or -1 if the buffer is not
* allocated
*/
int ringBufferSize(struct ringBuffer *ring)
{
int retVal = -1;
if(ring != NULL)
{
retVal = ring->input - ring->output;
if(retVal < 0)
retVal += SIZE;
}
return retVal;
}

More Related Content

Similar to ringBufferSample

Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdf
kitty811
 
Multithreaded sockets c++11
Multithreaded sockets c++11Multithreaded sockets c++11
Multithreaded sockets c++11
Russell Childs
 
The Ring programming language version 1.5.3 book - Part 94 of 184
The Ring programming language version 1.5.3 book - Part 94 of 184The Ring programming language version 1.5.3 book - Part 94 of 184
The Ring programming language version 1.5.3 book - Part 94 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 95 of 184
The Ring programming language version 1.5.3 book - Part 95 of 184The Ring programming language version 1.5.3 book - Part 95 of 184
The Ring programming language version 1.5.3 book - Part 95 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 126 of 210
The Ring programming language version 1.9 book - Part 126 of 210The Ring programming language version 1.9 book - Part 126 of 210
The Ring programming language version 1.9 book - Part 126 of 210
Mahmoud Samir Fayed
 
Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
contact32
 
I am having problems getting the correct output- Here are the instruct (1).pdf
I am having problems getting the correct output- Here are the instruct (1).pdfI am having problems getting the correct output- Here are the instruct (1).pdf
I am having problems getting the correct output- Here are the instruct (1).pdf
sonunotwani
 
Radix 2 code
Radix 2 codeRadix 2 code
Radix 2 codepradipakv
 
Help implement BST- The code must follow the instruction below as well.pdf
Help implement BST- The code must follow the instruction below as well.pdfHelp implement BST- The code must follow the instruction below as well.pdf
Help implement BST- The code must follow the instruction below as well.pdf
a2zmobiles
 
The Ring programming language version 1.3 book - Part 64 of 88
The Ring programming language version 1.3 book - Part 64 of 88The Ring programming language version 1.3 book - Part 64 of 88
The Ring programming language version 1.3 book - Part 64 of 88
Mahmoud Samir Fayed
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
Russell Childs
 
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
ajay1317
 

Similar to ringBufferSample (13)

Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdf
 
Multithreaded sockets c++11
Multithreaded sockets c++11Multithreaded sockets c++11
Multithreaded sockets c++11
 
The Ring programming language version 1.5.3 book - Part 94 of 184
The Ring programming language version 1.5.3 book - Part 94 of 184The Ring programming language version 1.5.3 book - Part 94 of 184
The Ring programming language version 1.5.3 book - Part 94 of 184
 
The Ring programming language version 1.5.3 book - Part 95 of 184
The Ring programming language version 1.5.3 book - Part 95 of 184The Ring programming language version 1.5.3 book - Part 95 of 184
The Ring programming language version 1.5.3 book - Part 95 of 184
 
The Ring programming language version 1.9 book - Part 126 of 210
The Ring programming language version 1.9 book - Part 126 of 210The Ring programming language version 1.9 book - Part 126 of 210
The Ring programming language version 1.9 book - Part 126 of 210
 
Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
 
I am having problems getting the correct output- Here are the instruct (1).pdf
I am having problems getting the correct output- Here are the instruct (1).pdfI am having problems getting the correct output- Here are the instruct (1).pdf
I am having problems getting the correct output- Here are the instruct (1).pdf
 
Radix 2 code
Radix 2 codeRadix 2 code
Radix 2 code
 
Supermarket
SupermarketSupermarket
Supermarket
 
Help implement BST- The code must follow the instruction below as well.pdf
Help implement BST- The code must follow the instruction below as well.pdfHelp implement BST- The code must follow the instruction below as well.pdf
Help implement BST- The code must follow the instruction below as well.pdf
 
The Ring programming language version 1.3 book - Part 64 of 88
The Ring programming language version 1.3 book - Part 64 of 88The Ring programming language version 1.3 book - Part 64 of 88
The Ring programming language version 1.3 book - Part 64 of 88
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
 

ringBufferSample

  • 1. /********************************************************************************************* * * ringBufferSample.c * * This file contains code for a simple ring buffer. Ring buffers were commonly used in * microcontroller architectures with limited memory, and are mostly deprecated in newer * systems. Most commonly, these were used for catching serial character inputs and * holding the data until a full data packet could arrive. * * Ring buffers are used primarily as statically allocated queues. The reason it uses * statically allocated memory over dynamic memory is due to its primary use being in * microcontrollers with very small amounts of memory, typically on the order of 64kB. * * The struct definition, RING_BUFFER_ERROR definition, and function declarations should * be contained in a header file. * * This was written as sample code for my LinkedIn profile. * * This sample code is Copyright by Matt Kutschera, 2017 * * It may viewed by anyone, and may be distributed by LinkedIn only on my personal * LinkedIn profile. Any other method of distribution is available only by approval on * a case by case basis. * * 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. * **********************************************************************************************/ /* * START OF HEADER FILE ----------------------------------------------------------------------- */ #define RING_BUFFER_ERROR 0x0100 //number larger than a character /* * The ring buffer contains an array to hold the data, and two array indeces */ struct ringBuffer { char buffer[SIZE]; size_t input; size_t output; }; /** * initBuffer() * * Initializes a ring buffer * * @param *ring Pointer to an allocated ring buffer * @return 0 upon successful completion, RING_BUFFER_ERROR if buffer is not allocated */ int initBuffer(struct ringBuffer *ring); /** * * ringBufferEnQueue() * * Adds a value to the end of the ring buffer * * @param *ring Pointer to ring buffer to add value to * @param value Value to add * @return Value added, or RING_BUFFER_ERROR if either the buffer is not allocated or * buffer is full
  • 2. */ int ringBufferEnQueue(struct ringBuffer *ring, char value); /** * * ringBufferDeQueue() * * Reads a value from the front of the ring buffer, and removes it from the buffer * * @param *ring Pointer to ring buffer to get value from * @return Value from ring buffer, or RING_BUFFER_ERROR if either the buffer is not * allocated or buffer is empty */ int ringBufferDeQueue(struct ringBuffer *ring); /** * * ringBufferSize() * * Gets the number of bytes in the ring buffer * * @param *ring Pointer to ring buffer to count elements of * @return Number of elements in the buffer, or -1 if the buffer is not * allocated */ int ringBufferSize(struct ringBuffer *ring); /* * END OF HEADER FILE / START OF C FILE ----------------------------------------------------- */ #define SIZE 128 //arbitrary value for sample. in actual use, it should be //large enough to hold at least two packets of data, but //not so large that it restricts the available memory for //other aspects of the program. /* * As this code is meant for small memory programs, it is possible that libraries containing * the standard NULL macro are not included. */ #ifndef NULL #define NULL (void *)0 #endif /** * initBuffer() * * Initializes a ring buffer * * @param *ring Pointer to an allocated ring buffer * @return 0 upon successful completion, RING_BUFFER_ERROR if buffer is not allocated */ int initBuffer(struct ringBuffer *ring) { int retVal = RING_BUFFER_ERROR; if (ring != NULL) { ring->input = 0; ring->output = 0; retVal = 0; } return retVal; } /** * * ringBufferEnQueue() * * Adds a value to the end of the ring buffer *
  • 3. * @param *ring Pointer to ring buffer to add value to * @param value Value to add * @return Value added, or RING_BUFFER_ERROR if either the buffer is not allocated or * buffer is full */ int ringBufferEnQueue(struct ringBuffer *ring, char value) { int retVal = RING_BUFFER_ERROR; if((ring != NULL) && (((ring->input +1) % SIZE) != ring->output)) //check to see if buffer is full { retVal = value; ring->buffer[input]; input = (input++) % SIZE; //wraps around to first element } return retVal; } /** * * ringBufferDeQueue() * * Reads a value from the front of the ring buffer, and removes it from the buffer * * @param *ring Pointer to ring buffer to get value from * @return Value from ring buffer, or RING_BUFFER_ERROR if either the buffer is not * allocated or buffer is empty */ int ringBufferDeQueue(struct ringBuffer *ring) { int retVal = RING_BUFFER_ERROR; if((ring != NULL) && (ring->input != ring->output)) //check to see if buffer is empty { retVal = ring->buffer[output]; output = (output++) % SIZE; //wraps around to first element } return retVal; } /** * * ringBufferSize() * * Gets the number of bytes in the ring buffer * * @param *ring Pointer to ring buffer to count elements of * @return Number of elements in the buffer, or -1 if the buffer is not * allocated */ int ringBufferSize(struct ringBuffer *ring) { int retVal = -1; if(ring != NULL) { retVal = ring->input - ring->output; if(retVal < 0) retVal += SIZE; } return retVal; }