1 /* malloc with out of memory checking.
2 Copyright (C) 2001-2004, 2006, 2019-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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 <https://www.gnu.org/licenses/>. */
17
18 #ifndef _XALLOC_H
19 #define _XALLOC_H
20
21 #include <stddef.h>
22
23 #include "noreturn.h"
24 #include "xalloc-oversized.h"
25
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31
32 /* Defined in xmalloc.c. */
33
34 /* Allocate SIZE bytes of memory dynamically, with error checking. */
35 extern void *xmalloc (size_t size);
36
37 /* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
38 SIZE must be > 0. */
39 extern void *xnmalloc (size_t nmemb, size_t size);
40
41 /* Allocate SIZE bytes of memory dynamically, with error checking,
42 and zero it. */
43 extern void *xzalloc (size_t size);
44
45 /* Allocate memory for NMEMB elements of SIZE bytes, with error checking,
46 and zero it. */
47 extern void *xcalloc (size_t nmemb, size_t size);
48
49 /* Change the size of an allocated block of memory PTR to SIZE bytes,
50 with error checking. If PTR is NULL, run xmalloc. */
51 extern void *xrealloc (void *ptr, size_t size);
52 #ifdef __cplusplus
53 }
54 template <typename T>
xrealloc(T * ptr,size_t size)55 inline T * xrealloc (T * ptr, size_t size)
56 {
57 return (T *) xrealloc ((void *) ptr, size);
58 }
59 extern "C" {
60 #endif
61
62 /* If P is null, allocate a block of at least *PN bytes; otherwise,
63 reallocate P so that it contains more than *PN bytes. *PN must be
64 nonzero unless P is null. Set *PN to the new block's size, and
65 return the pointer to the new block. *PN is never set to zero, and
66 the returned pointer is never null. */
67 extern void *x2realloc (void *ptr, size_t *pn);
68 #ifdef __cplusplus
69 }
70 template <typename T>
x2realloc(T * ptr,size_t * pn)71 inline T * x2realloc (T * ptr, size_t *pn)
72 {
73 return (T *) x2realloc ((void *) ptr, pn);
74 }
75 extern "C" {
76 #endif
77
78 /* This function is always triggered when memory is exhausted. It is
79 in charge of honoring the three previous items. This is the
80 function to call when one wants the program to die because of a
81 memory allocation failure. */
82 _GL_NORETURN_FUNC extern void xalloc_die (void);
83
84 /* In the following macros, T must be an elementary or structure/union or
85 typedef'ed type, or a pointer to such a type. To apply one of the
86 following macros to a function pointer or array type, you need to typedef
87 it first and use the typedef name. */
88
89 /* Allocate an object of type T dynamically, with error checking. */
90 /* extern T *XMALLOC (typename T); */
91 #define XMALLOC(T) \
92 ((T *) xmalloc (sizeof (T)))
93
94 /* Allocate memory for NMEMB elements of type T, with error checking. */
95 /* extern T *XNMALLOC (size_t nmemb, typename T); */
96 #if HAVE_INLINE
97 /* xnmalloc performs a division and multiplication by sizeof (T). Arrange to
98 perform the division at compile-time and the multiplication with a factor
99 known at compile-time. */
100 # define XNMALLOC(N,T) \
101 ((T *) (sizeof (T) == 1 \
102 ? xmalloc (N) \
103 : xnboundedmalloc(N, (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / sizeof (T), sizeof (T))))
104 static inline void *
xnboundedmalloc(size_t n,size_t bound,size_t s)105 xnboundedmalloc (size_t n, size_t bound, size_t s)
106 {
107 if (n > bound)
108 xalloc_die ();
109 return xmalloc (n * s);
110 }
111 #else
112 # define XNMALLOC(N,T) \
113 ((T *) (sizeof (T) == 1 ? xmalloc (N) : xnmalloc (N, sizeof (T))))
114 #endif
115
116 /* Allocate an object of type T dynamically, with error checking,
117 and zero it. */
118 /* extern T *XZALLOC (typename T); */
119 #define XZALLOC(T) \
120 ((T *) xzalloc (sizeof (T)))
121
122 /* Allocate memory for NMEMB elements of type T, with error checking,
123 and zero it. */
124 /* extern T *XCALLOC (size_t nmemb, typename T); */
125 #define XCALLOC(N,T) \
126 ((T *) xcalloc (N, sizeof (T)))
127
128 /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
129 except it returns char *. */
130 #define xcharalloc(N) \
131 XNMALLOC (N, char)
132
133
134 /* Defined in xstrdup.c. */
135
136 /* Return a newly allocated copy of the N bytes of memory starting at P. */
137 extern void *xmemdup (const void *p, size_t n);
138 #ifdef __cplusplus
139 }
140 template <typename T>
xmemdup(const T * p,size_t n)141 inline T * xmemdup (const T * p, size_t n)
142 {
143 return (T *) xmemdup ((const void *) p, n);
144 }
145 extern "C" {
146 #endif
147
148 /* Return a newly allocated copy of STRING. */
149 extern char *xstrdup (const char *string);
150
151
152 #ifdef __cplusplus
153 }
154 #endif
155
156
157 #endif /* _XALLOC_H */
158