summaryrefslogtreecommitdiffstats
path: root/gl/xalloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'gl/xalloc.h')
-rw-r--r--gl/xalloc.h29
1 files changed, 16 insertions, 13 deletions
diff --git a/gl/xalloc.h b/gl/xalloc.h
index 17ab514..40dcf4b 100644
--- a/gl/xalloc.h
+++ b/gl/xalloc.h
@@ -1,12 +1,12 @@
1/* xalloc.h -- malloc with out-of-memory checking 1/* xalloc.h -- malloc with out-of-memory checking
2 2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2003, 2004, 2006 Free Software Foundation, Inc. 4 1999, 2000, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
5 5
6 This program is free software; you can redistribute it and/or modify 6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option) 8 the Free Software Foundation; either version 3 of the License, or
9 any later version. 9 (at your option) any later version.
10 10
11 This program is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,8 +14,7 @@
14 GNU General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation, 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 18
20#ifndef XALLOC_H_ 19#ifndef XALLOC_H_
21# define XALLOC_H_ 20# define XALLOC_H_
@@ -29,7 +28,7 @@ extern "C" {
29 28
30 29
31# ifndef __attribute__ 30# ifndef __attribute__
32# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ 31# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
33# define __attribute__(x) 32# define __attribute__(x)
34# endif 33# endif
35# endif 34# endif
@@ -139,10 +138,10 @@ xnrealloc (void *p, size_t n, size_t s)
139 allocating an initial block with a nonzero size, or by allocating a 138 allocating an initial block with a nonzero size, or by allocating a
140 larger block. 139 larger block.
141 140
142 In the following implementation, nonzero sizes are doubled so that 141 In the following implementation, nonzero sizes are increased by a
143 repeated reallocations have O(N log N) overall cost rather than 142 factor of approximately 1.5 so that repeated reallocations have
144 O(N**2) cost, but the specification for this function does not 143 O(N) overall cost rather than O(N**2) cost, but the
145 guarantee that sizes are doubled. 144 specification for this function does not guarantee that rate.
146 145
147 Here is an example of use: 146 Here is an example of use:
148 147
@@ -204,9 +203,13 @@ x2nrealloc (void *p, size_t *pn, size_t s)
204 } 203 }
205 else 204 else
206 { 205 {
207 if (((size_t) -1) / 2 / s < n) 206 /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
207 Check for overflow, so that N * S stays in size_t range.
208 The check is slightly conservative, but an exact check isn't
209 worth the trouble. */
210 if ((size_t) -1 / 3 * 2 / s <= n)
208 xalloc_die (); 211 xalloc_die ();
209 n *= 2; 212 n += (n + 1) / 2;
210 } 213 }
211 214
212 *pn = n; 215 *pn = n;