Documented -execdir, and the "+" variants of -exec and -execdir.
[findutils.git] / lib / extendbuf.c
blob9b52c5c94da33b6ec8b15e6cc52877a2f9d4cc35
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
29 #include "xalloc.h"
30 #include "extendbuf.h"
33 /* We initially use a small default size to ensure that this code
34 * gets exercised.
36 #ifndef SIZE_DEFAULT
37 # define SIZE_DEFAULT 16
38 #endif
40 static size_t
41 decide_size(size_t current, size_t wanted)
43 size_t newsize;
45 if (0 == current)
46 newsize = SIZE_DEFAULT;
47 else
48 newsize = current;
50 while (newsize < wanted)
52 if (2 * newsize < newsize)
53 xalloc_die ();
54 newsize *= 2;
56 return newsize;
60 void *
61 extendbuf(void* existing, size_t wanted, size_t *allocated)
63 size_t newsize;
65 assert(wanted > 0u);
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);
78 else
80 if (newsize != (*allocated) )
82 (*allocated) = newsize;
83 return xrealloc (existing, newsize);
85 else
87 return existing;