summaryrefslogtreecommitdiff
path: root/devel/bmake/files/buf.c
diff options
context:
space:
mode:
Diffstat (limited to 'devel/bmake/files/buf.c')
-rw-r--r--devel/bmake/files/buf.c185
1 files changed, 50 insertions, 135 deletions
diff --git a/devel/bmake/files/buf.c b/devel/bmake/files/buf.c
index 87507f06559..594bf86b69b 100644
--- a/devel/bmake/files/buf.c
+++ b/devel/bmake/files/buf.c
@@ -1,4 +1,4 @@
-/* $NetBSD: buf.c,v 1.3 2008/11/11 14:37:05 joerg Exp $ */
+/* $NetBSD: buf.c,v 1.4 2009/09/18 21:27:25 joerg 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.3 2008/11/11 14:37:05 joerg Exp $";
+static char rcsid[] = "$NetBSD: buf.c,v 1.4 2009/09/18 21:27:25 joerg 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.3 2008/11/11 14:37:05 joerg Exp $");
+__RCSID("$NetBSD: buf.c,v 1.4 2009/09/18 21:27:25 joerg Exp $");
#endif
#endif /* not lint */
#endif
@@ -87,7 +87,6 @@ __RCSID("$NetBSD: buf.c,v 1.3 2008/11/11 14:37:05 joerg Exp $");
* Functions for automatically-expanded buffers.
*/
-#include "sprite.h"
#include "make.h"
#include "buf.h"
@@ -95,56 +94,22 @@ __RCSID("$NetBSD: buf.c,v 1.3 2008/11/11 14:37:05 joerg 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)->outPtr = newBuf + ((bp)->outPtr - (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 --
@@ -159,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 --
@@ -188,19 +154,18 @@ 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->outPtr;
- }
+ if (numBytesPtr != NULL)
+ *numBytesPtr = bp->count;
- return (bp->outPtr);
+ return (bp->buffer);
}
-
+
/*-
*-----------------------------------------------------------------------
- * Buf_Discard --
+ * Buf_Empty --
* Throw away bytes in a buffer.
*
* Results:
@@ -212,38 +177,13 @@ Buf_GetAll(Buffer bp, int *numBytesPtr)
*-----------------------------------------------------------------------
*/
void
-Buf_Discard(Buffer bp, int numBytes)
+Buf_Empty(Buffer *bp)
{
- if (bp->inPtr - bp->outPtr <= numBytes) {
- bp->inPtr = bp->outPtr = bp->buffer;
- bp->left = bp->size;
- *bp->inPtr = 0;
- } else {
- bp->outPtr += numBytes;
- }
-}
-
-/*-
- *-----------------------------------------------------------------------
- * 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->outPtr);
+ bp->count = 0;
+ *bp->buffer = 0;
}
-
+
/*-
*-----------------------------------------------------------------------
* Buf_Init --
@@ -262,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->outPtr = bp->buffer;
- *bp->inPtr = 0;
-
- return (bp);
+ *bp->buffer = 0;
}
-
+
/*-
*-----------------------------------------------------------------------
* Buf_Destroy --
@@ -290,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->outPtr)
- Buf_AddByte(buf, byte);
- else
- *(buf->inPtr - 1) = byte;
+
+ buf->size = 0;
+ buf->count = 0;
+ buf->buffer = NULL;
+
+ return data;
}