[boilerplate] Move xasprintf to xmalloc.c

This commit is contained in:
Behdad Esfahbod 2007-04-20 00:34:51 -04:00
parent 5331445c12
commit 2e709321d8
5 changed files with 71 additions and 67 deletions

View file

@ -57,9 +57,7 @@
#include "cairo-boilerplate-xlib-private.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
@ -558,46 +556,3 @@ cairo_boilerplate_surface_set_user_data (cairo_surface_t *surface,
exit (1);
}
}
void
xasprintf (char **strp, const char *fmt, ...)
{
#ifdef HAVE_VASPRINTF
va_list va;
int ret;
va_start (va, fmt);
ret = vasprintf (strp, fmt, va);
va_end (va);
if (ret < 0) {
CAIRO_BOILERPLATE_LOG ("Error: Out of memory. Exiting.\n");
exit (1);
}
#else /* !HAVE_VASNPRINTF */
#define BUF_SIZE 1024
va_list va;
char buffer[BUF_SIZE];
int ret;
va_start (va, fmt);
ret = vsnprintf (buffer, sizeof(buffer), fmt, va);
va_end (va);
if (ret < 0) {
CAIRO_BOILERPLATE_LOG ("Failure in vsnprintf\n");
exit (1);
}
if (strlen (buffer) == sizeof(buffer) - 1) {
CAIRO_BOILERPLATE_LOG ("Overflowed fixed buffer\n");
exit (1);
}
*strp = strdup (buffer);
if (!*strp) {
CAIRO_BOILERPLATE_LOG ("Out of memory\n");
exit (1);
}
#endif /* !HAVE_VASNPRINTF */
}

View file

@ -67,18 +67,16 @@
#error Cannot find definitions for fixed-width integral types (uint8_t, uint32_t, etc.)
#endif
#include "xmalloc.h"
#ifndef CAIRO_BOILERPLATE_LOG
#define CAIRO_BOILERPLATE_LOG(...) fprintf(stderr, __VA_ARGS__)
#endif
/* A fake format we use for the flattened ARGB output of the PS and
* PDF surfaces. */
#define CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED ((unsigned int) -1)
const char *
cairo_boilerplate_content_name (cairo_content_t content);
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#define CAIRO_BOILERPLATE_PRINTF_FORMAT(fmt_index, va_index) \
__attribute__((__format__(__printf__, fmt_index, va_index)))
#else
#define CAIRO_BOILERPLATE_PRINTF_FORMAT(fmt_index, va_index)
#endif
#ifndef FALSE
#define FALSE 0
@ -88,6 +86,14 @@ cairo_boilerplate_content_name (cairo_content_t content);
#define TRUE 1
#endif
/* A fake format we use for the flattened ARGB output of the PS and
* PDF surfaces. */
#define CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED ((unsigned int) -1)
const char *
cairo_boilerplate_content_name (cairo_content_t content);
typedef enum {
CAIRO_BOILERPLATE_MODE_TEST,
CAIRO_BOILERPLATE_MODE_PERF
@ -130,20 +136,12 @@ cairo_boilerplate_get_targets (int *num_targets, cairo_bool_t *limited_targets);
void
cairo_boilerplate_free_targets (cairo_boilerplate_target_t **targets);
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#define CAIRO_PRINTF_FORMAT(fmt_index, va_index) \
__attribute__((__format__(__printf__, fmt_index, va_index)))
#else
#define CAIRO_PRINTF_FORMAT(fmt_index, va_index)
#endif
void
cairo_boilerplate_surface_set_user_data (cairo_surface_t *surface,
const cairo_user_data_key_t *key,
void *user_data,
cairo_destroy_func_t destroy);
void
xasprintf (char **strp, const char *fmt, ...) CAIRO_PRINTF_FORMAT(2, 3);
#include "xmalloc.h"
#endif

View file

@ -23,12 +23,13 @@
* Author: Carl D. Worth <cworth@cworth.org>
*/
#include <stdio.h>
#include <stdlib.h>
#include "cairo-boilerplate.h"
#include "xmalloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void *
xmalloc (size_t size)
{
@ -68,3 +69,46 @@ xrealloc (void *buf, size_t size)
return buf;
}
void
xasprintf (char **strp, const char *fmt, ...)
{
#ifdef HAVE_VASPRINTF
va_list va;
int ret;
va_start (va, fmt);
ret = vasprintf (strp, fmt, va);
va_end (va);
if (ret < 0) {
CAIRO_BOILERPLATE_LOG ("Error: Out of memory. Exiting.\n");
exit (1);
}
#else /* !HAVE_VASNPRINTF */
#define BUF_SIZE 1024
va_list va;
char buffer[BUF_SIZE];
int ret;
va_start (va, fmt);
ret = vsnprintf (buffer, sizeof(buffer), fmt, va);
va_end (va);
if (ret < 0) {
CAIRO_BOILERPLATE_LOG ("Failure in vsnprintf\n");
exit (1);
}
if (strlen (buffer) == sizeof(buffer) - 1) {
CAIRO_BOILERPLATE_LOG ("Overflowed fixed buffer\n");
exit (1);
}
*strp = strdup (buffer);
if (!*strp) {
CAIRO_BOILERPLATE_LOG ("Out of memory\n");
exit (1);
}
#endif /* !HAVE_VASNPRINTF */
}

View file

@ -26,15 +26,22 @@
#ifndef _XMALLOC_H_
#define _XMALLOC_H_
#include <stdlib.h>
#include "cairo-boilerplate.h"
#define xmalloc cairo_boilerplate_xmalloc
void *
xmalloc (size_t size);
#define xcalloc cairo_boilerplate_xcalloc
void *
xcalloc (size_t nmemb, size_t size);
#define xrealloc cairo_boilerplate_recalloc
void *
xrealloc (void *buf, size_t size);
#define xasprintf cairo_boilerplate_xasprintf
void
xasprintf (char **strp, const char *fmt, ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 3);
#endif

View file

@ -118,7 +118,7 @@ cairo_test_fini (void);
/* Print a message to the log file, ala printf. */
void
cairo_test_log (const char *fmt, ...) CAIRO_PRINTF_FORMAT(1, 2);
cairo_test_log (const char *fmt, ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(1, 2);
/* Helper functions that take care of finding source images even when
* building in a non-srcdir manner, (ie. the tests will be run in a