2 * "$Id: error.c 9771 2011-05-12 05:21:56Z mike $"
4 * Raster error handling for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * _cupsRasterAddError() - Add an error message to the error buffer.
20 * _cupsRasterClearError() - Clear the error buffer.
21 * cupsRasterErrorString() - Return the last error from a raster function.
22 * get_error_buffer() - Return a pointer to thread local storage.
23 * raster_init() - Initialize error buffer once.
24 * raster_destructor() - Free memory allocated by get_error_buffer().
28 * Include necessary headers...
31 #include "image-private.h"
39 typedef struct _cups_raster_error_s /**** Error buffer structure ****/
41 char *start, /* Start of buffer */
42 *current, /* Current position in buffer */
43 *end; /* End of buffer */
44 } _cups_raster_error_t;
51 static _cups_raster_error_t *get_error_buffer(void);
55 * '_cupsRasterAddError()' - Add an error message to the error buffer.
59 _cupsRasterAddError(const char *f, /* I - Printf-style error message */
60 ...) /* I - Additional arguments as needed */
62 _cups_raster_error_t *buf = get_error_buffer();
64 va_list ap; /* Pointer to additional arguments */
65 char s[2048]; /* Message string */
66 size_t bytes; /* Bytes in message string */
70 bytes = vsnprintf(s, sizeof(s), f, ap);
78 if (bytes >= sizeof(s))
81 if (bytes > (size_t)(buf->end - buf->current))
84 * Allocate more memory...
87 char *temp; /* New buffer */
88 size_t size; /* Size of buffer */
91 size = buf->end - buf->start + 2 * bytes + 1024;
94 temp = realloc(buf->start, size);
105 buf->end = temp + size;
106 buf->current = temp + (buf->current - buf->start);
111 * Append the message to the end of the current string...
114 memcpy(buf->current, s, bytes);
115 buf->current += bytes - 1;
120 * '_cupsRasterClearError()' - Clear the error buffer.
124 _cupsRasterClearError(void)
126 _cups_raster_error_t *buf = get_error_buffer();
130 buf->current = buf->start;
133 *(buf->start) = '\0';
138 * 'cupsRasterErrorString()' - Return the last error from a raster function.
140 * If there are no recent errors, NULL is returned.
142 * @since CUPS 1.3/Mac OS X 10.5@
145 const char * /* O - Last error */
146 cupsRasterErrorString(void)
148 _cups_raster_error_t *buf = get_error_buffer();
152 if (buf->current == buf->start)
159 #ifdef HAVE_PTHREAD_H
161 * Implement per-thread globals...
164 # include <pthread.h>
171 static pthread_key_t raster_key = -1;
172 /* Thread local storage key */
173 static pthread_once_t raster_key_once = PTHREAD_ONCE_INIT;
174 /* One-time initialization object */
181 static void raster_init(void);
182 static void raster_destructor(void *value);
186 * 'get_error_buffer()' - Return a pointer to thread local storage.
189 _cups_raster_error_t * /* O - Pointer to error buffer */
190 get_error_buffer(void)
192 _cups_raster_error_t *buf; /* Pointer to error buffer */
196 * Initialize the global data exactly once...
199 DEBUG_puts("get_error_buffer()");
201 pthread_once(&raster_key_once, raster_init);
204 * See if we have allocated the data yet...
207 if ((buf = (_cups_raster_error_t *)pthread_getspecific(raster_key))
210 DEBUG_puts("get_error_buffer: allocating memory for thread...");
213 * No, allocate memory as set the pointer for the key...
216 buf = calloc(1, sizeof(_cups_raster_error_t));
217 pthread_setspecific(raster_key, buf);
219 DEBUG_printf((" buf=%p\n", buf));
223 * Return the pointer to the data...
231 * 'raster_init()' - Initialize error buffer once.
237 pthread_key_create(&raster_key, raster_destructor);
239 DEBUG_printf(("raster_init(): raster_key=%x(%u)\n", (unsigned)raster_key,
240 (unsigned)raster_key));
245 * 'raster_destructor()' - Free memory allocated by get_error_buffer().
249 raster_destructor(void *value) /* I - Data to free */
251 _cups_raster_error_t *buf = (_cups_raster_error_t *)value;
255 DEBUG_printf(("raster_destructor(value=%p)\n", value));
266 * Implement static globals...
270 * 'get_error_buffer()' - Return a pointer to thread local storage.
273 _cups_raster_error_t * /* O - Pointer to error buffer */
274 get_error_buffer(void)
276 static _cups_raster_error_t buf = { 0, 0, 0 };
282 #endif /* HAVE_PTHREAD_H */
286 * End of "$Id: error.c 9771 2011-05-12 05:21:56Z mike $".