Change to arena `realloc' interface, to fix a design bug.
[mLib] / pkbuf.c
1 /* -*-c-*-
2 *
3 * $Id: pkbuf.c,v 1.2 2000/07/16 12:29:16 mdw Exp $
4 *
5 * Simple packet buffering
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: pkbuf.c,v $
33 * Revision 1.2 2000/07/16 12:29:16 mdw
34 * Change to arena `realloc' interface, to fix a design bug.
35 *
36 * Revision 1.1 2000/06/17 10:39:19 mdw
37 * Experimental new support for packet buffering.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "alloc.h"
48 #include "arena.h"
49 #include "pkbuf.h"
50
51 /*----- Main code ---------------------------------------------------------*/
52
53 /* --- @pkbuf_flush@ --- *
54 *
55 * Arguments: @pkbuf *pk@ = pointer to buffer block
56 * @octet *p@ = pointer to where to start searching
57 * @size_t len@ = length of new material added
58 *
59 * Returns: ---
60 *
61 * Use: Flushes any complete packets in a packet buffer. New
62 * material is assumed to have been added starting at @p@. If
63 * @p@ is null, then the scan starts at the beginning of the
64 * buffer, and the size of data already in the buffer is used in
65 * place of @len@.
66 *
67 * It is assumed that the buffer is initially enabled. You
68 * shouldn't be contributing data to a disabled buffer anyway.
69 * However, the buffer handler may at some point disable itself,
70 * and @pkbuf_flush@ can cope with this eventuality. Any
71 * pending data is left at the start of the buffer and can be
72 * flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer
73 * is ever re-enabled.
74 */
75
76 void pkbuf_flush(pkbuf *pk, octet *p, size_t len)
77 {
78 size_t l;
79 size_t keep;
80
81 /* --- Initialize variables as necessary --- */
82
83 if (!p) {
84 p = pk->buf;
85 len = pk->len;
86 }
87 l = p + len - pk->buf;
88 p = pk->buf;
89
90 /* --- Now grind through any packets which have accumulated --- */
91
92 while (l > pk->want) {
93 size_t sz = pk->want;
94
95 /* --- Pass a packet to the user handler --- */
96
97 keep = 0;
98 pk->func(p, sz, pk, &keep, pk->p);
99
100 /* --- Adjust all the pointers for the next packet --- */
101
102 sz -= keep;
103 p += sz;
104 l -= sz;
105
106 /* --- Abort here if disabled --- */
107
108 if (!(pk->f & PKBUF_ENABLE))
109 break;
110 }
111
112 /* --- Shunt data around in the buffer --- */
113
114 if (p > pk->buf && l != 0)
115 memmove(pk->buf, p, l);
116 pk->len = l;
117 }
118
119 /* --- @pkbuf_close@ --- *
120 *
121 * Arguments: @pkbuf *pk@ = pointer to buffer block
122 *
123 * Returns: ---
124 *
125 * Use: Informs the client that no more data is likely to arrive. If
126 * there is a partial packet in the buffer, it is discarded.
127 */
128
129 void pkbuf_close(pkbuf *pk)
130 {
131 if (pk->buf) {
132 fprintf(stderr, "*** destroying buffer, closing down\n");
133 x_free(pk->a, pk->buf);
134 pk->buf = 0;
135 }
136 if (pk->f & PKBUF_ENABLE)
137 pk->func(0, 0, pk, 0, pk->p);
138 }
139
140 /* --- @pkbuf_free@ --- *
141 *
142 * Arguments: @pkbuf *pk@ = pointer to buffer block
143 * @octet **p@ = output pointer to free space
144 *
145 * Returns: Free buffer size.
146 *
147 * Use: Returns the free portion of a packet buffer. Data can then
148 * be written to this portion, and split out into packets by
149 * calling @pkbuf_flush@. A buffer is allocated if none
150 * currently exists.
151 */
152
153 size_t pkbuf_free(pkbuf *pk, octet **p)
154 {
155 if (!pk->buf) {
156 fprintf(stderr, "*** allocating new buffer\n");
157 pk->buf = x_alloc(pk->a, pk->sz);
158 }
159 *p = pk->buf + pk->len;
160 return (pk->sz - pk->len);
161 }
162
163 /* --- @pkbuf_snarf@ --- *
164 *
165 * Arguments: @pkbuf *pk@ = pointer to buffer block
166 * @const void *p@ = pointer to input data buffer
167 * @size_t sz@ = size of data in input buffer
168 *
169 * Returns: ---
170 *
171 * Use: Snarfs the data from the input buffer and spits it out as
172 * packets. This interface ignores the complexities of dealing
173 * with disablement: you should be using @pkbuf_free@ to
174 * contribute data if you want to cope with that.
175 */
176
177 void pkbuf_snarf(pkbuf *pk, const void *p, size_t sz)
178 {
179 const octet *pp = p;
180 while (sz && (pk->f & PKBUF_ENABLE)) {
181 size_t bsz;
182 octet *bp;
183
184 bsz = pkbuf_free(pk, &bp);
185 if (bsz > sz)
186 bsz = sz;
187 memcpy(bp, pp, bsz);
188 pkbuf_flush(pk, bp, bsz);
189 pp += bsz;
190 sz -= bsz;
191 }
192 }
193
194 /* --- @pkbuf_want@ --- *
195 *
196 * Arguments: @pkbuf *pk@ = pointer to buffer block
197 * @size_t want@ = how many octets wanted for next packet
198 *
199 * Returns: ---
200 *
201 * Use: Sets the desired size for the next packet to be read. If
202 * it's larger than the current buffer, the buffer is extended.
203 */
204
205 void pkbuf_want(pkbuf *pk, size_t want)
206 {
207 pk->want = want;
208 if (want > pk->sz) {
209 do pk->sz <<= 1; while (want < pk->sz);
210 if (pk->buf) {
211 if (pk->len)
212 pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);
213 else {
214 x_free(pk->a, pk->buf);
215 pk->buf = 0;
216 }
217 }
218 }
219 }
220
221 /* --- @pkbuf_init@ --- *
222 *
223 * Arguments: @pkbuf *pk@ = pointer to buffer block
224 * @void (*func)(octet *b, size_t sz, pkbuf *pk,@
225 * @size_t *keep, void *p)@ =
226 * handler function
227 * @void *p@ = argument pointer for @func@
228 *
229 * Returns: ---
230 *
231 * Use: Initializes a packet buffer block. Any packets are passed to
232 * the provided function for handling.
233 */
234
235 void pkbuf_init(pkbuf *pk,
236 void (*func)(octet */*b*/, size_t /*sz*/,
237 pkbuf */*pk*/, size_t */*keep*/, void */*p*/),
238 void *p)
239 {
240 pk->func = func;
241 pk->p = p;
242 pk->len = 0;
243 pk->f = PKBUF_ENABLE;
244 pk->buf = 0;
245 pk->sz = 256;
246 pk->want = 1;
247 pk->a = arena_global;
248 }
249
250 /* --- @pkbuf_destroy@ --- *
251 *
252 * Arguments: @pkbuf *pk@ = pointer to buffer block
253 *
254 * Returns: ---
255 *
256 * Use: Deallocates a line buffer and frees any resources it owned.
257 */
258
259 void pkbuf_destroy(pkbuf *pk)
260 {
261 if (pk->buf) {
262 x_free(pk->a, pk->buf);
263 pk->buf = 0;
264 }
265 }
266
267 /*----- That's all, folks -------------------------------------------------*/