summaryrefslogtreecommitdiff
path: root/usr.bin/make/buf.c
diff options
context:
space:
mode:
authordsl <dsl@NetBSD.org>2009-01-17 13:29:37 +0000
committerdsl <dsl@NetBSD.org>2009-01-17 13:29:37 +0000
commit462d090d0f748eb6ce53359df0b97e2ddefe2ade (patch)
tree950b659773eaa3f3934efccd205e37e9edcaa978 /usr.bin/make/buf.c
parentecef5ef3032b984a39dede3a6b195ec3c0ece5ff (diff)
Change 'Buffer' so that it is the actual struct, not a pointer to it.
Saves having to malloc/free a fixed size structure. Buf_Init() now takes ptr to Buffer to initialiase. Change Buf_Destroy() to return ptr to string when not freed. Remove large number of casts to (Byte) and (Byte *) - 'Byte' is 'char' here. Buf_AddByte[s] guarantees that the data is 0 termininated, so never add '\0'. Keep 'count' not 'left' and 'inPtr', code is simplier with only one update. Fix fallou, no functional change.
Diffstat (limited to 'usr.bin/make/buf.c')
-rw-r--r--usr.bin/make/buf.c176
1 files changed, 48 insertions, 128 deletions
diff --git a/usr.bin/make/buf.c b/usr.bin/make/buf.c
index 66ace599b51..1b9f5b4fe86 100644
--- a/usr.bin/make/buf.c
+++ b/usr.bin/make/buf.c
@@ -1,4 +1,4 @@
-/* $NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $ */
+/* $NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
*/
#ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $";
+static char rcsid[] = "$NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
#else
-__RCSID("$NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $");
+__RCSID("$NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $");
#endif
#endif /* not lint */
#endif
@@ -87,7 +87,6 @@ __RCSID("$NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $");
* Functions for automatically-expanded buffers.
*/
-#include "sprite.h"
#include "make.h"
#include "buf.h"
@@ -95,55 +94,22 @@ __RCSID("$NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $");
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
-/*
- * BufExpand --
- * Expand the given buffer to hold the given number of additional
- * bytes.
- * Makes sure there's room for an extra NULL byte at the end of the
- * buffer in case it holds a string.
- */
-#define BufExpand(bp,nb) \
- while (bp->left < (nb)+1) {\
- int newSize = (bp)->size * 2; \
- Byte *newBuf = (Byte *)bmake_realloc((bp)->buffer, newSize); \
- \
- (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
- (bp)->buffer = newBuf;\
- (bp)->size = newSize;\
- (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
- }
-
#define BUF_DEF_SIZE 256 /* Default buffer size */
/*-
*-----------------------------------------------------------------------
- * Buf_OvAddByte --
- * Add a single byte to the buffer. left is zero or negative.
- *
- * Results:
- * None.
- *
- * Side Effects:
- * The buffer may be expanded.
+ * Buf_Expand_1 --
+ * Extend buffer for single byte add.
*
*-----------------------------------------------------------------------
*/
void
-Buf_OvAddByte(Buffer bp, int byte)
+Buf_Expand_1(Buffer *bp)
{
- int nbytes = 1;
- bp->left = 0;
- BufExpand(bp, nbytes);
-
- *bp->inPtr++ = byte;
- bp->left--;
-
- /*
- * Null-terminate
- */
- *bp->inPtr = 0;
+ bp->size += max(bp->size, 16);
+ bp->buffer = bmake_realloc(bp->buffer, bp->size);
}
-
+
/*-
*-----------------------------------------------------------------------
* Buf_AddBytes --
@@ -158,21 +124,22 @@ Buf_OvAddByte(Buffer bp, int byte)
*-----------------------------------------------------------------------
*/
void
-Buf_AddBytes(Buffer bp, int numBytes, const Byte *bytesPtr)
+Buf_AddBytes(Buffer *bp, int numBytes, const Byte *bytesPtr)
{
+ int count = bp->count;
+ Byte *ptr;
- BufExpand(bp, numBytes);
-
- memcpy(bp->inPtr, bytesPtr, numBytes);
- bp->inPtr += numBytes;
- bp->left -= numBytes;
+ if (__predict_false(count + numBytes >= bp->size)) {
+ bp->size += max(bp->size, numBytes + 16);
+ bp->buffer = bmake_realloc(bp->buffer, bp->size);
+ }
- /*
- * Null-terminate
- */
- *bp->inPtr = 0;
+ ptr = bp->buffer + count;
+ bp->count = count + numBytes;
+ ptr[numBytes] = 0;
+ memcpy(ptr, bytesPtr, numBytes);
}
-
+
/*-
*-----------------------------------------------------------------------
* Buf_GetAll --
@@ -187,16 +154,15 @@ Buf_AddBytes(Buffer bp, int numBytes, const Byte *bytesPtr)
*-----------------------------------------------------------------------
*/
Byte *
-Buf_GetAll(Buffer bp, int *numBytesPtr)
+Buf_GetAll(Buffer *bp, int *numBytesPtr)
{
- if (numBytesPtr != NULL) {
- *numBytesPtr = bp->inPtr - bp->buffer;
- }
+ if (numBytesPtr != NULL)
+ *numBytesPtr = bp->count;
return (bp->buffer);
}
-
+
/*-
*-----------------------------------------------------------------------
* Buf_Empty --
@@ -211,34 +177,13 @@ Buf_GetAll(Buffer bp, int *numBytesPtr)
*-----------------------------------------------------------------------
*/
void
-Buf_Empty(Buffer bp)
+Buf_Empty(Buffer *bp)
{
- bp->inPtr = bp->buffer;
- bp->left = bp->size;
- *bp->inPtr = 0;
+ bp->count = 0;
+ *bp->buffer = 0;
}
-
-/*-
- *-----------------------------------------------------------------------
- * Buf_Size --
- * Returns the number of bytes in the given buffer. Doesn't include
- * the null-terminating byte.
- *
- * Results:
- * The number of bytes.
- *
- * Side Effects:
- * None.
- *
- *-----------------------------------------------------------------------
- */
-int
-Buf_Size(Buffer buf)
-{
- return (buf->inPtr - buf->buffer);
-}
-
+
/*-
*-----------------------------------------------------------------------
* Buf_Init --
@@ -257,24 +202,18 @@ Buf_Size(Buffer buf)
*
*-----------------------------------------------------------------------
*/
-Buffer
-Buf_Init(int size)
+void
+Buf_Init(Buffer *bp, int size)
{
- Buffer bp; /* New Buffer */
-
- bp = bmake_malloc(sizeof(*bp));
-
if (size <= 0) {
size = BUF_DEF_SIZE;
}
- bp->left = bp->size = size;
+ bp->size = size;
+ bp->count = 0;
bp->buffer = bmake_malloc(size);
- bp->inPtr = bp->buffer;
- *bp->inPtr = 0;
-
- return (bp);
+ *bp->buffer = 0;
}
-
+
/*-
*-----------------------------------------------------------------------
* Buf_Destroy --
@@ -285,46 +224,27 @@ Buf_Init(int size)
* freeData TRUE if the data should be destroyed
*
* Results:
- * None.
+ * Data buffer, NULL if freed
*
* Side Effects:
* The buffer is freed.
*
*-----------------------------------------------------------------------
*/
-void
-Buf_Destroy(Buffer buf, Boolean freeData)
+Byte *
+Buf_Destroy(Buffer *buf, Boolean freeData)
{
+ Byte *data;
+ data = buf->buffer;
if (freeData) {
- free(buf->buffer);
+ free(data);
+ data = NULL;
}
- free(buf);
-}
-
-/*-
- *-----------------------------------------------------------------------
- * Buf_ReplaceLastByte --
- * Replace the last byte in a buffer.
- *
- * Input:
- * buf buffer to augment
- * byte byte to be written
- *
- * Results:
- * None.
- *
- * Side Effects:
- * If the buffer was empty intially, then a new byte will be added.
- * Otherwise, the last byte is overwritten.
- *
- *-----------------------------------------------------------------------
- */
-void
-Buf_ReplaceLastByte(Buffer buf, int byte)
-{
- if (buf->inPtr == buf->buffer)
- Buf_AddByte(buf, byte);
- else
- *(buf->inPtr - 1) = byte;
+
+ buf->size = 0;
+ buf->count = 0;
+ buf->buffer = NULL;
+
+ return data;
}