diff options
author | Marc Espie <espie@cvs.openbsd.org> | 1999-12-16 16:46:39 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 1999-12-16 16:46:39 +0000 |
commit | 296ccc19536f9d5d7d391200ec996df08ccac23f (patch) | |
tree | 2d2c68b7969b5d86a026767c7c66bb0bee667007 /usr.bin | |
parent | 187eaef221933174b3243421f178461acc5bcc8f (diff) |
Remove redundant fields from struct Buffer.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/make/buf.c | 126 | ||||
-rw-r--r-- | usr.bin/make/buf.h | 27 |
2 files changed, 41 insertions, 112 deletions
diff --git a/usr.bin/make/buf.c b/usr.bin/make/buf.c index 1a104899cfb..58b1c9af29d 100644 --- a/usr.bin/make/buf.c +++ b/usr.bin/make/buf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.c,v 1.10 1999/12/16 16:41:41 espie Exp $ */ +/* $OpenBSD: buf.c,v 1.11 1999/12/16 16:46:38 espie Exp $ */ /* $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */ /* @@ -70,7 +70,7 @@ #if 0 static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: buf.c,v 1.10 1999/12/16 16:41:41 espie Exp $"; +static char rcsid[] = "$OpenBSD: buf.c,v 1.11 1999/12/16 16:46:38 espie Exp $"; #endif #endif /* not lint */ @@ -83,32 +83,23 @@ static char rcsid[] = "$OpenBSD: buf.c,v 1.10 1999/12/16 16:41:41 espie Exp $"; #include "make.h" #include "buf.h" -#ifndef max -#define max(a,b) ((a) > (b) ? (a) : (b)) -#endif - -/* - * BufExpand -- +/* BufExpand -- * Expand the given buffer to hold the given number of additional - * chars. - * Makes sure there's room for an extra NULL char at the end of the - * buffer in case it holds a string. - */ -#define BufExpand(bp,nb) \ -do { \ - char *newBuf; \ - size_t newSize = (bp)->size; \ - \ - do { \ - newSize *= 2 ; \ - (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer); \ - } while ((bp)->left < (nb)+1+BUF_MARGIN); \ - newBuf = erealloc((bp)->buffer, newSize); \ - (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \ - (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer); \ - (bp)->buffer = newBuf; \ - (bp)->size = newSize; \ -} while (0) + * chars. Makes sure there's room for an extra '\0' char at + * the end of the buffer to terminate the string. */ +#define BufExpand(bp,nb) \ +do { \ + size_t occupied = (bp)->inPtr - (bp)->buffer; \ + size_t size = (bp)->endPtr - (bp)->buffer; \ + \ + do { \ + size *= 2 ; \ + } while (size - occupied < (nb)+1+BUF_MARGIN); \ + (bp)->buffer = (bp)->inPtr = (bp)->endPtr = \ + erealloc((bp)->buffer, size); \ + (bp)->inPtr += occupied; \ + (bp)->endPtr += size; \ +} while (0); #define BUF_DEF_SIZE 256 /* Default buffer size */ #define BUF_MARGIN 256 /* Make sure we are comfortable */ @@ -121,17 +112,7 @@ BufOverflow(bp) { BufExpand(bp, 1); } - -/*- - *----------------------------------------------------------------------- - * Buf_AddChars -- - * Add a number of chars to the buffer. - * - * Side Effects: - * Guess what? - * - *----------------------------------------------------------------------- - */ + void Buf_AddChars(bp, numBytes, bytesPtr) Buffer bp; @@ -139,48 +120,13 @@ Buf_AddChars(bp, numBytes, bytesPtr) const char *bytesPtr; { - if (bp->left < numBytes+1) + if (bp->endPtr - bp->inPtr < numBytes+1) BufExpand(bp, numBytes); memcpy(bp->inPtr, bytesPtr, numBytes); bp->inPtr += numBytes; - bp->left -= numBytes; } - -/*- - *----------------------------------------------------------------------- - * Buf_Reset - - * Throw away all chars in a buffer. - * - * Side Effects: - * The chars are discarded. - * - *----------------------------------------------------------------------- - */ -void -Buf_Reset(bp) - Buffer bp; -{ - bp->inPtr = bp->outPtr = bp->buffer; - bp->left = bp->size; -} - -/*- - *----------------------------------------------------------------------- - * Buf_Init -- - * Initialize a buffer. If no initial size is given, a reasonable - * default is used. - * - * Results: - * A buffer to be given to other functions in this library. - * - * Side Effects: - * The buffer is created, the space allocated and pointers - * initialized. - * - *----------------------------------------------------------------------- - */ Buffer Buf_Init(size) size_t size; /* Initial size for the buffer */ @@ -192,23 +138,12 @@ Buf_Init(size) if (size == 0) { size = BUF_DEF_SIZE; } - bp->left = bp->size = size; - bp->buffer = emalloc(size); - bp->inPtr = bp->outPtr = bp->buffer; + bp->inPtr = bp->endPtr = bp->buffer = emalloc(size); + bp->endPtr += size; return (bp); } - -/*- - *----------------------------------------------------------------------- - * Buf_Destroy -- - * Nuke a buffer and all its resources. - * - * Side Effects: - * The buffer is freed. - * - *----------------------------------------------------------------------- - */ + void Buf_Destroy(buf, freeData) Buffer buf; /* Buffer to destroy */ @@ -220,24 +155,13 @@ Buf_Destroy(buf, freeData) } free ((char *)buf); } - -/*- - *----------------------------------------------------------------------- - * Buf_ReplaceLastChar -- - * Replace the last char in a buffer. - * - * Side Effects: - * If the buffer was empty intially, then a new byte will be added. - * Otherwise, the last byte is overwritten. - * - *----------------------------------------------------------------------- - */ + void Buf_ReplaceLastChar(buf, byte) Buffer buf; /* buffer to augment */ char byte; /* byte to be written */ { - if (buf->inPtr == buf->outPtr) + if (buf->inPtr == buf->buffer) Buf_AddChar(buf, byte); else *(buf->inPtr - 1) = byte; diff --git a/usr.bin/make/buf.h b/usr.bin/make/buf.h index 3531ad92bd7..14a6889e2d6 100644 --- a/usr.bin/make/buf.h +++ b/usr.bin/make/buf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.h,v 1.9 1999/12/16 16:41:41 espie Exp $ */ +/* $OpenBSD: buf.h,v 1.10 1999/12/16 16:46:38 espie Exp $ */ /* $NetBSD: buf.h,v 1.7 1996/12/31 17:53:22 christos Exp $ */ /* @@ -52,20 +52,20 @@ #include "sprite.h" typedef struct Buffer { - size_t size; /* Current size of the buffer */ - size_t left; /* Space left (== size - (inPtr - buffer)) */ - char *buffer; /* The buffer itself */ - char *inPtr; /* Place to write to */ - char *outPtr; /* Place to read from */ + char *buffer; /* The buffer itself. */ + char *inPtr; /* Place to write to. */ + char *endPtr; /* End of allocated space. */ } *Buffer; /* Internal support for Buf_AddChar. */ void BufOverflow __P((Buffer)); -/* Buf_AddChar -- Add a single char to a buffer. */ +/* User interface */ + +/* Buf_AddChar -- Add a single char to buffer. */ #define Buf_AddChar(bp, byte) \ do { \ - if (--(bp)->left == 0) \ + if ((bp)->endPtr - (bp)->inPtr <= 1) \ BufOverflow(bp); \ *(bp)->inPtr++ = (byte); \ } while (0) @@ -74,6 +74,8 @@ do { \ /* Buf_AddChars -- Add a number of chars to the buffer. */ void Buf_AddChars __P((Buffer, size_t, const char *)); +/* Buf_Reset -- Remove all chars from a buffer. */ +#define Buf_Reset(bp) ((void)((bp)->inPtr = (bp)->buffer)) /* Buf_AddSpace -- Add a space to buffer. */ #define Buf_AddSpace(b) Buf_AddChar((b), ' ') /* Buf_AddString -- Add the contents of a NULL terminated string to buffer. */ @@ -82,15 +84,18 @@ void Buf_AddChars __P((Buffer, size_t, const char *)); #define Buf_AddInterval(b, s, e) Buf_AddChars((b), (e) - (s), (s)) /* Buf_Retrieve -- Retrieve data from a buffer, as a NULL terminated string. */ -#define Buf_Retrieve(bp) (*(bp)->inPtr = '\0', (bp)->outPtr) +#define Buf_Retrieve(bp) (*(bp)->inPtr = '\0', (bp)->buffer) /* Buf_Size -- Return the number of chars in the given buffer. * Doesn't include the null-terminating char. */ -#define Buf_Size(bp) ((size_t)((bp)->inPtr - (bp)->outPtr)) +#define Buf_Size(bp) ((size_t)((bp)->inPtr - (bp)->buffer)) -void Buf_Reset __P((Buffer)); +/* Buf_Init -- Initialize a buffer. If no initial size is given, + * a reasonable default is used. */ Buffer Buf_Init __P((size_t)); +/* Buf_Destroy -- Nuke a buffer and all its resources if Boolean is TRUE. */ void Buf_Destroy __P((Buffer, Boolean)); +/* Buf_ReplaceLastChar -- Replace the last char in a buffer. */ void Buf_ReplaceLastChar __P((Buffer, char)); #endif /* _BUF_H */ |