4 * supports a concurrent reader and writer without locking
8 struct ts27010_ringbuf {
16 static inline struct ts27010_ringbuf *ts27010_ringbuf_alloc(int len)
18 struct ts27010_ringbuf *rbuf;
20 rbuf = kzalloc(sizeof(*rbuf) + len, GFP_KERNEL);
31 static inline void ts27010_ringbuf_free(struct ts27010_ringbuf *rbuf)
36 static inline int ts27010_ringbuf_level(struct ts27010_ringbuf *rbuf)
38 int level = rbuf->head - rbuf->tail;
41 level = rbuf->len + level;
46 static inline int ts27010_ringbuf_room(struct ts27010_ringbuf *rbuf)
48 return rbuf->len - ts27010_ringbuf_level(rbuf) - 1;
51 static inline u8 ts27010_ringbuf_peek(struct ts27010_ringbuf *rbuf, int i)
53 return rbuf->buf[(rbuf->tail + i) % rbuf->len];
56 static inline int ts27010_ringbuf_consume(struct ts27010_ringbuf *rbuf,
59 count = min(count, ts27010_ringbuf_level(rbuf));
61 rbuf->tail = (rbuf->tail + count) % rbuf->len;
66 static inline int ts27010_ringbuf_push(struct ts27010_ringbuf *rbuf, u8 datum)
68 if (ts27010_ringbuf_room(rbuf) == 0)
71 rbuf->buf[rbuf->head] = datum;
72 rbuf->head = (rbuf->head + 1) % rbuf->len;
77 static inline int ts27010_ringbuf_write(struct ts27010_ringbuf *rbuf,
78 const u8 *data, int len)
83 for (i = 0; i < len; i++)
84 count += ts27010_ringbuf_push(rbuf, data[i]);