buf.c revision 1.27 1 1.27 rillig /* $NetBSD: buf.c,v 1.27 2020/07/26 13:39:30 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.27 rillig static char rcsid[] = "$NetBSD: buf.c,v 1.27 2020/07/26 13:39:30 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.27 rillig __RCSID("$NetBSD: buf.c,v 1.27 2020/07/26 13:39:30 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.1 cgd /*-
145 1.1 cgd *-----------------------------------------------------------------------
146 1.27 rillig * Buf_AddInt --
147 1.27 rillig * Add the given number to the buffer.
148 1.27 rillig *
149 1.27 rillig *-----------------------------------------------------------------------
150 1.27 rillig */
151 1.27 rillig void
152 1.27 rillig Buf_AddInt(Buffer *bp, int n)
153 1.27 rillig {
154 1.27 rillig /*
155 1.27 rillig * We need enough space for the decimal representation of an int.
156 1.27 rillig * We calculate the space needed for the octal representation, and
157 1.27 rillig * add enough slop to cope with a '-' sign and a trailing '\0'.
158 1.27 rillig */
159 1.27 rillig size_t bits = sizeof(int) * CHAR_BIT;
160 1.27 rillig char buf[1 + (bits + 2) / 3 + 1];
161 1.27 rillig
162 1.27 rillig int len = snprintf(buf, sizeof buf, "%d", n);
163 1.27 rillig Buf_AddBytes(bp, len, buf);
164 1.27 rillig }
165 1.27 rillig
166 1.27 rillig /*-
167 1.27 rillig *-----------------------------------------------------------------------
168 1.1 cgd * Buf_GetAll --
169 1.1 cgd * Get all the available data at once.
170 1.1 cgd *
171 1.1 cgd * Results:
172 1.1 cgd * A pointer to the data and the number of bytes available.
173 1.1 cgd *
174 1.1 cgd * Side Effects:
175 1.1 cgd * None.
176 1.1 cgd *
177 1.1 cgd *-----------------------------------------------------------------------
178 1.1 cgd */
179 1.1 cgd Byte *
180 1.24 dsl Buf_GetAll(Buffer *bp, int *numBytesPtr)
181 1.1 cgd {
182 1.1 cgd
183 1.24 dsl if (numBytesPtr != NULL)
184 1.24 dsl *numBytesPtr = bp->count;
185 1.8 christos
186 1.26 rillig return bp->buffer;
187 1.1 cgd }
188 1.24 dsl
189 1.1 cgd /*-
190 1.1 cgd *-----------------------------------------------------------------------
191 1.23 dsl * Buf_Empty --
192 1.1 cgd * Throw away bytes in a buffer.
193 1.1 cgd *
194 1.1 cgd * Results:
195 1.1 cgd * None.
196 1.1 cgd *
197 1.1 cgd * Side Effects:
198 1.8 christos * The bytes are discarded.
199 1.1 cgd *
200 1.1 cgd *-----------------------------------------------------------------------
201 1.1 cgd */
202 1.1 cgd void
203 1.24 dsl Buf_Empty(Buffer *bp)
204 1.1 cgd {
205 1.1 cgd
206 1.24 dsl bp->count = 0;
207 1.24 dsl *bp->buffer = 0;
208 1.1 cgd }
209 1.24 dsl
210 1.1 cgd /*-
211 1.1 cgd *-----------------------------------------------------------------------
212 1.1 cgd * Buf_Init --
213 1.1 cgd * Initialize a buffer. If no initial size is given, a reasonable
214 1.1 cgd * default is used.
215 1.1 cgd *
216 1.13 wiz * Input:
217 1.13 wiz * size Initial size for the buffer
218 1.13 wiz *
219 1.1 cgd * Results:
220 1.1 cgd * A buffer to be given to other functions in this library.
221 1.1 cgd *
222 1.1 cgd * Side Effects:
223 1.1 cgd * The buffer is created, the space allocated and pointers
224 1.1 cgd * initialized.
225 1.1 cgd *
226 1.1 cgd *-----------------------------------------------------------------------
227 1.1 cgd */
228 1.24 dsl void
229 1.24 dsl Buf_Init(Buffer *bp, int size)
230 1.1 cgd {
231 1.21 christos if (size <= 0) {
232 1.1 cgd size = BUF_DEF_SIZE;
233 1.1 cgd }
234 1.24 dsl bp->size = size;
235 1.24 dsl bp->count = 0;
236 1.22 joerg bp->buffer = bmake_malloc(size);
237 1.24 dsl *bp->buffer = 0;
238 1.24 dsl }
239 1.1 cgd
240 1.1 cgd /*-
241 1.1 cgd *-----------------------------------------------------------------------
242 1.1 cgd * Buf_Destroy --
243 1.1 cgd * Nuke a buffer and all its resources.
244 1.1 cgd *
245 1.13 wiz * Input:
246 1.13 wiz * buf Buffer to destroy
247 1.13 wiz * freeData TRUE if the data should be destroyed
248 1.13 wiz *
249 1.1 cgd * Results:
250 1.24 dsl * Data buffer, NULL if freed
251 1.1 cgd *
252 1.1 cgd * Side Effects:
253 1.1 cgd * The buffer is freed.
254 1.1 cgd *
255 1.1 cgd *-----------------------------------------------------------------------
256 1.1 cgd */
257 1.24 dsl Byte *
258 1.24 dsl Buf_Destroy(Buffer *buf, Boolean freeData)
259 1.1 cgd {
260 1.24 dsl Byte *data;
261 1.8 christos
262 1.24 dsl data = buf->buffer;
263 1.1 cgd if (freeData) {
264 1.24 dsl free(data);
265 1.24 dsl data = NULL;
266 1.1 cgd }
267 1.24 dsl
268 1.24 dsl buf->size = 0;
269 1.24 dsl buf->count = 0;
270 1.24 dsl buf->buffer = NULL;
271 1.24 dsl
272 1.24 dsl return data;
273 1.1 cgd }
274 1.25 sjg
275 1.25 sjg
276 1.25 sjg /*-
277 1.25 sjg *-----------------------------------------------------------------------
278 1.25 sjg * Buf_DestroyCompact --
279 1.25 sjg * Nuke a buffer and return its data.
280 1.25 sjg *
281 1.25 sjg * Input:
282 1.25 sjg * buf Buffer to destroy
283 1.25 sjg *
284 1.25 sjg * Results:
285 1.25 sjg * Data buffer
286 1.25 sjg *
287 1.25 sjg * Side Effects:
288 1.25 sjg * If the buffer size is much greater than its content,
289 1.25 sjg * a new buffer will be allocated and the old one freed.
290 1.25 sjg *
291 1.25 sjg *-----------------------------------------------------------------------
292 1.25 sjg */
293 1.25 sjg #ifndef BUF_COMPACT_LIMIT
294 1.25 sjg # define BUF_COMPACT_LIMIT 128 /* worthwhile saving */
295 1.25 sjg #endif
296 1.25 sjg
297 1.25 sjg Byte *
298 1.25 sjg Buf_DestroyCompact(Buffer *buf)
299 1.25 sjg {
300 1.25 sjg #if BUF_COMPACT_LIMIT > 0
301 1.25 sjg Byte *data;
302 1.25 sjg
303 1.25 sjg if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
304 1.25 sjg /* We trust realloc to be smart */
305 1.25 sjg data = bmake_realloc(buf->buffer, buf->count + 1);
306 1.25 sjg if (data) {
307 1.25 sjg data[buf->count] = 0;
308 1.25 sjg Buf_Destroy(buf, FALSE);
309 1.25 sjg return data;
310 1.25 sjg }
311 1.25 sjg }
312 1.25 sjg #endif
313 1.25 sjg return Buf_Destroy(buf, FALSE);
314 1.25 sjg }
315