Skip to content

Commit 763a7e1

Browse files
committed
Update from GNU gettext.
1 parent 353b958 commit 763a7e1

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

ChangeLog

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
2007-10-23 Bruno Haible <bruno@clisp.org>
2+
3+
Update from GNU gettext.
4+
2006-11-26 Bruno Haible <bruno@clisp.org>
5+
* gnulib-local/lib/xalloc.h (xmemdup): Add a typesafe C++
6+
template variant.
7+
Based on a patch from Paul Eggert in gnulib.
8+
2006-11-06 Bruno Haible <bruno@clisp.org>
9+
* gnulib-local/lib/xalloc.h (xcharalloc): New macro.
10+
(xmemdup): New declaration.
11+
* gnulib-local/lib/xstrdup.c (xmemdup): New function.
12+
2006-11-03 Bruno Haible <bruno@clisp.org>
13+
* gnulib-local/lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC,
14+
XCALLOC): New macros.
15+
(xnboundedmalloc): New inline function.
16+
* gnulib-local/lib/xstrdup.c (xstrdup): Use XNMALLOC instead of
17+
xmalloc.
18+
2006-11-02 Bruno Haible <bruno@clisp.org>
19+
* lib/xalloc.h (xnmalloc): New declaration. From gnulib
20+
xalloc.h.
21+
* lib/xmalloc.c (fixup_null_alloc): Write NULL, not 0.
22+
(xnmalloc): New function.
23+
124
2007-10-23 Bruno Haible <bruno@clisp.org>
225

326
Moved module xreadlink to gnulib.

gnulib-local/lib/xalloc.h

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ extern "C" {
3131
/* Allocate SIZE bytes of memory dynamically, with error checking. */
3232
extern 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. */
3640
extern void *xzalloc (size_t size);
@@ -47,7 +51,7 @@ extern void *xrealloc (void *ptr, size_t size);
4751
template <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
}
5256
extern "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. */
69134
extern char *xstrdup (const char *string);
70135

gnulib-local/lib/xmalloc.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fixup_null_alloc (size_t n)
4646
{
4747
void *p;
4848

49-
p = 0;
49+
p = NULL;
5050
if (n == 0)
5151
p = malloc ((size_t) 1);
5252
if (p == NULL)
@@ -67,6 +67,24 @@ xmalloc (size_t n)
6767
return p;
6868
}
6969

70+
/* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
71+
SIZE must be > 0. */
72+
73+
void *
74+
xnmalloc (size_t nmemb, size_t size)
75+
{
76+
size_t n;
77+
void *p;
78+
79+
if (xalloc_oversized (nmemb, size))
80+
xalloc_die ();
81+
n = nmemb * size;
82+
p = malloc (n);
83+
if (p == NULL)
84+
p = fixup_null_alloc (n);
85+
return p;
86+
}
87+
7088
/* Allocate SIZE bytes of memory dynamically, with error checking,
7189
and zero it. */
7290

gnulib-local/lib/xstrdup.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@
2121

2222
#include <string.h>
2323

24+
/* Return a newly allocated copy of the N bytes of memory starting at P. */
25+
26+
void *
27+
xmemdup (const void *p, size_t n)
28+
{
29+
void *q = xmalloc (n);
30+
memcpy (q, p, n);
31+
return q;
32+
}
33+
2434
/* Return a newly allocated copy of STRING. */
2535

2636
char *
2737
xstrdup (const char *string)
2838
{
29-
return strcpy ((char *) xmalloc (strlen (string) + 1), string);
39+
return strcpy (XNMALLOC (strlen (string) + 1, char), string);
3040
}

0 commit comments

Comments
 (0)