Home | History | Annotate | Line # | Download | only in make
buf.c revision 1.31
      1 /*	$NetBSD: buf.c,v 1.31 2020/08/03 20:26:09 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1988, 1989 by Adam de Boor
     37  * Copyright (c) 1989 by Berkeley Softworks
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Adam de Boor.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  */
     71 
     72 #ifndef MAKE_NATIVE
     73 static char rcsid[] = "$NetBSD: buf.c,v 1.31 2020/08/03 20:26:09 rillig Exp $";
     74 #else
     75 #include <sys/cdefs.h>
     76 #ifndef lint
     77 #if 0
     78 static char sccsid[] = "@(#)buf.c	8.1 (Berkeley) 6/6/93";
     79 #else
     80 __RCSID("$NetBSD: buf.c,v 1.31 2020/08/03 20:26:09 rillig Exp $");
     81 #endif
     82 #endif /* not lint */
     83 #endif
     84 
     85 /* Functions for automatically-expanded NUL-terminated buffers. */
     86 
     87 #include <limits.h>
     88 #include "make.h"
     89 #include "buf.h"
     90 
     91 #ifndef max
     92 #define max(a, b)  ((a) > (b) ? (a) : (b))
     93 #endif
     94 
     95 #define BUF_DEF_SIZE	256 	/* Default buffer size */
     96 
     97 /* Extend the buffer for adding a single byte. */
     98 void
     99 Buf_Expand_1(Buffer *bp)
    100 {
    101     bp->size += max(bp->size, 16);
    102     bp->buffer = bmake_realloc(bp->buffer, bp->size);
    103 }
    104 
    105 /* Add the given bytes to the buffer. */
    106 void
    107 Buf_AddBytesZ(Buffer *bp, const Byte *bytesPtr, size_t numBytes)
    108 {
    109     size_t count = bp->count;
    110     Byte *ptr;
    111 
    112     if (__predict_false(count + numBytes >= bp->size)) {
    113 	bp->size += max(bp->size, numBytes + 16);
    114 	bp->buffer = bmake_realloc(bp->buffer, bp->size);
    115     }
    116 
    117     ptr = bp->buffer + count;
    118     bp->count = count + numBytes;
    119     memcpy(ptr, bytesPtr, numBytes);
    120     ptr[numBytes] = '\0';
    121 }
    122 
    123 /* Add the bytes between start and end to the buffer. */
    124 void
    125 Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
    126 {
    127     Buf_AddBytesZ(bp, start, (size_t)(end - start));
    128 }
    129 
    130 /* Add the given string to the buffer. */
    131 void
    132 Buf_AddStr(Buffer *bp, const char *str)
    133 {
    134     Buf_AddBytesZ(bp, str, strlen(str));
    135 }
    136 
    137 /* Add the given number to the buffer. */
    138 void
    139 Buf_AddInt(Buffer *bp, int n)
    140 {
    141     /*
    142      * We need enough space for the decimal representation of an int.
    143      * We calculate the space needed for the octal representation, and
    144      * add enough slop to cope with a '-' sign and a trailing '\0'.
    145      */
    146     size_t bits = sizeof(int) * CHAR_BIT;
    147     char buf[1 + (bits + 2) / 3 + 1];
    148 
    149     size_t len = (size_t)snprintf(buf, sizeof buf, "%d", n);
    150     Buf_AddBytesZ(bp, buf, len);
    151 }
    152 
    153 /* Get the data (usually a string) from the buffer.
    154  * The returned data is valid until the next modifying operation
    155  * on the buffer.
    156  *
    157  * Returns the pointer to the data and optionally the length of the
    158  * data in the buffer. */
    159 Byte *
    160 Buf_GetAllZ(Buffer *bp, size_t *numBytesPtr)
    161 {
    162     if (numBytesPtr != NULL)
    163 	*numBytesPtr = bp->count;
    164     return bp->buffer;
    165 }
    166 
    167 /* Mark the buffer as empty, so it can be filled with data again. */
    168 void
    169 Buf_Empty(Buffer *bp)
    170 {
    171     bp->count = 0;
    172     bp->buffer[0] = '\0';
    173 }
    174 
    175 /* Initialize a buffer.
    176  * If the given initial size is 0, a reasonable default is used. */
    177 void
    178 Buf_InitZ(Buffer *bp, size_t size)
    179 {
    180     if (size <= 0) {
    181 	size = BUF_DEF_SIZE;
    182     }
    183     bp->size = size;
    184     bp->count = 0;
    185     bp->buffer = bmake_malloc(size);
    186     *bp->buffer = 0;
    187 }
    188 
    189 /* Reset the buffer.
    190  * If freeData is TRUE, the data from the buffer is freed as well.
    191  * Otherwise it is kept and returned. */
    192 Byte *
    193 Buf_Destroy(Buffer *buf, Boolean freeData)
    194 {
    195     Byte *data = buf->buffer;
    196     if (freeData) {
    197 	free(data);
    198 	data = NULL;
    199     }
    200 
    201     buf->size = 0;
    202     buf->count = 0;
    203     buf->buffer = NULL;
    204 
    205     return data;
    206 }
    207 
    208 #ifndef BUF_COMPACT_LIMIT
    209 # define BUF_COMPACT_LIMIT 128          /* worthwhile saving */
    210 #endif
    211 
    212 /* Reset the buffer and return its data.
    213  *
    214  * If the buffer size is much greater than its content,
    215  * a new buffer will be allocated and the old one freed. */
    216 Byte *
    217 Buf_DestroyCompact(Buffer *buf)
    218 {
    219 #if BUF_COMPACT_LIMIT > 0
    220     if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
    221 	/* We trust realloc to be smart */
    222 	Byte *data = bmake_realloc(buf->buffer, buf->count + 1);
    223 	data[buf->count] = 0;
    224 	Buf_Destroy(buf, FALSE);
    225 	return data;
    226     }
    227 #endif
    228     return Buf_Destroy(buf, FALSE);
    229 }
    230