annotate lib/xmalloc.c @ 276:23034db7dd8a 2.5

fixed expression 'slots' to not store null on a pass 1 store
author lost
date Mon, 31 Aug 2009 08:39:36 +0000
parents d5392bb5da3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1 /* xmalloc.c -- malloc with out of memory checking
d5392bb5da3c Added generated files
lost
parents:
diff changeset
2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
4 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2008 Free Software Foundation,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
5 Inc.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
6
d5392bb5da3c Added generated files
lost
parents:
diff changeset
7 This program is free software: you can redistribute it and/or modify
d5392bb5da3c Added generated files
lost
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
d5392bb5da3c Added generated files
lost
parents:
diff changeset
9 the Free Software Foundation; either version 3 of the License, or
d5392bb5da3c Added generated files
lost
parents:
diff changeset
10 (at your option) any later version.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
11
d5392bb5da3c Added generated files
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
15 GNU General Public License for more details.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
16
d5392bb5da3c Added generated files
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
d5392bb5da3c Added generated files
lost
parents:
diff changeset
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
19
d5392bb5da3c Added generated files
lost
parents:
diff changeset
20 #include <config.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
21
d5392bb5da3c Added generated files
lost
parents:
diff changeset
22 #if ! HAVE_INLINE
d5392bb5da3c Added generated files
lost
parents:
diff changeset
23 # define static_inline
d5392bb5da3c Added generated files
lost
parents:
diff changeset
24 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
25 #include "xalloc.h"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
26 #undef static_inline
d5392bb5da3c Added generated files
lost
parents:
diff changeset
27
d5392bb5da3c Added generated files
lost
parents:
diff changeset
28 #include <stdlib.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
29 #include <string.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
30
d5392bb5da3c Added generated files
lost
parents:
diff changeset
31 #ifndef SIZE_MAX
d5392bb5da3c Added generated files
lost
parents:
diff changeset
32 # define SIZE_MAX ((size_t) -1)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
33 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
34
d5392bb5da3c Added generated files
lost
parents:
diff changeset
35 /* 1 if calloc is known to be compatible with GNU calloc. This
d5392bb5da3c Added generated files
lost
parents:
diff changeset
36 matters if we are not also using the calloc module, which defines
d5392bb5da3c Added generated files
lost
parents:
diff changeset
37 HAVE_CALLOC and supports the GNU API even on non-GNU platforms. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
38 #if defined HAVE_CALLOC || defined __GLIBC__
d5392bb5da3c Added generated files
lost
parents:
diff changeset
39 enum { HAVE_GNU_CALLOC = 1 };
d5392bb5da3c Added generated files
lost
parents:
diff changeset
40 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
41 enum { HAVE_GNU_CALLOC = 0 };
d5392bb5da3c Added generated files
lost
parents:
diff changeset
42 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
43
d5392bb5da3c Added generated files
lost
parents:
diff changeset
44 /* Allocate N bytes of memory dynamically, with error checking. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
45
d5392bb5da3c Added generated files
lost
parents:
diff changeset
46 void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
47 xmalloc (size_t n)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
48 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
49 void *p = malloc (n);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
50 if (!p && n != 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
51 xalloc_die ();
d5392bb5da3c Added generated files
lost
parents:
diff changeset
52 return p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
53 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
54
d5392bb5da3c Added generated files
lost
parents:
diff changeset
55 /* Change the size of an allocated block of memory P to N bytes,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
56 with error checking. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
57
d5392bb5da3c Added generated files
lost
parents:
diff changeset
58 void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
59 xrealloc (void *p, size_t n)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
60 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
61 p = realloc (p, n);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
62 if (!p && n != 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
63 xalloc_die ();
d5392bb5da3c Added generated files
lost
parents:
diff changeset
64 return p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
65 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
66
d5392bb5da3c Added generated files
lost
parents:
diff changeset
67 /* If P is null, allocate a block of at least *PN bytes; otherwise,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
68 reallocate P so that it contains more than *PN bytes. *PN must be
d5392bb5da3c Added generated files
lost
parents:
diff changeset
69 nonzero unless P is null. Set *PN to the new block's size, and
d5392bb5da3c Added generated files
lost
parents:
diff changeset
70 return the pointer to the new block. *PN is never set to zero, and
d5392bb5da3c Added generated files
lost
parents:
diff changeset
71 the returned pointer is never null. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
72
d5392bb5da3c Added generated files
lost
parents:
diff changeset
73 void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
74 x2realloc (void *p, size_t *pn)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
75 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
76 return x2nrealloc (p, pn, 1);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
77 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
78
d5392bb5da3c Added generated files
lost
parents:
diff changeset
79 /* Allocate S bytes of zeroed memory dynamically, with error checking.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
80 There's no need for xnzalloc (N, S), since it would be equivalent
d5392bb5da3c Added generated files
lost
parents:
diff changeset
81 to xcalloc (N, S). */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
82
d5392bb5da3c Added generated files
lost
parents:
diff changeset
83 void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
84 xzalloc (size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
85 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
86 return memset (xmalloc (s), 0, s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
87 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
88
d5392bb5da3c Added generated files
lost
parents:
diff changeset
89 /* Allocate zeroed memory for N elements of S bytes, with error
d5392bb5da3c Added generated files
lost
parents:
diff changeset
90 checking. S must be nonzero. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
91
d5392bb5da3c Added generated files
lost
parents:
diff changeset
92 void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
93 xcalloc (size_t n, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
94 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
95 void *p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
96 /* Test for overflow, since some calloc implementations don't have
d5392bb5da3c Added generated files
lost
parents:
diff changeset
97 proper overflow checks. But omit overflow and size-zero tests if
d5392bb5da3c Added generated files
lost
parents:
diff changeset
98 HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
d5392bb5da3c Added generated files
lost
parents:
diff changeset
99 returns NULL if successful. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
100 if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
101 || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
102 xalloc_die ();
d5392bb5da3c Added generated files
lost
parents:
diff changeset
103 return p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
104 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
105
d5392bb5da3c Added generated files
lost
parents:
diff changeset
106 /* Clone an object P of size S, with error checking. There's no need
d5392bb5da3c Added generated files
lost
parents:
diff changeset
107 for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
d5392bb5da3c Added generated files
lost
parents:
diff changeset
108 need for an arithmetic overflow check. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
109
d5392bb5da3c Added generated files
lost
parents:
diff changeset
110 void *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
111 xmemdup (void const *p, size_t s)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
112 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
113 return memcpy (xmalloc (s), p, s);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
114 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
115
d5392bb5da3c Added generated files
lost
parents:
diff changeset
116 /* Clone STRING. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
117
d5392bb5da3c Added generated files
lost
parents:
diff changeset
118 char *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
119 xstrdup (char const *string)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
120 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
121 return xmemdup (string, strlen (string) + 1);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
122 }