5 Copyright (c) 2016 Wrymouth Innovation Ltd
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
27 #include <sys/types.h>
41 /* the buffer is organised internally as follows:
43 * * There are b->size bytes in the buffer.
45 * * Bytes are at offsets 0 to b->size-1
47 * * b->ridx points to the first readable byte
49 * * b->widx points to the first empty space
51 * * b->ridx < b->widx indicates a non-wrapped buffer:
56 * ........XXXXXXXXX................
58 * * b->ridx > b->widx indicates a wrapped buffer:
63 * XXXXXXXX.........XXXXXXXXXXXXXXXX
65 * * b->ridx == b->widx indicates a FULL buffer:
67 * * b->ridx == b->widx indicates a wrapped buffer:
72 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
74 * An empty buffer is indicated by empty=1
79 bufNew (ssize_t size
, ssize_t hwm
)
81 buffer_t
*b
= calloc (1, sizeof (buffer_t
));
82 b
->buf
= calloc (1, size
);
91 bufFree (buffer_t
* b
)
97 /* get a maximal span to read. Returns 0 if buffer
101 bufGetReadSpan (buffer_t
* b
, void **addr
)
108 *addr
= &(b
->buf
[b
->ridx
]);
109 ssize_t len
= b
->widx
- b
->ridx
;
111 len
= b
->size
- b
->ridx
;
115 /* get a maximal span to write. Returns 0 id buffer is full
118 bufGetWriteSpan (buffer_t
* b
, void **addr
)
127 if (b
->ridx
== b
->widx
)
132 *addr
= &(b
->buf
[b
->widx
]);
133 ssize_t len
= b
->ridx
- b
->widx
;
135 len
= b
->size
- b
->widx
;
139 /* mark size bytes as read */
141 bufDoneRead (buffer_t
* b
, ssize_t size
)
143 while (!b
->empty
&& (size
> 0))
145 /* empty can't occur here, so equal pointers means full */
146 ssize_t len
= b
->widx
- b
->ridx
;
148 len
= b
->size
- b
->ridx
;
150 /* len is the number of bytes in one read span */
155 if (b
->ridx
>= b
->size
)
158 if (b
->ridx
== b
->widx
)
169 /* mark size bytes as written */
171 bufDoneWrite (buffer_t
* b
, ssize_t size
)
173 while ((b
->empty
|| (b
->ridx
!= b
->widx
)) && (size
> 0))
175 /* full can't occur here, so equal pointers means empty */
176 ssize_t len
= b
->ridx
- b
->widx
;
178 len
= b
->size
- b
->widx
;
180 /* len is the number of bytes in one write span */
185 if (b
->widx
>= b
->size
)
188 /* it can't be empty as we've written at least one byte */
196 bufIsEmpty (buffer_t
* b
)
202 bufIsFull (buffer_t
* b
)
204 return !b
->empty
&& (b
->ridx
== b
->widx
);
208 bufIsOverHWM (buffer_t
* b
)
210 return bufGetCount (b
) > b
->hwm
;
214 bufGetFree (buffer_t
* b
)
216 return b
->size
- bufGetCount (b
);
220 bufGetCount (buffer_t
* b
)
224 return b
->widx
- b
->ridx
+ ((b
->ridx
< b
->widx
) ? 0 : b
->size
);