Home | History | Annotate | Line # | Download | only in make
buf.c revision 1.28
      1  1.28    rillig /*	$NetBSD: buf.c,v 1.28 2020/07/26 15:09:10 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.28    rillig static char rcsid[] = "$NetBSD: buf.c,v 1.28 2020/07/26 15:09:10 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.28    rillig __RCSID("$NetBSD: buf.c,v 1.28 2020/07/26 15:09:10 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.1       cgd /*-
     86   1.1       cgd  * buf.c --
     87  1.27    rillig  *	Functions for automatically-expanded NUL-terminated buffers.
     88   1.1       cgd  */
     89   1.1       cgd 
     90  1.27    rillig #include <limits.h>
     91  1.27    rillig #include "make.h"
     92  1.27    rillig #include "buf.h"
     93   1.1       cgd 
     94   1.1       cgd #ifndef max
     95   1.1       cgd #define max(a,b)  ((a) > (b) ? (a) : (b))
     96   1.1       cgd #endif
     97   1.1       cgd 
     98   1.1       cgd #define BUF_DEF_SIZE	256 	/* Default buffer size */
     99   1.1       cgd 
    100   1.1       cgd /*-
    101   1.1       cgd  *-----------------------------------------------------------------------
    102  1.24       dsl  * Buf_Expand_1 --
    103  1.24       dsl  *	Extend buffer for single byte add.
    104   1.1       cgd  *
    105   1.1       cgd  *-----------------------------------------------------------------------
    106   1.1       cgd  */
    107   1.1       cgd void
    108  1.24       dsl Buf_Expand_1(Buffer *bp)
    109   1.1       cgd {
    110  1.24       dsl     bp->size += max(bp->size, 16);
    111  1.24       dsl     bp->buffer = bmake_realloc(bp->buffer, bp->size);
    112   1.1       cgd }
    113  1.24       dsl 
    114   1.1       cgd /*-
    115   1.1       cgd  *-----------------------------------------------------------------------
    116   1.1       cgd  * Buf_AddBytes --
    117   1.1       cgd  *	Add a number of bytes to the buffer.
    118   1.1       cgd  *
    119   1.1       cgd  * Results:
    120   1.1       cgd  *	None.
    121   1.1       cgd  *
    122   1.1       cgd  * Side Effects:
    123   1.1       cgd  *	Guess what?
    124   1.1       cgd  *
    125   1.1       cgd  *-----------------------------------------------------------------------
    126   1.1       cgd  */
    127   1.1       cgd void
    128  1.24       dsl Buf_AddBytes(Buffer *bp, int numBytes, const Byte *bytesPtr)
    129   1.1       cgd {
    130  1.24       dsl     int count = bp->count;
    131  1.24       dsl     Byte *ptr;
    132   1.1       cgd 
    133  1.24       dsl     if (__predict_false(count + numBytes >= bp->size)) {
    134  1.24       dsl 	bp->size += max(bp->size, numBytes + 16);
    135  1.24       dsl 	bp->buffer = bmake_realloc(bp->buffer, bp->size);
    136  1.24       dsl     }
    137   1.1       cgd 
    138  1.24       dsl     ptr = bp->buffer + count;
    139  1.24       dsl     bp->count = count + numBytes;
    140  1.24       dsl     ptr[numBytes] = 0;
    141  1.24       dsl     memcpy(ptr, bytesPtr, numBytes);
    142   1.1       cgd }
    143  1.24       dsl 
    144  1.28    rillig void
    145  1.28    rillig Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
    146  1.28    rillig {
    147  1.28    rillig     Buf_AddBytes(bp, (int)(end - start), start);
    148  1.28    rillig }
    149  1.28    rillig 
    150  1.28    rillig void
    151  1.28    rillig Buf_AddStr(Buffer *bp, const char *str)
    152  1.28    rillig {
    153  1.28    rillig     Buf_AddBytes(bp, (int)strlen(str), str);
    154  1.28    rillig }
    155  1.28    rillig 
    156   1.1       cgd /*-
    157   1.1       cgd  *-----------------------------------------------------------------------
    158  1.27    rillig  * Buf_AddInt --
    159  1.27    rillig  *	Add the given number to the buffer.
    160  1.27    rillig  *
    161  1.27    rillig  *-----------------------------------------------------------------------
    162  1.27    rillig  */
    163  1.27    rillig void
    164  1.27    rillig Buf_AddInt(Buffer *bp, int n)
    165  1.27    rillig {
    166  1.27    rillig     /*
    167  1.27    rillig      * We need enough space for the decimal representation of an int.
    168  1.27    rillig      * We calculate the space needed for the octal representation, and
    169  1.27    rillig      * add enough slop to cope with a '-' sign and a trailing '\0'.
    170  1.27    rillig      */
    171  1.27    rillig     size_t bits = sizeof(int) * CHAR_BIT;
    172  1.27    rillig     char buf[1 + (bits + 2) / 3 + 1];
    173  1.27    rillig 
    174  1.27    rillig     int len = snprintf(buf, sizeof buf, "%d", n);
    175  1.27    rillig     Buf_AddBytes(bp, len, buf);
    176  1.27    rillig }
    177  1.27    rillig 
    178  1.27    rillig /*-
    179  1.27    rillig  *-----------------------------------------------------------------------
    180   1.1       cgd  * Buf_GetAll --
    181   1.1       cgd  *	Get all the available data at once.
    182   1.1       cgd  *
    183   1.1       cgd  * Results:
    184   1.1       cgd  *	A pointer to the data and the number of bytes available.
    185   1.1       cgd  *
    186   1.1       cgd  * Side Effects:
    187   1.1       cgd  *	None.
    188   1.1       cgd  *
    189   1.1       cgd  *-----------------------------------------------------------------------
    190   1.1       cgd  */
    191   1.1       cgd Byte *
    192  1.24       dsl Buf_GetAll(Buffer *bp, int *numBytesPtr)
    193   1.1       cgd {
    194   1.1       cgd 
    195  1.24       dsl     if (numBytesPtr != NULL)
    196  1.24       dsl 	*numBytesPtr = bp->count;
    197   1.8  christos 
    198  1.26    rillig     return bp->buffer;
    199   1.1       cgd }
    200  1.24       dsl 
    201   1.1       cgd /*-
    202   1.1       cgd  *-----------------------------------------------------------------------
    203  1.23       dsl  * Buf_Empty --
    204   1.1       cgd  *	Throw away bytes in a buffer.
    205   1.1       cgd  *
    206   1.1       cgd  * Results:
    207   1.1       cgd  *	None.
    208   1.1       cgd  *
    209   1.1       cgd  * Side Effects:
    210   1.8  christos  *	The bytes are discarded.
    211   1.1       cgd  *
    212   1.1       cgd  *-----------------------------------------------------------------------
    213   1.1       cgd  */
    214   1.1       cgd void
    215  1.24       dsl Buf_Empty(Buffer *bp)
    216   1.1       cgd {
    217   1.1       cgd 
    218  1.24       dsl     bp->count = 0;
    219  1.24       dsl     *bp->buffer = 0;
    220   1.1       cgd }
    221  1.24       dsl 
    222   1.1       cgd /*-
    223   1.1       cgd  *-----------------------------------------------------------------------
    224   1.1       cgd  * Buf_Init --
    225   1.1       cgd  *	Initialize a buffer. If no initial size is given, a reasonable
    226   1.1       cgd  *	default is used.
    227   1.1       cgd  *
    228  1.13       wiz  * Input:
    229  1.13       wiz  *	size		Initial size for the buffer
    230  1.13       wiz  *
    231   1.1       cgd  * Results:
    232   1.1       cgd  *	A buffer to be given to other functions in this library.
    233   1.1       cgd  *
    234   1.1       cgd  * Side Effects:
    235   1.1       cgd  *	The buffer is created, the space allocated and pointers
    236   1.1       cgd  *	initialized.
    237   1.1       cgd  *
    238   1.1       cgd  *-----------------------------------------------------------------------
    239   1.1       cgd  */
    240  1.24       dsl void
    241  1.24       dsl Buf_Init(Buffer *bp, int size)
    242   1.1       cgd {
    243  1.21  christos     if (size <= 0) {
    244   1.1       cgd 	size = BUF_DEF_SIZE;
    245   1.1       cgd     }
    246  1.24       dsl     bp->size = size;
    247  1.24       dsl     bp->count = 0;
    248  1.22     joerg     bp->buffer = bmake_malloc(size);
    249  1.24       dsl     *bp->buffer = 0;
    250  1.24       dsl }
    251   1.1       cgd 
    252   1.1       cgd /*-
    253   1.1       cgd  *-----------------------------------------------------------------------
    254   1.1       cgd  * Buf_Destroy --
    255   1.1       cgd  *	Nuke a buffer and all its resources.
    256   1.1       cgd  *
    257  1.13       wiz  * Input:
    258  1.13       wiz  *	buf		Buffer to destroy
    259  1.13       wiz  *	freeData	TRUE if the data should be destroyed
    260  1.13       wiz  *
    261   1.1       cgd  * Results:
    262  1.24       dsl  *	Data buffer, NULL if freed
    263   1.1       cgd  *
    264   1.1       cgd  * Side Effects:
    265   1.1       cgd  *	The buffer is freed.
    266   1.1       cgd  *
    267   1.1       cgd  *-----------------------------------------------------------------------
    268   1.1       cgd  */
    269  1.24       dsl Byte *
    270  1.24       dsl Buf_Destroy(Buffer *buf, Boolean freeData)
    271   1.1       cgd {
    272  1.24       dsl     Byte *data;
    273   1.8  christos 
    274  1.24       dsl     data = buf->buffer;
    275   1.1       cgd     if (freeData) {
    276  1.24       dsl 	free(data);
    277  1.24       dsl 	data = NULL;
    278   1.1       cgd     }
    279  1.24       dsl 
    280  1.24       dsl     buf->size = 0;
    281  1.24       dsl     buf->count = 0;
    282  1.24       dsl     buf->buffer = NULL;
    283  1.24       dsl 
    284  1.24       dsl     return data;
    285   1.1       cgd }
    286  1.25       sjg 
    287  1.25       sjg 
    288  1.25       sjg /*-
    289  1.25       sjg  *-----------------------------------------------------------------------
    290  1.25       sjg  * Buf_DestroyCompact --
    291  1.25       sjg  *	Nuke a buffer and return its data.
    292  1.25       sjg  *
    293  1.25       sjg  * Input:
    294  1.25       sjg  *	buf		Buffer to destroy
    295  1.25       sjg  *
    296  1.25       sjg  * Results:
    297  1.25       sjg  *	Data buffer
    298  1.25       sjg  *
    299  1.25       sjg  * Side Effects:
    300  1.25       sjg  *	If the buffer size is much greater than its content,
    301  1.25       sjg  *	a new buffer will be allocated and the old one freed.
    302  1.25       sjg  *
    303  1.25       sjg  *-----------------------------------------------------------------------
    304  1.25       sjg  */
    305  1.25       sjg #ifndef BUF_COMPACT_LIMIT
    306  1.25       sjg # define BUF_COMPACT_LIMIT 128          /* worthwhile saving */
    307  1.25       sjg #endif
    308  1.25       sjg 
    309  1.25       sjg Byte *
    310  1.25       sjg Buf_DestroyCompact(Buffer *buf)
    311  1.25       sjg {
    312  1.25       sjg #if BUF_COMPACT_LIMIT > 0
    313  1.25       sjg     Byte *data;
    314  1.25       sjg 
    315  1.25       sjg     if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
    316  1.25       sjg 	/* We trust realloc to be smart */
    317  1.25       sjg 	data = bmake_realloc(buf->buffer, buf->count + 1);
    318  1.25       sjg 	if (data) {
    319  1.25       sjg 	    data[buf->count] = 0;
    320  1.25       sjg 	    Buf_Destroy(buf, FALSE);
    321  1.25       sjg 	    return data;
    322  1.25       sjg 	}
    323  1.25       sjg     }
    324  1.25       sjg #endif
    325  1.25       sjg     return Buf_Destroy(buf, FALSE);
    326  1.25       sjg }
    327