1 /* $NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $ */
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
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.
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
73 static char rcsid
[] = "$NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $";
75 #include <sys/cdefs.h>
78 static char sccsid
[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
80 __RCSID("$NetBSD: buf.c,v 1.23 2008/12/20 18:08:24 dsl Exp $");
87 * Functions for automatically-expanded buffers.
94 #define max(a,b) ((a) > (b) ? (a) : (b))
97 #define BUF_DEF_SIZE 256 /* Default buffer size */
100 *-----------------------------------------------------------------------
102 * Extend buffer for single byte add.
104 *-----------------------------------------------------------------------
107 Buf_Expand_1(Buffer
*bp
)
109 bp
->size
+= max(bp
->size
, 16);
110 bp
->buffer
= bmake_realloc(bp
->buffer
, bp
->size
);
114 *-----------------------------------------------------------------------
116 * Add a number of bytes to the buffer.
124 *-----------------------------------------------------------------------
127 Buf_AddBytes(Buffer
*bp
, int numBytes
, const Byte
*bytesPtr
)
129 int count
= bp
->count
;
132 if (__predict_false(count
+ numBytes
>= bp
->size
)) {
133 bp
->size
+= max(bp
->size
, numBytes
+ 16);
134 bp
->buffer
= bmake_realloc(bp
->buffer
, bp
->size
);
137 ptr
= bp
->buffer
+ count
;
138 bp
->count
= count
+ numBytes
;
140 memcpy(ptr
, bytesPtr
, numBytes
);
144 *-----------------------------------------------------------------------
146 * Get all the available data at once.
149 * A pointer to the data and the number of bytes available.
154 *-----------------------------------------------------------------------
157 Buf_GetAll(Buffer
*bp
, int *numBytesPtr
)
160 if (numBytesPtr
!= NULL
)
161 *numBytesPtr
= bp
->count
;
167 *-----------------------------------------------------------------------
169 * Throw away bytes in a buffer.
175 * The bytes are discarded.
177 *-----------------------------------------------------------------------
180 Buf_Empty(Buffer
*bp
)
188 *-----------------------------------------------------------------------
190 * Initialize a buffer. If no initial size is given, a reasonable
194 * size Initial size for the buffer
197 * A buffer to be given to other functions in this library.
200 * The buffer is created, the space allocated and pointers
203 *-----------------------------------------------------------------------
206 Buf_Init(Buffer
*bp
, int size
)
213 bp
->buffer
= bmake_malloc(size
);
218 *-----------------------------------------------------------------------
220 * Nuke a buffer and all its resources.
223 * buf Buffer to destroy
224 * freeData TRUE if the data should be destroyed
227 * Data buffer, NULL if freed
230 * The buffer is freed.
232 *-----------------------------------------------------------------------
235 Buf_Destroy(Buffer
*buf
, Boolean freeData
)