Imported Upstream version 4.5.14
[platform/upstream/findutils.git] / lib / extendbuf.c
1 /* extendbuf.c -- manage a dynamically-allocated buffer
2
3    Copyright 2004, 2010, 2011 Free Software Foundation, Inc.
4
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 3 of the License, or
8    (at your option) any later version.
9
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.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /* Written by James Yougnman <jay@gnu.org>. */
19
20 /* config.h must be included first. */
21 #include <config.h>
22
23 /* system headers. */
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdlib.h>
27
28 /* gnulib headers. */
29 #include "xalloc.h"
30
31 /* find headers. */
32 #include "extendbuf.h"
33
34
35 /* We initially use a small default size to ensure that this code
36  * gets exercised.
37  */
38 #ifndef SIZE_DEFAULT
39 # define SIZE_DEFAULT 16
40 #endif
41
42 static size_t
43 decide_size (size_t current, size_t wanted)
44 {
45   size_t newsize;
46
47   if (0 == current)
48     newsize = SIZE_DEFAULT;
49   else
50     newsize = current;
51
52   while (newsize < wanted)
53     {
54       if (2 * newsize < newsize)
55         return wanted;
56       newsize *= 2;
57     }
58   return newsize;
59 }
60
61
62 void *
63 extendbuf (void* existing, size_t wanted, size_t *allocated)
64 {
65   int saved_errno;
66   size_t newsize;
67   void *result; /* leave uninitialized to allow static code checkers to identify bugs */
68
69   saved_errno = errno;
70
71   assert (wanted > 0u);
72   newsize = decide_size (*allocated, wanted);
73
74   if ( (*allocated) == 0 )
75     {
76       /* Sanity check: If there is no existing allocation size, there
77        * must be no existing allocated buffer.
78        */
79       assert (NULL == existing);
80
81       (*allocated) = newsize;
82       result = malloc (newsize);
83     }
84   else
85     {
86       if (newsize != (*allocated) )
87         {
88           (*allocated) = newsize;
89           result = realloc (existing, newsize);
90           if (NULL == result)
91             {
92               saved_errno = errno;
93             }
94         }
95       else
96         {
97           result = existing;
98         }
99     }
100
101   if (result)
102     {
103       /* malloc () or realloc () may have changed errno, but in the
104          success case we want to preserve the previous value.
105       */
106       errno = saved_errno;
107     }
108   return result;
109 }
110
111
112 void *
113 xextendbuf (void* existing, size_t wanted, size_t *allocated)
114 {
115   void *p = extendbuf (existing, wanted, allocated);
116   if (NULL == p)
117     {
118       free (existing);
119       xalloc_die ();
120     }
121   return p;
122 }