comparison lib/xalloc.h @ 4783:d1dc5d9bf1ba

Revamp xalloc interface so that it can check for address arithmetic overflow.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 13 Oct 2003 06:07:10 +0000
parents fd6696a97000
children 7c67f04e1c19
comparison
equal deleted inserted replaced
4782:bb7b44d87b4f 4783:d1dc5d9bf1ba
46 exit_failure (defined in exitfail.h). This is the 46 exit_failure (defined in exitfail.h). This is the
47 function to call when one wants the program to die because of a 47 function to call when one wants the program to die because of a
48 memory allocation failure. */ 48 memory allocation failure. */
49 extern void xalloc_die (void) ATTRIBUTE_NORETURN; 49 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
50 50
51 void *xmalloc (size_t n); 51 void *xmalloc (size_t s);
52 void *xnmalloc (size_t n, size_t s);
53 void *xzalloc (size_t s);
52 void *xcalloc (size_t n, size_t s); 54 void *xcalloc (size_t n, size_t s);
53 void *xrealloc (void *p, size_t n); 55 void *xrealloc (void *p, size_t s);
56 void *xnrealloc (void *p, size_t n, size_t s);
57 void *xclone (void const *p, size_t s);
54 char *xstrdup (const char *str); 58 char *xstrdup (const char *str);
55 59
56 # define XMALLOC(Type, N_items) xmalloc (sizeof (Type) * (N_items)) 60 /* These macros are deprecated; they will go away soon, and are retained
57 # define XCALLOC(Type, N_items) xcalloc (sizeof (Type), N_items) 61 temporarily only to ease conversion to the functions described above. */
58 # define XREALLOC(Ptr, Type, N_items) xrealloc (Ptr, sizeof (Type) * (N_items)) 62 # define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
59 63 # define CLONE(p) xclone (p, sizeof *(p))
60 /* Declare and alloc memory for VAR of type TYPE. */ 64 # define NEW(type, var) type *var = xmalloc (sizeof (type))
61 # define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) 65 # define XCALLOC(type, n) xcalloc (n, sizeof (type))
62 66 # define XMALLOC(type, n) xnmalloc (n, sizeof (type))
63 /* Free VAR only if non NULL. */ 67 # define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
64 # define XFREE(Var) \ 68 # define XFREE(p) free (p)
65 do { \
66 if (Var) \
67 free (Var); \
68 } while (0)
69
70 /* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
71 # define CCLONE(Src, Num) \
72 (memcpy (xmalloc (sizeof *(Src) * (Num)), Src, sizeof *(Src) * (Num)))
73
74 /* Return a malloc'ed copy of SRC. */
75 # define CLONE(Src) CCLONE (Src, 1)
76
77 69
78 #endif /* !XALLOC_H_ */ 70 #endif /* !XALLOC_H_ */