replace hash.c; speedup/cleanup from Chris Torek
[unix-history] / usr / src / usr.bin / make / buf.c
index 2f5ed29..6278fad 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)buf.c      5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)buf.c      5.5 (Berkeley) %G%";
 #endif /* not lint */
 
 /*-
 #endif /* not lint */
 
 /*-
@@ -22,13 +22,6 @@ static char sccsid[] = "@(#)buf.c    5.4 (Berkeley) %G%";
 #include    "sprite.h"
 #include    "buf.h"
 
 #include    "sprite.h"
 #include    "buf.h"
 
-typedef struct {
-    int            size;       /* Current size of the buffer */
-    Byte    *buffer;   /* The buffer itself */
-    Byte    *inPtr;    /* Place to write to */
-    Byte    *outPtr;   /* Place to read from */
-} Buf, *BufPtr;
-
 #ifndef max
 #define max(a,b)  ((a) > (b) ? (a) : (b))
 #endif
 #ifndef max
 #define max(a,b)  ((a) > (b) ? (a) : (b))
 #endif
@@ -41,7 +34,7 @@ typedef struct {
  *     buffer in case it holds a string.
  */
 #define BufExpand(bp,nb) \
  *     buffer in case it holds a string.
  */
 #define BufExpand(bp,nb) \
-       if (((bp)->size - ((bp)->inPtr - (bp)->buffer)) < (nb)+1) {\
+       if (bp->left < (nb)+1) {\
            int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
            Byte  *newBuf = (Byte *) realloc((bp)->buffer, newSize); \
            \
            int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
            Byte  *newBuf = (Byte *) realloc((bp)->buffer, newSize); \
            \
@@ -49,6 +42,7 @@ typedef struct {
            (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
            (bp)->buffer = newBuf;\
            (bp)->size = newSize;\
            (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 */
        }
 
 #define BUF_DEF_SIZE   256     /* Default buffer size */
@@ -57,8 +51,8 @@ typedef struct {
 
 /*-
  *-----------------------------------------------------------------------
 
 /*-
  *-----------------------------------------------------------------------
- * Buf_AddByte --
- *     Add a single byte to the buffer.
+ * Buf_OvAddByte --
+ *     Add a single byte to the buffer.  left is zero or negative.
  *
  * Results:
  *     None.
  *
  * Results:
  *     None.
@@ -69,16 +63,16 @@ typedef struct {
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_AddByte (buf, byte)
-    Buffer  buf;
+Buf_OvAddByte (bp, byte)
+    register Buffer bp;
     Byte    byte;
 {
     Byte    byte;
 {
-    register BufPtr  bp = (BufPtr) buf;
 
 
+    bp->left = 0;
     BufExpand (bp, 1);
 
     BufExpand (bp, 1);
 
-    *bp->inPtr = byte;
-    bp->inPtr += 1;
+    *bp->inPtr++ = byte;
+    bp->left--;
 
     /*
      * Null-terminate
 
     /*
      * Null-terminate
@@ -100,17 +94,17 @@ Buf_AddByte (buf, byte)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_AddBytes (buf, numBytes, bytesPtr)
-    Buffer  buf;
+Buf_AddBytes (bp, numBytes, bytesPtr)
+    register Buffer bp;
     int            numBytes;
     Byte    *bytesPtr;
 {
     int            numBytes;
     Byte    *bytesPtr;
 {
-    register BufPtr  bp = (BufPtr) buf;
 
     BufExpand (bp, numBytes);
 
     bcopy (bytesPtr, bp->inPtr, numBytes);
     bp->inPtr += numBytes;
 
     BufExpand (bp, numBytes);
 
     bcopy (bytesPtr, bp->inPtr, numBytes);
     bp->inPtr += numBytes;
+    bp->left -= numBytes;
 
     /*
      * Null-terminate
 
     /*
      * Null-terminate
@@ -132,18 +126,18 @@ Buf_AddBytes (buf, numBytes, bytesPtr)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_UngetByte (buf, byte)
-    Buffer  buf;
+Buf_UngetByte (bp, byte)
+    register Buffer bp;
     Byte    byte;
 {
     Byte    byte;
 {
-    register BufPtr    bp = (BufPtr) buf;
 
     if (bp->outPtr != bp->buffer) {
 
     if (bp->outPtr != bp->buffer) {
-       bp->outPtr -= 1;
+       bp->outPtr--;
        *bp->outPtr = byte;
     } else if (bp->outPtr == bp->inPtr) {
        *bp->inPtr = byte;
        *bp->outPtr = byte;
     } else if (bp->outPtr == bp->inPtr) {
        *bp->inPtr = byte;
-       bp->inPtr += 1;
+       bp->inPtr++;
+       bp->left--;
        *bp->inPtr = 0;
     } else {
        /*
        *bp->inPtr = 0;
     } else {
        /*
@@ -156,13 +150,14 @@ Buf_UngetByte (buf, byte)
        Byte      *newBuf;
 
        newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
        Byte      *newBuf;
 
        newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
-       bcopy ((Address)bp->outPtr,
-                       (Address)(newBuf+BUF_UNGET_INC), numBytes+1);
+       bcopy ((char *)bp->outPtr,
+                       (char *)(newBuf+BUF_UNGET_INC), numBytes+1);
        bp->outPtr = newBuf + BUF_UNGET_INC;
        bp->inPtr = bp->outPtr + numBytes;
        bp->outPtr = newBuf + BUF_UNGET_INC;
        bp->inPtr = bp->outPtr + numBytes;
-       free ((Address)bp->buffer);
+       free ((char *)bp->buffer);
        bp->buffer = newBuf;
        bp->size += BUF_UNGET_INC;
        bp->buffer = newBuf;
        bp->size += BUF_UNGET_INC;
+       bp->left = bp->size - (bp->inPtr - bp->buffer);
        bp->outPtr -= 1;
        *bp->outPtr = byte;
     }
        bp->outPtr -= 1;
        *bp->outPtr = byte;
     }
@@ -182,32 +177,32 @@ Buf_UngetByte (buf, byte)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_UngetBytes (buf, numBytes, bytesPtr)
-    Buffer  buf;
+Buf_UngetBytes (bp, numBytes, bytesPtr)
+    register Buffer bp;
     int            numBytes;
     Byte    *bytesPtr;
 {
     int            numBytes;
     Byte    *bytesPtr;
 {
-    register BufPtr    bp = (BufPtr) buf;
 
     if (bp->outPtr - bp->buffer >= numBytes) {
        bp->outPtr -= numBytes;
        bcopy (bytesPtr, bp->outPtr, numBytes);
     } else if (bp->outPtr == bp->inPtr) {
 
     if (bp->outPtr - bp->buffer >= numBytes) {
        bp->outPtr -= numBytes;
        bcopy (bytesPtr, bp->outPtr, numBytes);
     } else if (bp->outPtr == bp->inPtr) {
-       Buf_AddBytes (buf, numBytes, bytesPtr);
+       Buf_AddBytes (bp, numBytes, bytesPtr);
     } else {
        int       curNumBytes = bp->inPtr - bp->outPtr;
        Byte      *newBuf;
        int       newBytes = max(numBytes,BUF_UNGET_INC);
 
        newBuf = (Byte *)emalloc (bp->size + newBytes);
     } else {
        int       curNumBytes = bp->inPtr - bp->outPtr;
        Byte      *newBuf;
        int       newBytes = max(numBytes,BUF_UNGET_INC);
 
        newBuf = (Byte *)emalloc (bp->size + newBytes);
-       bcopy((Address)bp->outPtr, (Address)(newBuf+newBytes), curNumBytes+1);
+       bcopy((char *)bp->outPtr, (char *)(newBuf+newBytes), curNumBytes+1);
        bp->outPtr = newBuf + newBytes;
        bp->inPtr = bp->outPtr + curNumBytes;
        bp->outPtr = newBuf + newBytes;
        bp->inPtr = bp->outPtr + curNumBytes;
-       free ((Address)bp->buffer);
+       free ((char *)bp->buffer);
        bp->buffer = newBuf;
        bp->size += newBytes;
        bp->buffer = newBuf;
        bp->size += newBytes;
+       bp->left = bp->size - (bp->inPtr - bp->buffer);
        bp->outPtr -= numBytes;
        bp->outPtr -= numBytes;
-       bcopy ((Address)bytesPtr, (Address)bp->outPtr, numBytes);
+       bcopy ((char *)bytesPtr, (char *)bp->outPtr, numBytes);
     }
 }
 \f
     }
 }
 \f
@@ -227,10 +222,9 @@ Buf_UngetBytes (buf, numBytes, bytesPtr)
  *-----------------------------------------------------------------------
  */
 int
  *-----------------------------------------------------------------------
  */
 int
-Buf_GetByte (buf)
-    Buffer  buf;
+Buf_GetByte (bp)
+    register Buffer bp;
 {
 {
-    BufPtr  bp = (BufPtr) buf;
     int            res;
 
     if (bp->inPtr == bp->outPtr) {
     int            res;
 
     if (bp->inPtr == bp->outPtr) {
@@ -240,7 +234,8 @@ Buf_GetByte (buf)
        bp->outPtr += 1;
        if (bp->outPtr == bp->inPtr) {
            bp->outPtr = bp->inPtr = bp->buffer;
        bp->outPtr += 1;
        if (bp->outPtr == bp->inPtr) {
            bp->outPtr = bp->inPtr = bp->buffer;
-           bp->inPtr = 0;
+           bp->left = bp->size;
+           *bp->inPtr = 0;
        }
        return (res);
     }
        }
        return (res);
     }
@@ -260,12 +255,11 @@ Buf_GetByte (buf)
  *-----------------------------------------------------------------------
  */
 int
  *-----------------------------------------------------------------------
  */
 int
-Buf_GetBytes (buf, numBytes, bytesPtr)
-    Buffer  buf;
+Buf_GetBytes (bp, numBytes, bytesPtr)
+    register Buffer bp;
     int            numBytes;
     Byte    *bytesPtr;
 {
     int            numBytes;
     Byte    *bytesPtr;
 {
-    BufPtr  bp = (BufPtr) buf;
     
     if (bp->inPtr - bp->outPtr < numBytes) {
        numBytes = bp->inPtr - bp->outPtr;
     
     if (bp->inPtr - bp->outPtr < numBytes) {
        numBytes = bp->inPtr - bp->outPtr;
@@ -275,6 +269,7 @@ Buf_GetBytes (buf, numBytes, bytesPtr)
 
     if (bp->outPtr == bp->inPtr) {
        bp->outPtr = bp->inPtr = bp->buffer;
 
     if (bp->outPtr == bp->inPtr) {
        bp->outPtr = bp->inPtr = bp->buffer;
+       bp->left = bp->size;
        *bp->inPtr = 0;
     }
     return (numBytes);
        *bp->inPtr = 0;
     }
     return (numBytes);
@@ -294,11 +289,10 @@ Buf_GetBytes (buf, numBytes, bytesPtr)
  *-----------------------------------------------------------------------
  */
 Byte *
  *-----------------------------------------------------------------------
  */
 Byte *
-Buf_GetAll (buf, numBytesPtr)
-    Buffer  buf;
+Buf_GetAll (bp, numBytesPtr)
+    register Buffer bp;
     int            *numBytesPtr;
 {
     int            *numBytesPtr;
 {
-    BufPtr  bp = (BufPtr)buf;
 
     if (numBytesPtr != (int *)NULL) {
        *numBytesPtr = bp->inPtr - bp->outPtr;
 
     if (numBytesPtr != (int *)NULL) {
        *numBytesPtr = bp->inPtr - bp->outPtr;
@@ -321,14 +315,14 @@ Buf_GetAll (buf, numBytesPtr)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_Discard (buf, numBytes)
-    Buffer  buf;
+Buf_Discard (bp, numBytes)
+    register Buffer bp;
     int            numBytes;
 {
     int            numBytes;
 {
-    register BufPtr    bp = (BufPtr) buf;
 
     if (bp->inPtr - bp->outPtr <= numBytes) {
        bp->inPtr = bp->outPtr = bp->buffer;
 
     if (bp->inPtr - bp->outPtr <= numBytes) {
        bp->inPtr = bp->outPtr = bp->buffer;
+       bp->left = bp->size;
        *bp->inPtr = 0;
     } else {
        bp->outPtr += numBytes;
        *bp->inPtr = 0;
     } else {
        bp->outPtr += numBytes;
@@ -353,7 +347,7 @@ int
 Buf_Size (buf)
     Buffer  buf;
 {
 Buf_Size (buf)
     Buffer  buf;
 {
-    return (((BufPtr)buf)->inPtr - ((BufPtr)buf)->outPtr);
+    return (buf->inPtr - buf->outPtr);
 }
 \f
 /*-
 }
 \f
 /*-
@@ -375,19 +369,19 @@ Buffer
 Buf_Init (size)
     int            size;       /* Initial size for the buffer */
 {
 Buf_Init (size)
     int            size;       /* Initial size for the buffer */
 {
-    BufPtr  bp;                /* New Buffer */
+    Buffer bp;         /* New Buffer */
 
 
-    bp = (Buf *)emalloc(sizeof(Buf));
+    bp = (Buffer)emalloc(sizeof(*bp));
 
     if (size <= 0) {
        size = BUF_DEF_SIZE;
     }
 
     if (size <= 0) {
        size = BUF_DEF_SIZE;
     }
-    bp->size = size;
-    bp->buffer = (Byte *)emalloc (size);
+    bp->left = bp->size = size;
+    bp->buffer = (Byte *)emalloc(size);
     bp->inPtr = bp->outPtr = bp->buffer;
     *bp->inPtr = 0;
 
     bp->inPtr = bp->outPtr = bp->buffer;
     *bp->inPtr = 0;
 
-    return ((Buffer) bp);
+    return (bp);
 }
 \f
 /*-
 }
 \f
 /*-
@@ -408,10 +402,9 @@ Buf_Destroy (buf, freeData)
     Buffer  buf;       /* Buffer to destroy */
     Boolean freeData;  /* TRUE if the data should be destroyed as well */
 {
     Buffer  buf;       /* Buffer to destroy */
     Boolean freeData;  /* TRUE if the data should be destroyed as well */
 {
-    BufPtr  bp = (BufPtr) buf;
     
     if (freeData) {
     
     if (freeData) {
-       free ((Address)bp->buffer);
+       free ((char *)buf->buffer);
     }
     }
-    free ((Address)bp);
+    free ((char *)buf);
 }
 }