1
1
/* xalloc.h -- malloc with out-of-memory checking
3
Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4
2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation,
3
Copyright (C) 1990-2000, 2003-2004, 2006-2012 Free Software Foundation, Inc.
7
5
This program is free software: you can redistribute it and/or modify
8
6
it under the terms of the GNU General Public License as published by
23
21
# include <stddef.h>
23
# include "xalloc-oversized.h"
26
25
# ifdef __cplusplus
31
# ifndef __attribute__
32
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
33
# define __attribute__(x)
37
# ifndef ATTRIBUTE_NORETURN
38
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
41
# ifndef ATTRIBUTE_MALLOC
43
# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
45
# define ATTRIBUTE_MALLOC
31
# define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
33
# define _GL_ATTRIBUTE_MALLOC
36
# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
37
# define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
39
# define _GL_ATTRIBUTE_ALLOC_SIZE(args)
49
42
/* This function is always triggered when memory is exhausted.
51
44
or by using gnulib's xalloc-die module. This is the
52
45
function to call when one wants the program to die because of a
53
46
memory allocation failure. */
54
extern void xalloc_die (void) ATTRIBUTE_NORETURN;
47
extern _Noreturn void xalloc_die (void);
56
void *xmalloc (size_t s) ATTRIBUTE_MALLOC;
57
void *xzalloc (size_t s) ATTRIBUTE_MALLOC;
58
void *xcalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
59
void *xrealloc (void *p, size_t s);
49
void *xmalloc (size_t s)
50
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
51
void *xzalloc (size_t s)
52
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
53
void *xcalloc (size_t n, size_t s)
54
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
55
void *xrealloc (void *p, size_t s)
56
_GL_ATTRIBUTE_ALLOC_SIZE ((2));
60
57
void *x2realloc (void *p, size_t *pn);
61
void *xmemdup (void const *p, size_t s) ATTRIBUTE_MALLOC;
62
char *xstrdup (char const *str) ATTRIBUTE_MALLOC;
64
/* Return 1 if an array of N objects, each of size S, cannot exist due
65
to size arithmetic overflow. S must be positive and N must be
66
nonnegative. This is a macro, not an inline function, so that it
67
works correctly even when SIZE_MAX < N.
69
By gnulib convention, SIZE_MAX represents overflow in size
70
calculations, so the conservative dividend to use here is
71
SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
72
However, malloc (SIZE_MAX) fails on all known hosts where
73
sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
74
exactly-SIZE_MAX allocations on such hosts; this avoids a test and
75
branch when S is known to be 1. */
76
# define xalloc_oversized(n, s) \
77
((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
58
void *xmemdup (void const *p, size_t s)
59
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2));
60
char *xstrdup (char const *str)
80
63
/* In the following macros, T must be an elementary or structure/union or
81
64
typedef'ed type, or a pointer to such a type. To apply one of the
107
90
# define static_inline static inline
109
void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
110
void *xnrealloc (void *p, size_t n, size_t s);
92
void *xnmalloc (size_t n, size_t s)
93
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
94
void *xnrealloc (void *p, size_t n, size_t s)
95
_GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
111
96
void *x2nrealloc (void *p, size_t *pn, size_t s);
112
char *xcharalloc (size_t n) ATTRIBUTE_MALLOC;
97
char *xcharalloc (size_t n)
98
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
115
101
# ifdef static_inline
117
103
/* Allocate an array of N objects, each with S bytes of memory,
118
104
dynamically, with error checking. S must be nonzero. */
120
static_inline void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
106
static_inline void *xnmalloc (size_t n, size_t s)
107
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
121
108
static_inline void *
122
109
xnmalloc (size_t n, size_t s)
129
116
/* Change the size of an allocated block of memory P to an array of N
130
117
objects each of S bytes, with error checking. S must be nonzero. */
119
static_inline void *xnrealloc (void *p, size_t n, size_t s)
120
_GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
132
121
static_inline void *
133
122
xnrealloc (void *p, size_t n, size_t s)
204
193
/* The approximate size to use for initial small allocation
205
194
requests, when the invoking code specifies an old size of
206
zero. 64 bytes is the largest "small" request for the
207
GNU C library malloc. */
208
enum { DEFAULT_MXFAST = 64 };
195
zero. This is the largest "small" request for the GNU C
197
enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
210
199
n = DEFAULT_MXFAST / s;