[PATCH 2/2] shared: Print .c file/line number in x* routines

Pekka Paalanen ppaalanen at gmail.com
Wed Mar 16 09:57:45 UTC 2016


On Tue, 15 Mar 2016 15:23:31 -0700
Bryce Harrington <bryce at osg.samsung.com> wrote:

> Switches from inline to pre-processor definitions in order to utilize
> __FILE__ and __LINE__ from the .c file in order to display the location
> of memory allocation failures when failing.
> 
> Now xmalloc, et al calls will produce:
> 
>     [weston-info] clients/weston-info.c:714: out of memory (1024)
> 
> Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
> ---
>  shared/xalloc.h | 28 ++++------------------------
>  1 file changed, 4 insertions(+), 24 deletions(-)
> 
> diff --git a/shared/xalloc.h b/shared/xalloc.h
> index 74a9b6c..4464133 100644
> --- a/shared/xalloc.h
> +++ b/shared/xalloc.h
> @@ -38,30 +38,10 @@ extern "C" {
>  void *
>  fail_on_null(void *p, size_t size, char *file, int32_t line);
>  
> -static inline void *
> -xmalloc(size_t s)
> -{
> -        return fail_on_null(malloc(s), s, NULL, 0);
> -}
> -
> -static inline void *
> -xzalloc(size_t s)
> -{
> -	return fail_on_null(zalloc(s), s, NULL, 0);
> -}
> -
> -static inline char *
> -xstrdup(const char *s)
> -{
> -	return fail_on_null(strdup(s), 0, NULL, 0);
> -}
> -
> -static inline void *
> -xrealloc(char *p, size_t s)
> -{
> -        return fail_on_null(realloc(p, s), s, NULL, 0);
> -}
> -
> +#define xmalloc(s) (fail_on_null(malloc(s), s, __FILE__, __LINE__))
> +#define xzalloc(s) (fail_on_null(zalloc(s), s, __FILE__, __LINE__))
> +#define xstrdup(s) (fail_on_null(strdup(s), 1, __FILE__, __LINE__))
> +#define xrealloc(p, s) (fail_on_null(realloc(p, s), s, __FILE__, __LINE__))
>  
>  #ifdef  __cplusplus
>  }

Hi Bryce,

in these macros the arguments are evaluated multiple times and could
probably use more parentheses. xstrdup will report a spurious size of
1, while no size (zero) would avoid a confusing message.


Thanks,
pq
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 811 bytes
Desc: OpenPGP digital signature
URL: <https://lists.freedesktop.org/archives/wayland-devel/attachments/20160316/048608a2/attachment.sig>


More information about the wayland-devel mailing list