xref: /netbsd-src/usr.bin/make/buf.c (revision 4d342c046e3288fb5a1edcd33cfec48c41c80664)
1 /*	$NetBSD: buf.c,v 1.38 2020/09/13 15:15:51 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1988, 1989 by Adam de Boor
37  * Copyright (c) 1989 by Berkeley Softworks
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Adam de Boor.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
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.
58  *
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
69  * SUCH DAMAGE.
70  */
71 
72 /* Functions for automatically-expanded null-terminated buffers. */
73 
74 #include <limits.h>
75 #include "make.h"
76 
77 /*	"@(#)buf.c	8.1 (Berkeley) 6/6/93"	*/
78 MAKE_RCSID("$NetBSD: buf.c,v 1.38 2020/09/13 15:15:51 rillig Exp $");
79 
80 /* Extend the buffer for adding a single byte. */
81 void
82 Buf_Expand_1(Buffer *bp)
83 {
84     bp->size += MAX(bp->size, 16);
85     bp->buffer = bmake_realloc(bp->buffer, bp->size);
86 }
87 
88 /* Add the given bytes to the buffer. */
89 void
90 Buf_AddBytes(Buffer *bp, const char *bytesPtr, size_t numBytes)
91 {
92     size_t count = bp->count;
93     char *ptr;
94 
95     if (__predict_false(count + numBytes >= bp->size)) {
96 	bp->size += MAX(bp->size, numBytes + 16);
97 	bp->buffer = bmake_realloc(bp->buffer, bp->size);
98     }
99 
100     ptr = bp->buffer + count;
101     bp->count = count + numBytes;
102     memcpy(ptr, bytesPtr, numBytes);
103     ptr[numBytes] = '\0';
104 }
105 
106 /* Add the bytes between start and end to the buffer. */
107 void
108 Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
109 {
110     Buf_AddBytes(bp, start, (size_t)(end - start));
111 }
112 
113 /* Add the given string to the buffer. */
114 void
115 Buf_AddStr(Buffer *bp, const char *str)
116 {
117     Buf_AddBytes(bp, str, strlen(str));
118 }
119 
120 /* Add the given number to the buffer. */
121 void
122 Buf_AddInt(Buffer *bp, int n)
123 {
124     enum {
125 	bits = sizeof(int) * CHAR_BIT,
126 	max_octal_digits = (bits + 2) / 3,
127 	max_decimal_digits = /* at most */ max_octal_digits,
128 	max_sign_chars = 1,
129 	buf_size = max_sign_chars + max_decimal_digits + 1
130     };
131     char buf[buf_size];
132 
133     size_t len = (size_t)snprintf(buf, sizeof buf, "%d", n);
134     Buf_AddBytes(bp, buf, len);
135 }
136 
137 /* Get the data (usually a string) from the buffer.
138  * The returned data is valid until the next modifying operation
139  * on the buffer.
140  *
141  * Returns the pointer to the data and optionally the length of the
142  * data in the buffer. */
143 char *
144 Buf_GetAll(Buffer *bp, size_t *numBytesPtr)
145 {
146     if (numBytesPtr != NULL)
147 	*numBytesPtr = bp->count;
148     return bp->buffer;
149 }
150 
151 /* Mark the buffer as empty, so it can be filled with data again. */
152 void
153 Buf_Empty(Buffer *bp)
154 {
155     bp->count = 0;
156     bp->buffer[0] = '\0';
157 }
158 
159 /* Initialize a buffer.
160  * If the given initial size is 0, a reasonable default is used. */
161 void
162 Buf_Init(Buffer *bp, size_t size)
163 {
164     if (size <= 0) {
165 	size = 256;
166     }
167     bp->size = size;
168     bp->count = 0;
169     bp->buffer = bmake_malloc(size);
170     bp->buffer[0] = '\0';
171 }
172 
173 /* Reset the buffer.
174  * If freeData is TRUE, the data from the buffer is freed as well.
175  * Otherwise it is kept and returned. */
176 char *
177 Buf_Destroy(Buffer *buf, Boolean freeData)
178 {
179     char *data = buf->buffer;
180     if (freeData) {
181 	free(data);
182 	data = NULL;
183     }
184 
185     buf->size = 0;
186     buf->count = 0;
187     buf->buffer = NULL;
188 
189     return data;
190 }
191 
192 #ifndef BUF_COMPACT_LIMIT
193 # define BUF_COMPACT_LIMIT 128		/* worthwhile saving */
194 #endif
195 
196 /* Reset the buffer and return its data.
197  *
198  * If the buffer size is much greater than its content,
199  * a new buffer will be allocated and the old one freed. */
200 char *
201 Buf_DestroyCompact(Buffer *buf)
202 {
203 #if BUF_COMPACT_LIMIT > 0
204     if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
205 	/* We trust realloc to be smart */
206 	char *data = bmake_realloc(buf->buffer, buf->count + 1);
207 	data[buf->count] = '\0';
208 	Buf_Destroy(buf, FALSE);
209 	return data;
210     }
211 #endif
212     return Buf_Destroy(buf, FALSE);
213 }
214