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