1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#ifndef _XALLOC_H
#define _XALLOC_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
extern void *xmalloc (size_t size);
extern void *xnmalloc (size_t nmemb, size_t size);
extern void *xzalloc (size_t size);
extern void *xcalloc (size_t nmemb, size_t size);
extern void *xrealloc (void *ptr, size_t size);
#ifdef __cplusplus
}
template <typename T>
inline T * xrealloc (T * ptr, size_t size)
{
return (T *) xrealloc ((void *) ptr, size);
}
extern "C" {
#endif
extern void xalloc_die (void)
#if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) && !__STRICT_ANSI__
__attribute__ ((__noreturn__))
#endif
;
#define XMALLOC(T) \
((T *) xmalloc (sizeof (T)))
#if HAVE_INLINE
# define XNMALLOC(N,T) \
((T *) (sizeof (T) == 1 \
? xmalloc (N) \
: xnboundedmalloc(N, (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / sizeof (T), sizeof (T))))
static inline void *
xnboundedmalloc (size_t n, size_t bound, size_t s)
{
if (n > bound)
xalloc_die ();
return xmalloc (n * s);
}
#else
# define XNMALLOC(N,T) \
((T *) (sizeof (T) == 1 ? xmalloc (N) : xnmalloc (N, sizeof (T))))
#endif
#define XZALLOC(T) \
((T *) xzalloc (sizeof (T)))
#define XCALLOC(N,T) \
((T *) xcalloc (N, sizeof (T)))
#define xcharalloc(N) \
XNMALLOC (N, char)
extern void *xmemdup (const void *p, size_t n);
#ifdef __cplusplus
}
template <typename T>
inline T * xmemdup (const T * p, size_t n)
{
return (T *) xmemdup ((const void *) p, n);
}
extern "C" {
#endif
extern char *xstrdup (const char *string);
# define xalloc_oversized(n, s) \
((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
#ifdef __cplusplus
}
#endif
#endif
|