[PATCH:libXt 2/4] Add XtAsprintf() to Alloc.c & Intrinsic.h

walter harms wharms at bfs.de
Tue Dec 14 08:38:26 PST 2010



Am 14.12.2010 01:40, schrieb Alan Coopersmith:
> Like asprintf() but using XtMalloc() to tie into the Xt memory allocation
> and error handling subsystems.
> 
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
>  COPYING                 |    2 +-
>  include/X11/Intrinsic.h |    9 +++++++++
>  src/Alloc.c             |   37 ++++++++++++++++++++++++++++++++++++-
>  3 files changed, 46 insertions(+), 2 deletions(-)
> 
> Will use _X_RESTRICT_KYWD from patch 1/4 if present, but does not require
> it to avoid a hard dependency on a new x11proto release.
> 
> diff --git a/COPYING b/COPYING
> index 5507aa4..1c8683e 100644
> --- a/COPYING
> +++ b/COPYING
> @@ -19,7 +19,7 @@ DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
>  TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
>  PERFORMANCE OF THIS SOFTWARE.
>  
> -Copyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
> +Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
>  
>  Permission is hereby granted, free of charge, to any person obtaining a
>  copy of this software and associated documentation files (the "Software"),
> diff --git a/include/X11/Intrinsic.h b/include/X11/Intrinsic.h
> index 5111537..06f60c2 100644
> --- a/include/X11/Intrinsic.h
> +++ b/include/X11/Intrinsic.h
> @@ -1855,6 +1855,15 @@ extern void XtFree(
>      char*		/* ptr */
>  );
>  
> +#ifndef _X_RESTRICT_KYWD
> +# define _X_RESTRICT_KYWD
> +#endif
> +extern int XtAsprintf(
> +    char **ret,
> +    _Xconst char * _X_RESTRICT_KYWD format,
> +    ...
> +) _X_ATTRIBUTE_PRINTF(2,3);
> +
>  #ifdef XTTRACEMEMORY
>  
>  extern char *_XtMalloc( /* implementation-private */
> diff --git a/src/Alloc.c b/src/Alloc.c
> index 94cb90a..caf641e 100644
> --- a/src/Alloc.c
> +++ b/src/Alloc.c
> @@ -1,5 +1,5 @@
>  /***********************************************************
> -Copyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
> +Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
>  
>  Permission is hereby granted, free of charge, to any person obtaining a
>  copy of this software and associated documentation files (the "Software"),
> @@ -82,6 +82,8 @@ in this Software without prior written authorization from The Open Group.
>  #undef _XBCOPYFUNC
>  
>  #include <stdlib.h>
> +#include <stdio.h>
> +#include <stdarg.h>
>  
>  #define Xmalloc(size) malloc((size))
>  #define Xrealloc(ptr, size) realloc((ptr), (size))
> @@ -121,6 +123,39 @@ void _XtHeapInit(
>      heap->bytes_remaining = 0;
>  }
>  

should that have a "#ifndef HAVE_ASPRINTF" ? when the system has asprintf why no use it ?

re,
 wh

> +/* Version of asprintf() using XtMalloc
> + * Not currently available in XTTRACEMEMORY version, since that would
> + * require varargs macros everywhere, which are only standard in C99 & later.
> + */
> +int XtAsprintf(
> +    char **ret,
> +    _Xconst char * _X_RESTRICT_KYWD format,
> +    ...)
> +{
> +    char buf[256];
> +    int len;
> +    va_list ap;
> +
> +    va_start(ap, format);
> +    len = vsnprintf(buf, sizeof(buf), format, ap);
> +    va_end(ap);
> +
> +    len += 1; /* snprintf doesn't count trailing '\0' */
> +    *ret = XtMalloc(len);
> +    if (len <= sizeof(buf))
> +    {
> +	strncpy(*ret, buf, len);
> +    }
> +    else
> +    {
> +	va_start(ap, format);
> +	vsnprintf(*ret, len, format, ap);
> +	va_end(ap);
> +    }
> +    return len;
> +}
> +
> +
>  #ifndef XTTRACEMEMORY
>  
>  char *XtMalloc(


More information about the xorg-devel mailing list