(root)/
acl-2.3.1/
libmisc/
high_water_alloc.c
       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 modify it under
       7    the terms of the GNU Lesser General Public License as published by the
       8    Free Software Foundation; either version 2.1 of the License, or (at
       9    your option) any later version.
      10  
      11    This program is distributed in the hope that it will be useful, but WITHOUT
      12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
      14    License for more details.
      15  
      16    You should have received a copy of the GNU Lesser General Public
      17    License along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18  */
      19  
      20  #include "config.h"
      21  #include <stdio.h>
      22  #include <stdlib.h>
      23  #include "misc.h"
      24  
      25  int __acl_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  }