Skip to content

Instantly share code, notes, and snippets.

@jj11hh
Last active February 4, 2021 11:02
Show Gist options
  • Select an option

  • Save jj11hh/de5e4ab02464d6e1ed720c1713f3b5d0 to your computer and use it in GitHub Desktop.

Select an option

Save jj11hh/de5e4ab02464d6e1ed720c1713f3b5d0 to your computer and use it in GitHub Desktop.
#ifndef __LOOPBUF_H__
#define __LOOPBUF_H__
#define LOOPBUF_DEFINE(type, capacity, varname) \
struct { \
unsigned char in; \
unsigned char out; \
type buf[ capacity ]; \
} varname;
#define LOOPBUF_INIT(buf) do { (buf).in = 0; (buf).out = 0; ) while (0)
#define LOOPBUF_SIZE(buf, capacity) ((buf).in >= (buf).out ? (buf).in - (buf).out : (capacity) - (buf).out + (buf).in )
#define LOOPBUF_ENQUEUE(buf, cap, data) do {\
(buf).buf[(buf).in ++] = data;\
if ((buf).in >= capacity) (buf).in = 0;\
} while (0)
#define LOOPBUF_DEQUEUE(buf, cap, data) do {\
(data) = (buf).buf[(buf).out ++];\
if ((buf).out >= (cap)) (buf).out = 0;\
} while (0)
#define LOOPBUF_FULL(buf, cap) ( (((buf).in + 1) >= (cap)) ? (0) : ((buf).in + 1)) == (buf).out )
#endif //__LOOPBUF_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment