X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/0daaeb1882f918a279970db7bd9a7313881ca4c0..e3afa470a387e2726de5541e9871ccb2e9915a83:/pkbuf.c diff --git a/pkbuf.c b/pkbuf.c index dcc4d79..341c985 100644 --- a/pkbuf.c +++ b/pkbuf.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: pkbuf.c,v 1.5 2002/01/13 13:33:51 mdw Exp $ + * $Id: pkbuf.c,v 1.7 2004/04/08 01:36:13 mdw Exp $ * * Simple packet buffering * * (c) 2000 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,39 +15,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with mLib; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: pkbuf.c,v $ - * Revision 1.5 2002/01/13 13:33:51 mdw - * Packet handler functions now have a @typedef@ name. - * - * Revision 1.4 2001/02/03 16:23:33 mdw - * Bug fix: handle a disable during a close-induced flush without dumping - * core. - * - * Revision 1.3 2000/07/16 18:55:45 mdw - * Remove some stray debugging code. - * - * Revision 1.2 2000/07/16 12:29:16 mdw - * Change to arena `realloc' interface, to fix a design bug. - * - * Revision 1.1 2000/06/17 10:39:19 mdw - * Experimental new support for packet buffering. - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -86,7 +65,7 @@ void pkbuf_flush(pkbuf *pk, octet *p, size_t len) { size_t l; - size_t keep; + size_t o, keep; if (pk->f & PKBUF_CLOSE) { pk->func(0, 0, pk, 0, pk->p); @@ -100,22 +79,23 @@ void pkbuf_flush(pkbuf *pk, octet *p, size_t len) len = pk->len; } l = p + len - pk->buf; - p = pk->buf; + o = 0; /* --- Now grind through any packets which have accumulated --- */ - while (l > pk->want) { + pk->len = l; + while (l >= pk->want) { size_t sz = pk->want; /* --- Pass a packet to the user handler --- */ keep = 0; - pk->func(p, sz, pk, &keep, pk->p); + pk->func(pk->buf + o, sz, pk, &keep, pk->p); /* --- Adjust all the pointers for the next packet --- */ sz -= keep; - p += sz; + o += sz; l -= sz; /* --- Abort here if disabled --- */ @@ -126,8 +106,8 @@ void pkbuf_flush(pkbuf *pk, octet *p, size_t len) /* --- Shunt data around in the buffer --- */ - if (p > pk->buf && l != 0) - memmove(pk->buf, p, l); + if (o > 0 && l != 0) + memmove(pk->buf, pk->buf + o, l); pk->len = l; } @@ -167,10 +147,8 @@ void pkbuf_close(pkbuf *pk) size_t pkbuf_free(pkbuf *pk, octet **p) { - if (!pk->buf) { - fprintf(stderr, "*** allocating new buffer\n"); + if (!pk->buf) pk->buf = x_alloc(pk->a, pk->sz); - } *p = pk->buf + pk->len; return (pk->sz - pk->len); } @@ -221,7 +199,7 @@ void pkbuf_want(pkbuf *pk, size_t want) { pk->want = want; if (want > pk->sz) { - do pk->sz <<= 1; while (want < pk->sz); + do pk->sz <<= 1; while (want > pk->sz); if (pk->buf) { if (pk->len) pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);