Imported Upstream version 4.5.10
[platform/upstream/findutils.git] / lib / extendbuf.c
1 /* extendbuf.c -- manage a dynamically-allocated buffer
2
3    Copyright 2004, 2010 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 #include <config.h>
21
22
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <errno.h>
27
28 #include "xalloc.h"
29 #include "extendbuf.h"
30
31
32 /* We initially use a small default size to ensure that this code
33  * gets exercised.
34  */
35 #ifndef SIZE_DEFAULT
36 # define SIZE_DEFAULT 16
37 #endif
38
39 static size_t
40 decide_size (size_t current, size_t wanted)
41 {
42   size_t newsize;
43
44   if (0 == current)
45     newsize = SIZE_DEFAULT;
46   else
47     newsize = current;
48
49   while (newsize < wanted)
50     {
51       if (2 * newsize < newsize)
52         return wanted;
53       newsize *= 2;
54     }
55   return newsize;
56 }
57
58
59 void *
60 extendbuf (void* existing, size_t wanted, size_t *allocated)
61 {
62   int saved_errno;
63   size_t newsize;
64   void *result; /* leave uninitialised to allow static code checkers to identify bugs */
65
66   saved_errno = errno;
67
68   assert (wanted > 0u);
69   newsize = decide_size (*allocated, wanted);
70
71   if ( (*allocated) == 0 )
72     {
73       /* Sanity check: If there is no existing allocation size, there
74        * must be no existing allocated buffer.
75        */
76       assert (NULL == existing);
77
78       (*allocated) = newsize;
79       result = malloc (newsize);
80     }
81   else
82     {
83       if (newsize != (*allocated) )
84         {
85           (*allocated) = newsize;
86           result = realloc (existing, newsize);
87           if (NULL == result)
88             {
89               saved_errno = errno;
90             }
91         }
92       else
93         {
94           result = existing;
95         }
96     }
97
98   if (result)
99     {
100       /* malloc () or realloc () may have changed errno, but in the
101          success case we want to preserve the previous value.
102       */
103       errno = saved_errno;
104     }
105   return result;
106 }
107
108
109 void *
110 xextendbuf (void* existing, size_t wanted, size_t *allocated)
111 {
112   void *p = extendbuf (existing, wanted, allocated);
113   if (NULL == p)
114     {
115       free (existing);
116       xalloc_die ();
117     }
118   return p;
119 }