* MODULES.html.sh (File system functions): New module
[gnulib.git] / lib / xalloc.h
index f80977e..6881ea6 100644 (file)
@@ -71,8 +71,96 @@ char *xstrdup (char const *str);
 # define xalloc_oversized(n, s) \
     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
 
+/* In the following macros, T must be an elementary or structure/union or
+   typedef'ed type, or a pointer to such a type.  To apply one of the
+   following macros to a function pointer or array type, you need to typedef
+   it first and use the typedef name.  */
+
+/* Allocate an object of type T dynamically, with error checking.  */
+/* extern T *XMALLOC (typename T); */
+#define XMALLOC(T) \
+  ((T *) xmalloc (sizeof (T)))
+
+/* Allocate memory for NMEMB elements of type T, with error checking.  */
+/* extern T *XNMALLOC (size_t nmemb, typename T); */
+#if HAVE_INLINE
+/* xnmalloc performs a division and multiplication by sizeof (T).  Arrange to
+   perform the division at compile-time and the multiplication with a factor
+   known at compile-time.  */
+# 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
+
+/* Allocate an object of type T dynamically, with error checking,
+   and zero it.  */
+/* extern T *XZALLOC (typename T); */
+#define XZALLOC(T) \
+  ((T *) xzalloc (sizeof (T)))
+
+/* Allocate memory for NMEMB elements of type T, with error checking,
+   and zero it.  */
+/* extern T *XCALLOC (size_t nmemb, typename T); */
+#define XCALLOC(N,T) \
+  ((T *) xcalloc (N, sizeof (T)))
+
+/* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
+   except it returns char *.
+   xcharalloc (N) is equivalent to XNMALLOC (N, char).  */
+static inline char *
+xcharalloc (size_t n)
+{
+  return (char *) xmalloc (n);
+}
+
 # ifdef __cplusplus
 }
+
+/* C++ does not allow conversions from void * to other pointer types
+   without a cast.  Use templates to work around the problem when
+   possible.  */
+
+template <typename T> inline T *
+xrealloc (T *p, size_t s)
+{
+  return (T *) xrealloc ((void *) p, s);
+}
+
+template <typename T> inline T *
+xnrealloc (T *p, size_t n, size_t s)
+{
+  return (T *) xnrealloc ((void *) p, n, s);
+}
+
+template <typename T> inline T *
+x2realloc (T *p, size_t *pn)
+{
+  return (T *) x2realloc ((void *) p, pn);
+}
+
+template <typename T> inline T *
+x2nrealloc (T *p, size_t *pn, size_t s)
+{
+  return (T *) x2nrealloc ((void *) p, pn, s);
+}
+
+template <typename T> inline T *
+xmemdup (T const *p, size_t s)
+{
+  return (T *) xmemdup ((void const *) p, s);
+}
+
 # endif