11844Swollman/* $NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $ */ 250476Speter 323549Swosch/* 423549Swosch * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 535784Swosch * All rights reserved. 623549Swosch * 723549Swosch * This code is derived from software contributed to Berkeley by 823549Swosch * Adam de Boor. 923549Swosch * 1023549Swosch * Redistribution and use in source and binary forms, with or without 1123549Swosch * modification, are permitted provided that the following conditions 1223549Swosch * are met: 1323549Swosch * 1. Redistributions of source code must retain the above copyright 1423549Swosch * notice, this list of conditions and the following disclaimer. 1523549Swosch * 2. Redistributions in binary form must reproduce the above copyright 1623549Swosch * notice, this list of conditions and the following disclaimer in the 1723549Swosch * documentation and/or other materials provided with the distribution. 1823549Swosch * 3. Neither the name of the University nor the names of its contributors 1923549Swosch * may be used to endorse or promote products derived from this software 2023549Swosch * without specific prior written permission. 2134934Seivind * 2234934Seivind * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2334934Seivind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2434934Seivind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2534934Seivind * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2634934Seivind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2734934Seivind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2823549Swosch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2923549Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3023549Swosch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3123549Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3223549Swosch * SUCH DAMAGE. 3323549Swosch */ 3423549Swosch 3523549Swosch/* 3644922Sbde * Copyright (c) 1988, 1989 by Adam de Boor 3723549Swosch * Copyright (c) 1989 by Berkeley Softworks 381638Srgrimes * All rights reserved. 3938183Speter * 4038183Speter * This code is derived from software contributed to Berkeley by 4138183Speter * Adam de Boor. 4238183Speter * 4338183Speter * Redistribution and use in source and binary forms, with or without 4438183Speter * modification, are permitted provided that the following conditions 4523549Swosch * are met: 461638Srgrimes * 1. Redistributions of source code must retain the above copyright 471638Srgrimes * notice, this list of conditions and the following disclaimer. 4834934Seivind * 2. Redistributions in binary form must reproduce the above copyright 4934934Seivind * notice, this list of conditions and the following disclaimer in the 5034934Seivind * documentation and/or other materials provided with the distribution. 5134934Seivind * 3. All advertising materials mentioning features or use of this software 5234934Seivind * must display the following acknowledgement: 5334934Seivind * This product includes software developed by the University of 5434934Seivind * California, Berkeley and its contributors. 5534934Seivind * 4. Neither the name of the University nor the names of its contributors 5634934Seivind * may be used to endorse or promote products derived from this software 5734934Seivind * without specific prior written permission. 581638Srgrimes * 5934934Seivind * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 6034934Seivind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 6134934Seivind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 6234934Seivind * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 6334934Seivind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 6453152Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 6534934Seivind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6653152Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6753152Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6834934Seivind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6934934Seivind * SUCH DAMAGE. 7034934Seivind */ 7134934Seivind 7234934Seivind#ifndef MAKE_NATIVE 7334934Seivindstatic char rcsid[] = "$NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $"; 7434934Seivind#else 7534934Seivind#include <sys/cdefs.h> 7634934Seivind#ifndef lint 7734934Seivind#if 0 7834934Seivindstatic char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93"; 791638Srgrimes#else 801638Srgrimes__RCSID("$NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $"); 811638Srgrimes#endif 8253152Smarcel#endif /* not lint */ 8353152Smarcel#endif 841638Srgrimes 851638Srgrimes/*- 861638Srgrimes * buf.c -- 871638Srgrimes * Functions for automatically-expanded buffers. 881638Srgrimes */ 891638Srgrimes 9034892Seivind#include "make.h" 9144922Sbde#include "buf.h" 9231177Swosch 9315061Swosch#ifndef max 941638Srgrimes#define max(a,b) ((a) > (b) ? (a) : (b)) 9515061Swosch#endif 961638Srgrimes 971638Srgrimes#define BUF_DEF_SIZE 256 /* Default buffer size */ 981638Srgrimes 991638Srgrimes/*- 1001638Srgrimes *----------------------------------------------------------------------- 1011638Srgrimes * Buf_Expand_1 -- 1021638Srgrimes * Extend buffer for single byte add. 1031638Srgrimes * 1041638Srgrimes *----------------------------------------------------------------------- 1051638Srgrimes */ 1061638Srgrimesvoid 1071638SrgrimesBuf_Expand_1(Buffer *bp) 1081638Srgrimes{ 10915061Swosch bp->size += max(bp->size, 16); 11015061Swosch bp->buffer = bmake_realloc(bp->buffer, bp->size); 11115061Swosch} 1121638Srgrimes 11315061Swosch/*- 11423549Swosch *----------------------------------------------------------------------- 11526760Sjkh * Buf_AddBytes -- 11626760Sjkh * Add a number of bytes to the buffer. 11726760Sjkh * 1181638Srgrimes * Results: 119 * None. 120 * 121 * Side Effects: 122 * Guess what? 123 * 124 *----------------------------------------------------------------------- 125 */ 126void 127Buf_AddBytes(Buffer *bp, int numBytes, const Byte *bytesPtr) 128{ 129 int count = bp->count; 130 Byte *ptr; 131 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); 135 } 136 137 ptr = bp->buffer + count; 138 bp->count = count + numBytes; 139 ptr[numBytes] = 0; 140 memcpy(ptr, bytesPtr, numBytes); 141} 142 143/*- 144 *----------------------------------------------------------------------- 145 * Buf_GetAll -- 146 * Get all the available data at once. 147 * 148 * Results: 149 * A pointer to the data and the number of bytes available. 150 * 151 * Side Effects: 152 * None. 153 * 154 *----------------------------------------------------------------------- 155 */ 156Byte * 157Buf_GetAll(Buffer *bp, int *numBytesPtr) 158{ 159 160 if (numBytesPtr != NULL) 161 *numBytesPtr = bp->count; 162 163 return (bp->buffer); 164} 165 166/*- 167 *----------------------------------------------------------------------- 168 * Buf_Empty -- 169 * Throw away bytes in a buffer. 170 * 171 * Results: 172 * None. 173 * 174 * Side Effects: 175 * The bytes are discarded. 176 * 177 *----------------------------------------------------------------------- 178 */ 179void 180Buf_Empty(Buffer *bp) 181{ 182 183 bp->count = 0; 184 *bp->buffer = 0; 185} 186 187/*- 188 *----------------------------------------------------------------------- 189 * Buf_Init -- 190 * Initialize a buffer. If no initial size is given, a reasonable 191 * default is used. 192 * 193 * Input: 194 * size Initial size for the buffer 195 * 196 * Results: 197 * A buffer to be given to other functions in this library. 198 * 199 * Side Effects: 200 * The buffer is created, the space allocated and pointers 201 * initialized. 202 * 203 *----------------------------------------------------------------------- 204 */ 205void 206Buf_Init(Buffer *bp, int size) 207{ 208 if (size <= 0) { 209 size = BUF_DEF_SIZE; 210 } 211 bp->size = size; 212 bp->count = 0; 213 bp->buffer = bmake_malloc(size); 214 *bp->buffer = 0; 215} 216 217/*- 218 *----------------------------------------------------------------------- 219 * Buf_Destroy -- 220 * Nuke a buffer and all its resources. 221 * 222 * Input: 223 * buf Buffer to destroy 224 * freeData TRUE if the data should be destroyed 225 * 226 * Results: 227 * Data buffer, NULL if freed 228 * 229 * Side Effects: 230 * The buffer is freed. 231 * 232 *----------------------------------------------------------------------- 233 */ 234Byte * 235Buf_Destroy(Buffer *buf, Boolean freeData) 236{ 237 Byte *data; 238 239 data = buf->buffer; 240 if (freeData) { 241 free(data); 242 data = NULL; 243 } 244 245 buf->size = 0; 246 buf->count = 0; 247 buf->buffer = NULL; 248 249 return data; 250} 251 252 253/*- 254 *----------------------------------------------------------------------- 255 * Buf_DestroyCompact -- 256 * Nuke a buffer and return its data. 257 * 258 * Input: 259 * buf Buffer to destroy 260 * 261 * Results: 262 * Data buffer 263 * 264 * Side Effects: 265 * If the buffer size is much greater than its content, 266 * a new buffer will be allocated and the old one freed. 267 * 268 *----------------------------------------------------------------------- 269 */ 270#ifndef BUF_COMPACT_LIMIT 271# define BUF_COMPACT_LIMIT 128 /* worthwhile saving */ 272#endif 273 274Byte * 275Buf_DestroyCompact(Buffer *buf) 276{ 277#if BUF_COMPACT_LIMIT > 0 278 Byte *data; 279 280 if (buf->size - buf->count >= BUF_COMPACT_LIMIT) { 281 /* We trust realloc to be smart */ 282 data = bmake_realloc(buf->buffer, buf->count + 1); 283 if (data) { 284 data[buf->count] = 0; 285 Buf_Destroy(buf, FALSE); 286 return data; 287 } 288 } 289#endif 290 return Buf_Destroy(buf, FALSE); 291} 292