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