Update copyright statement
[nbd.git] / buffer.c
blob7009615dda4043fe2a3d997aafcddbed4e47ffc3
1 /*
3 The MIT License (MIT)
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>
29 #include "buffer.h"
31 struct buffer
33 char *buf;
34 ssize_t size;
35 ssize_t hwm;
36 ssize_t ridx;
37 ssize_t widx;
38 int empty;
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:
53 * 0 ridx widx size
54 * | | | |
55 * V V V V
56 * ........XXXXXXXXX................
58 * * b->ridx > b->widx indicates a wrapped buffer:
60 * 0 widx ridx size
61 * | | | |
62 * V V V V
63 * XXXXXXXX.........XXXXXXXXXXXXXXXX
65 * * b->ridx == b->widx indicates a FULL buffer:
67 * * b->ridx == b->widx indicates a wrapped buffer:
69 * 0 widx == ridx size
70 * | | |
71 * V V V
72 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
74 * An empty buffer is indicated by empty=1
78 buffer_t *
79 bufNew (ssize_t size, ssize_t hwm)
81 buffer_t *b = calloc (1, sizeof (buffer_t));
82 b->buf = calloc (1, size);
83 b->size = size;
84 b->hwm = hwm;
85 b->empty = 1;
86 return b;
90 void
91 bufFree (buffer_t * b)
93 free (b->buf);
94 free (b);
97 /* get a maximal span to read. Returns 0 if buffer
98 * is empty
100 ssize_t
101 bufGetReadSpan (buffer_t * b, void **addr)
103 if (b->empty)
105 *addr = NULL;
106 return 0;
108 *addr = &(b->buf[b->ridx]);
109 ssize_t len = b->widx - b->ridx;
110 if (len <= 0)
111 len = b->size - b->ridx;
112 return len;
115 /* get a maximal span to write. Returns 0 id buffer is full
117 ssize_t
118 bufGetWriteSpan (buffer_t * b, void **addr)
120 if (b->empty)
122 *addr = b->buf;
123 b->ridx = 0;
124 b->widx = 0;
125 return b->size;
127 if (b->ridx == b->widx)
129 *addr = NULL;
130 return 0;
132 *addr = &(b->buf[b->widx]);
133 ssize_t len = b->ridx - b->widx;
134 if (len <= 0)
135 len = b->size - b->widx;
136 return len;
139 /* mark size bytes as read */
140 void
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;
147 if (len <= 0)
148 len = b->size - b->ridx;
150 /* len is the number of bytes in one read span */
151 if (len > size)
152 len = size;
154 b->ridx += len;
155 if (b->ridx >= b->size)
156 b->ridx = 0;
158 if (b->ridx == b->widx)
160 b->ridx = 0;
161 b->widx = 0;
162 b->empty = 1;
165 size -= len;
169 /* mark size bytes as written */
170 void
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;
177 if (len <= 0)
178 len = b->size - b->widx;
180 /* len is the number of bytes in one write span */
181 if (len > size)
182 len = size;
184 b->widx += len;
185 if (b->widx >= b->size)
186 b->widx = 0;
188 /* it can't be empty as we've written at least one byte */
189 b->empty = 0;
191 size -= len;
196 bufIsEmpty (buffer_t * b)
198 return b->empty;
202 bufIsFull (buffer_t * b)
204 return !b->empty && (b->ridx == b->widx);
208 bufIsOverHWM (buffer_t * b)
210 return bufGetCount (b) > b->hwm;
213 ssize_t
214 bufGetFree (buffer_t * b)
216 return b->size - bufGetCount (b);
219 ssize_t
220 bufGetCount (buffer_t * b)
222 if (b->empty)
223 return 0;
224 return b->widx - b->ridx + ((b->ridx < b->widx) ? 0 : b->size);