1 /* extendbuf.c -- manage a dynamically-allocated buffer
3 Copyright 2004 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by James Yougnman <jay@gnu.org>. */
30 #include "extendbuf.h"
33 /* We initially use a small default size to ensure that this code
37 # define SIZE_DEFAULT 16
41 decide_size(size_t current
, size_t wanted
)
46 newsize
= SIZE_DEFAULT
;
50 while (newsize
< wanted
)
52 if (2 * newsize
< newsize
)
61 extendbuf(void* existing
, size_t wanted
, size_t *allocated
)
66 newsize
= decide_size(*allocated
, wanted
);
68 if ( (*allocated
) == 0 )
70 /* Sanity check: If there is no existing allocation size, three
71 * must be no existing allocated buffer.
73 assert(NULL
== existing
);
75 (*allocated
) = newsize
;
76 return xmalloc(newsize
);
80 if (newsize
!= (*allocated
) )
82 (*allocated
) = newsize
;
83 return xrealloc (existing
, newsize
);