@@ -31,6 +31,10 @@ extern "C" {
3131/* Allocate SIZE bytes of memory dynamically, with error checking. */
3232extern void *xmalloc (size_t size);
3333
34+ /* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
35+ SIZE must be > 0. */
36+ extern void *xnmalloc (size_t nmemb, size_t size);
37+
3438/* Allocate SIZE bytes of memory dynamically, with error checking,
3539 and zero it. */
3640extern void *xzalloc (size_t size);
@@ -47,7 +51,7 @@ extern void *xrealloc (void *ptr, size_t size);
4751template <typename T>
4852 inline T * xrealloc (T * ptr, size_t size)
4953 {
50- return (T *) xrealloc ((void *) ptr, size);
54+ return (T *) xrealloc ((void *) ptr, size);
5155 }
5256extern " C" {
5357#endif
@@ -62,9 +66,70 @@ extern void xalloc_die (void)
6266#endif
6367 ;
6468
69+ /* In the following macros, T must be an elementary or structure/union or
70+ typedef'ed type, or a pointer to such a type. To apply one of the
71+ following macros to a function pointer or array type, you need to typedef
72+ it first and use the typedef name. */
73+
74+ /* Allocate an object of type T dynamically, with error checking. */
75+ /* extern T *XMALLOC (typename T); */
76+ #define XMALLOC (T ) \
77+ ((T *) xmalloc (sizeof (T)))
78+
79+ /* Allocate memory for NMEMB elements of type T, with error checking. */
80+ /* extern T *XNMALLOC (size_t nmemb, typename T); */
81+ #if HAVE_INLINE
82+ /* xnmalloc performs a division and multiplication by sizeof (T). Arrange to
83+ perform the division at compile-time and the multiplication with a factor
84+ known at compile-time. */
85+ # define XNMALLOC (N,T ) \
86+ ((T *) (sizeof (T) == 1 \
87+ ? xmalloc (N) \
88+ : xnboundedmalloc(N, (size_t ) (sizeof (ptrdiff_t ) <= sizeof (size_t ) ? -1 : -2 ) / sizeof (T), sizeof (T))))
89+ static inline void *
90+ xnboundedmalloc (size_t n, size_t bound, size_t s)
91+ {
92+ if (n > bound)
93+ xalloc_die ();
94+ return xmalloc (n * s);
95+ }
96+ #else
97+ # define XNMALLOC (N,T ) \
98+ ((T *) (sizeof (T) == 1 ? xmalloc (N) : xnmalloc (N, sizeof (T))))
99+ #endif
100+
101+ /* Allocate an object of type T dynamically, with error checking,
102+ and zero it. */
103+ /* extern T *XZALLOC (typename T); */
104+ #define XZALLOC (T ) \
105+ ((T *) xzalloc (sizeof (T)))
106+
107+ /* Allocate memory for NMEMB elements of type T, with error checking,
108+ and zero it. */
109+ /* extern T *XCALLOC (size_t nmemb, typename T); */
110+ #define XCALLOC (N,T ) \
111+ ((T *) xcalloc (N, sizeof (T)))
112+
113+ /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
114+ except it returns char *. */
115+ #define xcharalloc (N ) \
116+ XNMALLOC (N, char )
117+
65118
66119/* Defined in xstrdup.c. */
67120
121+ /* Return a newly allocated copy of the N bytes of memory starting at P. */
122+ extern void *xmemdup (const void *p, size_t n);
123+ #ifdef __cplusplus
124+ }
125+ template <typename T>
126+ inline T * xmemdup (const T * p, size_t n)
127+ {
128+ return (T *) xmemdup ((const void *) p, n);
129+ }
130+ extern " C" {
131+ #endif
132+
68133/* Return a newly allocated copy of STRING. */
69134extern char *xstrdup (const char *string);
70135
0 commit comments