2 * "$Id: error.c 7460 2008-04-16 02:19:54Z mike $"
4 * Raster error handling for CUPS.
6 * Copyright 2007-2012 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 <cups/raster-private.h>
38 typedef struct _cups_raster_error_s
/**** Error buffer structure ****/
40 char *start
, /* Start of buffer */
41 *current
, /* Current position in buffer */
42 *end
; /* End of buffer */
43 } _cups_raster_error_t
;
50 static _cups_raster_error_t
*get_error_buffer(void);
54 * '_cupsRasterAddError()' - Add an error message to the error buffer.
58 _cupsRasterAddError(const char *f
, /* I - Printf-style error message */
59 ...) /* I - Additional arguments as needed */
61 _cups_raster_error_t
*buf
= get_error_buffer();
63 va_list ap
; /* Pointer to additional arguments */
64 char s
[2048]; /* Message string */
65 size_t bytes
; /* Bytes in message string */
69 bytes
= vsnprintf(s
, sizeof(s
), f
, ap
);
77 if (bytes
>= sizeof(s
))
80 if (bytes
> (size_t)(buf
->end
- buf
->current
))
83 * Allocate more memory...
86 char *temp
; /* New buffer */
87 size_t size
; /* Size of buffer */
90 size
= buf
->end
- buf
->start
+ 2 * bytes
+ 1024;
93 temp
= realloc(buf
->start
, size
);
104 buf
->end
= temp
+ size
;
105 buf
->current
= temp
+ (buf
->current
- buf
->start
);
110 * Append the message to the end of the current string...
113 memcpy(buf
->current
, s
, bytes
);
114 buf
->current
+= bytes
- 1;
119 * '_cupsRasterClearError()' - Clear the error buffer.
123 _cupsRasterClearError(void)
125 _cups_raster_error_t
*buf
= get_error_buffer();
129 buf
->current
= buf
->start
;
132 *(buf
->start
) = '\0';
137 * 'cupsRasterErrorString()' - Return the last error from a raster function.
139 * If there are no recent errors, NULL is returned.
141 * @since CUPS 1.3/OS X 10.5@
144 const char * /* O - Last error */
145 cupsRasterErrorString(void)
147 _cups_raster_error_t
*buf
= get_error_buffer();
151 if (buf
->current
== buf
->start
)
158 #ifdef HAVE_PTHREAD_H
160 * Implement per-thread globals...
163 # include <pthread.h>
170 static pthread_key_t raster_key
= -1;
171 /* Thread local storage key */
172 static pthread_once_t raster_key_once
= PTHREAD_ONCE_INIT
;
173 /* One-time initialization object */
180 static void raster_init(void);
181 static void raster_destructor(void *value
);
185 * 'get_error_buffer()' - Return a pointer to thread local storage.
188 _cups_raster_error_t
* /* O - Pointer to error buffer */
189 get_error_buffer(void)
191 _cups_raster_error_t
*buf
; /* Pointer to error buffer */
195 * Initialize the global data exactly once...
198 DEBUG_puts("get_error_buffer()");
200 pthread_once(&raster_key_once
, raster_init
);
203 * See if we have allocated the data yet...
206 if ((buf
= (_cups_raster_error_t
*)pthread_getspecific(raster_key
))
209 DEBUG_puts("get_error_buffer: allocating memory for thread...");
212 * No, allocate memory as set the pointer for the key...
215 buf
= calloc(1, sizeof(_cups_raster_error_t
));
216 pthread_setspecific(raster_key
, buf
);
218 DEBUG_printf((" buf=%p\n", buf
));
222 * Return the pointer to the data...
230 * 'raster_init()' - Initialize error buffer once.
236 pthread_key_create(&raster_key
, raster_destructor
);
238 DEBUG_printf(("raster_init(): raster_key=%x(%u)\n", (unsigned)raster_key
,
239 (unsigned)raster_key
));
244 * 'raster_destructor()' - Free memory allocated by get_error_buffer().
248 raster_destructor(void *value
) /* I - Data to free */
250 _cups_raster_error_t
*buf
= (_cups_raster_error_t
*)value
;
254 DEBUG_printf(("raster_destructor(value=%p)\n", value
));
265 * Implement static globals...
269 * 'get_error_buffer()' - Return a pointer to thread local storage.
272 _cups_raster_error_t
* /* O - Pointer to error buffer */
273 get_error_buffer(void)
275 static _cups_raster_error_t buf
= { 0, 0, 0 };
281 #endif /* HAVE_PTHREAD_H */
285 * End of "$Id: error.c 7460 2008-04-16 02:19:54Z mike $".