1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static inline uintptr_t RINGBUF_ADD(uintptr_t p, unsigned int v)
{
    return (((p)+(v))<buffer_len ? (p)+(v) : (p)+(v)-buffer_len);
}
//#define RINGBUF_ADD(p,v) (((p)+(v))<buffer_len ? (p)+(v) : (p)+(v)-buffer_len)
/* Buffer pointer (p) minus value (v), wrapped if necessary */
static inline uintptr_t RINGBUF_SUB(uintptr_t p, unsigned int v)
{
    return ((p>=v) ? (p)-(v) : (p)+buffer_len-(v));
}

//#define RINGBUF_SUB(p,v) ((p>=v) ? (p)-(v) : (p)+buffer_len-(v))
/* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
static inline intptr_t RINGBUF_ADD_CROSS(uintptr_t p1, int v, uintptr_t p2)
{
    return ((p1<p2) ? (int)((p1)+(v))-(int)(p2) : (int)((p1)+(v)-(p2))-(int)buffer_len);
}
/* #define RINGBUF_ADD_CROSS(p1,v,p2) \
((p1<p2) ? (int)((p1)+(v))-(int)(p2) : (int)((p1)+(v)-(p2))-(int)buffer_len) */
/* Bytes available in the buffer */
#define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)