chiark / gitweb /
serialmgrd: Support recent versions of perl
[sympathy.git] / src / ring.h
1 /* 
2  * ring.h:
3  *
4  * Copyright (c) 2008 James McKenzie <sympathy@madingley.org>,
5  * All rights reserved.
6  *
7  */
8
9 /* 
10  * $Id: ring.h,v 1.6 2008/03/10 11:49:33 james Exp $
11  */
12
13 /* 
14  * $Log: ring.h,v $
15  * Revision 1.6  2008/03/10 11:49:33  james
16  * *** empty log message ***
17  *
18  * Revision 1.5  2008/03/07 12:37:04  james
19  * *** empty log message ***
20  *
21  * Revision 1.4  2008/03/03 06:04:42  james
22  * *** empty log message ***
23  *
24  * Revision 1.3  2008/03/02 10:37:56  james
25  * *** empty log message ***
26  *
27  * Revision 1.2  2008/02/12 22:36:46  james
28  * *** empty log message ***
29  *
30  * Revision 1.1  2008/02/08 15:06:42  james
31  * *** empty log message ***
32  *
33  */
34
35 #ifndef __RING_H__
36 #define __RING_H__
37
38 typedef struct {
39   uint8_t *ring;
40   int wptr;
41   int rptr;
42   int size;
43 } Ring;
44
45 #define RING_NEXT(r,a) (((a)+1) % ((r)->size))
46 #define RING_NEXT_R(r) RING_NEXT(r,r->rptr)
47 #define RING_NEXT_W(r) RING_NEXT(r,r->wptr)
48
49 #define RING_EMPTY(r) (((r)->wptr) == ((r)->rptr))
50 #define RING_FULL(r) (RING_NEXT_W(r) == ((r)->rptr))
51
52 static inline int
53 ring_write_one (Ring * r, uint8_t * c)
54 {
55   if (RING_FULL (r))
56     return 0;
57
58   r->ring[r->wptr++] = *c;
59
60   if (r->wptr == r->size)
61     r->wptr = 0;
62
63   return 1;
64 }
65
66 static inline int
67 ring_read_one (Ring * r, uint8_t * c)
68 {
69   if (RING_EMPTY (r))
70     return 0;
71
72   *c = r->ring[r->rptr++];
73
74   if (r->rptr == r->size)
75     r->rptr = 0;
76
77   return 1;
78 }
79
80
81
82 #endif /* __RING_H__ */