Skip to content

Instantly share code, notes, and snippets.

@pftbest
Created June 24, 2017 15:53
Show Gist options
  • Save pftbest/5626b95d7b495b85ac669dd54a87ee4c to your computer and use it in GitHub Desktop.
Save pftbest/5626b95d7b495b85ac669dd54a87ee4c to your computer and use it in GitHub Desktop.
ring_t
#include "ring.h"
int ring_add(ring_t *ring, ring_data_t data)
{
ring_pos_t next_head = (ring->head + 1) % RING_SIZE;
if (next_head != ring->tail) {
ring->data[ring->head] = data;
ring->head = next_head;
return 0;
} else {
return -1;
}
}
int ring_del(ring_t *ring)
{
if (ring->head != ring->tail) {
ring_data_t data = ring->data[ring->tail];
ring->tail = (ring->tail + 1) % RING_SIZE;
return data;
} else {
return -1;
}
}
#ifndef RING_H
#define RING_H
#include <stdint.h>
#define RING_SIZE 32
typedef uint8_t ring_data_t;
typedef uint16_t ring_pos_t;
typedef struct {
volatile ring_pos_t head;
volatile ring_pos_t tail;
volatile ring_data_t data[RING_SIZE];
} ring_t;
int ring_add(ring_t *ring, ring_data_t data);
int ring_del(ring_t *ring);
#endif // RING_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment