[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: More attributes on xmalloc and friends?
From: |
Jim Meyering |
Subject: |
Re: More attributes on xmalloc and friends? |
Date: |
Wed, 17 Oct 2007 15:50:41 +0200 |
Colin Watson <address@hidden> wrote:
> If I'm not mistaken, the xalloc family of functions should have the same
> set of function attributes as the corresponding functions in glibc.
> glibc gives malloc, calloc, and realloc the 'malloc' and
> 'warn_unused_result' attributes; similarly, it gives strdup and strndup
> the 'malloc' and 'nonnull (1)' attributes. 'malloc' requires gcc 2.96,
> 'nonnull' requires gcc 3.3, and 'warn_unused_result' requires gcc 3.4.
> Could Gnulib do the same for its x* wrappers?
Yes, indeed.
I started doing this months ago, but never checked it in.
Here's a preliminary, and probably incomplete, patch:
lib/xalloc.h: Use __warn_unused_result__ and __malloc__ attributes.
---
lib/xalloc.h | 10 +++++++++-
1 files changed, 9 insertions(+), 1 deletions(-)
diff --git a/lib/xalloc.h b/lib/xalloc.h
index 9c40d72..441410c 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -107,7 +107,9 @@ char *xstrdup (char const *str);
/* Allocate an array of N objects, each with S bytes of memory,
dynamically, with error checking. S must be nonzero. */
-
+static_inline void *
+xnmalloc (size_t n, size_t s)
+ __attribute__((__warn_unused_result__)) __attribute__((__malloc__));
static_inline void *
xnmalloc (size_t n, size_t s)
{
@@ -121,6 +123,9 @@ xnmalloc (size_t n, size_t s)
static_inline void *
xnrealloc (void *p, size_t n, size_t s)
+ __attribute__((__warn_unused_result__));
+static_inline void *
+xnrealloc (void *p, size_t n, size_t s)
{
if (xalloc_oversized (n, s))
xalloc_die ();
@@ -184,6 +189,9 @@ xnrealloc (void *p, size_t n, size_t s)
static_inline void *
x2nrealloc (void *p, size_t *pn, size_t s)
+ __attribute__((__warn_unused_result__));
+static_inline void *
+x2nrealloc (void *p, size_t *pn, size_t s)
{
size_t n = *pn;
--
1.5.3.4.206.g58ba4