cvsimport
[findutils.git] / lib / extendbuf.c
bloba082733513d938d7b05781a719764853436471e4
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)
8 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by James Yougnman <jay@gnu.org>. */
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <errno.h>
30 #include "xalloc.h"
31 #include "extendbuf.h"
34 /* We initially use a small default size to ensure that this code
35 * gets exercised.
37 #ifndef SIZE_DEFAULT
38 # define SIZE_DEFAULT 16
39 #endif
41 static size_t
42 decide_size(size_t current, size_t wanted)
44 size_t newsize;
46 if (0 == current)
47 newsize = SIZE_DEFAULT;
48 else
49 newsize = current;
51 while (newsize < wanted)
53 if (2 * newsize < newsize)
54 xalloc_die ();
55 newsize *= 2;
57 return newsize;
61 void *
62 extendbuf(void* existing, size_t wanted, size_t *allocated)
64 int saved_errno;
65 size_t newsize;
66 void *result; /* leave uninitialised to allow static code checkers to identify bugs */
68 saved_errno = errno;
70 assert(wanted > 0u);
71 newsize = decide_size(*allocated, wanted);
73 if ( (*allocated) == 0 )
75 /* Sanity check: If there is no existing allocation size, there
76 * must be no existing allocated buffer.
78 assert(NULL == existing);
80 (*allocated) = newsize;
81 result = xmalloc(newsize);
83 else
85 if (newsize != (*allocated) )
87 (*allocated) = newsize;
88 result = xrealloc (existing, newsize);
91 else
93 result = existing;
97 if (result)
99 /* xmalloc() or xrealloc() may have changed errno, but in the
100 success case we want to preserve the previous value.
102 errno = saved_errno;
104 return result;