File: buffer.c

package info (click to toggle)
nbd 1:3.20-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,960 kB
  • sloc: ansic: 7,494; sh: 6,496; makefile: 147
file content (225 lines) | stat: -rw-r--r-- 4,801 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*

The MIT License (MIT)

Copyright (c) 2016 Wrymouth Innovation Ltd

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/

#include <sys/types.h>

#include "buffer.h"

struct buffer
{
  char *buf;
  ssize_t size;
  ssize_t hwm;
  ssize_t ridx;
  ssize_t widx;
  int empty;
};

/* the buffer is organised internally as follows:
 *
 * * There are b->size bytes in the buffer.
 *
 * * Bytes are at offsets 0 to b->size-1
 * 
 * * b->ridx points to the first readable byte
 *
 * * b->widx points to the first empty space
 *
 * * b->ridx < b->widx indicates a non-wrapped buffer:
 *
 *     0       ridx     widx            size
 *     |       |        |               |
 *     V       V        V               V
 *     ........XXXXXXXXX................
 *
 * * b->ridx > b->widx indicates a wrapped buffer:
 *
 *     0       widx     ridx            size
 *     |       |        |               |
 *     V       V        V               V
 *     XXXXXXXX.........XXXXXXXXXXXXXXXX
 *
 * * b->ridx == b->widx indicates a FULL buffer:
 *
 * * b->ridx == b->widx indicates a wrapped buffer:
 *
 *     0       widx == ridx            size
 *     |       |                       |
 *     V       V                       V
 *     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 *
 * An empty buffer is indicated by empty=1
 *
 */

buffer_t *
bufNew (ssize_t size, ssize_t hwm)
{
  buffer_t *b = calloc (1, sizeof (buffer_t));
  b->buf = calloc (1, size);
  b->size = size;
  b->hwm = hwm;
  b->empty = 1;
  return b;
}


void
bufFree (buffer_t * b)
{
  free (b->buf);
  free (b);
}

/* get a maximal span to read. Returns 0 if buffer
 * is empty
 */
ssize_t
bufGetReadSpan (buffer_t * b, void **addr)
{
  if (b->empty)
    {
      *addr = NULL;
      return 0;
    }
  *addr = &(b->buf[b->ridx]);
  ssize_t len = b->widx - b->ridx;
  if (len <= 0)
    len = b->size - b->ridx;
  return len;
}

/* get a maximal span to write. Returns 0 id buffer is full
 */
ssize_t
bufGetWriteSpan (buffer_t * b, void **addr)
{
  if (b->empty)
    {
      *addr = b->buf;
      b->ridx = 0;
      b->widx = 0;
      return b->size;
    }
  if (b->ridx == b->widx)
    {
      *addr = NULL;
      return 0;
    }
  *addr = &(b->buf[b->widx]);
  ssize_t len = b->ridx - b->widx;
  if (len <= 0)
    len = b->size - b->widx;
  return len;
}

/* mark size bytes as read */
void
bufDoneRead (buffer_t * b, ssize_t size)
{
  while (!b->empty && (size > 0))
    {
      /* empty can't occur here, so equal pointers means full */
      ssize_t len = b->widx - b->ridx;
      if (len <= 0)
	len = b->size - b->ridx;

      /* len is the number of bytes in one read span */
      if (len > size)
	len = size;

      b->ridx += len;
      if (b->ridx >= b->size)
	b->ridx = 0;

      if (b->ridx == b->widx)
	{
	  b->ridx = 0;
	  b->widx = 0;
	  b->empty = 1;
	}

      size -= len;
    }
}

/* mark size bytes as written */
void
bufDoneWrite (buffer_t * b, ssize_t size)
{
  while ((b->empty || (b->ridx != b->widx)) && (size > 0))
    {
      /* full can't occur here, so equal pointers means empty */
      ssize_t len = b->ridx - b->widx;
      if (len <= 0)
	len = b->size - b->widx;

      /* len is the number of bytes in one write span */
      if (len > size)
	len = size;

      b->widx += len;
      if (b->widx >= b->size)
	b->widx = 0;

      /* it can't be empty as we've written at least one byte */
      b->empty = 0;

      size -= len;
    }
}

int
bufIsEmpty (buffer_t * b)
{
  return b->empty;
}

int
bufIsFull (buffer_t * b)
{
  return !b->empty && (b->ridx == b->widx);
}

int
bufIsOverHWM (buffer_t * b)
{
  return bufGetCount (b) > b->hwm;
}

ssize_t
bufGetFree (buffer_t * b)
{
  return b->size - bufGetCount (b);
}

ssize_t
bufGetCount (buffer_t * b)
{
  if (b->empty)
    return 0;
  return b->widx - b->ridx + ((b->ridx < b->widx) ? 0 : b->size);
}