1/* 2 File: high_water_alloc.c 3 4 Copyright (C) 2001-2002 Silicon Graphics, Inc. All Rights Reserved. 5 6 This program is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Library General Public 8 License as published by the Free Software Foundation; either 9 version 2 of the License, or (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Library General Public License for more details. 15 16 You should have received a copy of the GNU Library General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19*/ 20 21#include <stdio.h> 22#include <stdlib.h> 23#include "misc.h" 24 25int high_water_alloc(void **buf, size_t *bufsize, size_t newsize) 26{ 27#define CHUNK_SIZE 256 28 /* 29 * Goal here is to avoid unnecessary memory allocations by 30 * using static buffers which only grow when necessary. 31 * Size is increased in fixed size chunks (CHUNK_SIZE). 32 */ 33 if (*bufsize < newsize) { 34 void *newbuf; 35 36 newsize = (newsize + CHUNK_SIZE-1) & ~(CHUNK_SIZE-1); 37 newbuf = realloc(*buf, newsize); 38 if (!newbuf) 39 return 1; 40 41 *buf = newbuf; 42 *bufsize = newsize; 43 } 44 return 0; 45} 46