Skip to content

Commit

Permalink
Add: Circular byte buffer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sidcha committed Aug 5, 2018
1 parent 0264508 commit 177b8ae
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
@@ -0,0 +1,18 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.{cpp,C,hpp,c,h}]
indent_size = 4
indent_style = space

[*.md]
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -50,3 +50,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

.vscode
92 changes: 92 additions & 0 deletions circular-byte-buffer.c
@@ -0,0 +1,92 @@
/******************************************************************************
Copyright (c) 2017 Siddharth Chandrasekaran
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.
Author : Siddharth Chandrasekaran
Email : siddharth@embedjournal.com
Date : Sun Aug 5 09:42:31 IST 2017
******************************************************************************/

#include "circular-byte-buffer.h"

int circ_bbuf_push(circ_bbuf_t *c, uint8_t data)
{
int next;

next = c->head + 1; // next is where head will point to after this write.
if (next >= c->maxlen)
next = 0;

if (next == c->tail) // if the head + 1 == tail, circular buffer is full
return -1;

c->buffer[c->head] = data; // Load data and then move
c->head = next; // head to next data offset.
return 0; // return success to indicate successful push.
}

int circ_bbuf_pop(circ_bbuf_t *c, uint8_t *data)
{
int next;

if (c->head == c->tail) // if the head == tail, we don't have any data
return -1;

next = c->tail + 1; // next is where tail will point to after this read.
if(next >= c->maxlen)
next = 0;

*data = c->buffer[c->tail]; // Read data and then move
c->tail = next; // tail to next offset.
return 0; // return success to indicate successful push.
}

#ifdef C_UTILS_TESTING
/* To test this module,
* $ gcc -Wall -DC_UTILS_TESTING circular-byte-buffer.c
* $ ./a.out
*/

CIRC_BBUF_DEF(my_circ_buf, 32);

#include <stdio.h>

int main()
{
uint8_t out_data=0, in_data = 0x55;

if (circ_bbuf_push(&my_circ_buf, in_data)) {
printf("Out of space in CB\n");
return -1;
}

if (circ_bbuf_pop(&my_circ_buf, &out_data)) {
printf("CB is empty\n");
return -1;
}

printf("Push: 0x%x\n", in_data);
printf("Pop: 0x%x\n", out_data);
return 0;
}

#endif
65 changes: 65 additions & 0 deletions circular-byte-buffer.h
@@ -0,0 +1,65 @@
/******************************************************************************
Copyright (c) 2017 Siddharth Chandrasekaran
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.
Author : Siddharth Chandrasekaran
Email : siddharth@embedjournal.com
Date : Sun Aug 5 09:42:31 IST 2017
******************************************************************************/
#ifndef __CIRCULAR_BYTE_BUFFER_H_
#define __CIRCULAR_BYTE_BUFFER_H_

#include <stdint.h>

typedef struct {
uint8_t * const buffer;
int head;
int tail;
const int maxlen;
} circ_bbuf_t;

#define CIRC_BBUF_DEF(x,y) \
uint8_t x##_data_space[y]; \
circ_bbuf_t x = { \
.buffer = x##_data_space, \
.head = 0, \
.tail = 0, \
.maxlen = y \
}

/*
* Method: circ_buf_pop
* Returns:
* 0 - Success
* -1 - Empty
*/
int circ_bbuf_pop(circ_bbuf_t *c, uint8_t *data);

/*
* Method: circ_buf_push
* Returns:
* 0 - Success
* -1 - Out of space
*/
int circ_bbuf_push(circ_bbuf_t *c, uint8_t data);

#endif /* __CIRCULAR_BYTE_BUFFER_H_ */

0 comments on commit 177b8ae

Please sign in to comment.