4 * Raster error handling for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include <cups/raster-private.h>
29 typedef struct _cups_raster_error_s
/**** Error buffer structure ****/
31 char *start
, /* Start of buffer */
32 *current
, /* Current position in buffer */
33 *end
; /* End of buffer */
34 } _cups_raster_error_t
;
41 static _cups_raster_error_t
*get_error_buffer(void);
45 * '_cupsRasterAddError()' - Add an error message to the error buffer.
49 _cupsRasterAddError(const char *f
, /* I - Printf-style error message */
50 ...) /* I - Additional arguments as needed */
52 _cups_raster_error_t
*buf
= get_error_buffer();
54 va_list ap
; /* Pointer to additional arguments */
55 char s
[2048]; /* Message string */
56 ssize_t bytes
; /* Bytes in message string */
60 bytes
= vsnprintf(s
, sizeof(s
), f
, ap
);
68 if ((size_t)bytes
>= sizeof(s
))
71 if (bytes
> (ssize_t
)(buf
->end
- buf
->current
))
74 * Allocate more memory...
77 char *temp
; /* New buffer */
78 size_t size
; /* Size of buffer */
81 size
= (size_t)(buf
->end
- buf
->start
+ 2 * bytes
+ 1024);
84 temp
= realloc(buf
->start
, size
);
95 buf
->end
= temp
+ size
;
96 buf
->current
= temp
+ (buf
->current
- buf
->start
);
101 * Append the message to the end of the current string...
104 memcpy(buf
->current
, s
, (size_t)bytes
);
105 buf
->current
+= bytes
- 1;
110 * '_cupsRasterClearError()' - Clear the error buffer.
114 _cupsRasterClearError(void)
116 _cups_raster_error_t
*buf
= get_error_buffer();
120 buf
->current
= buf
->start
;
123 *(buf
->start
) = '\0';
128 * 'cupsRasterErrorString()' - Return the last error from a raster function.
130 * If there are no recent errors, NULL is returned.
132 * @since CUPS 1.3/OS X 10.5@
135 const char * /* O - Last error */
136 cupsRasterErrorString(void)
138 _cups_raster_error_t
*buf
= get_error_buffer();
142 if (buf
->current
== buf
->start
)
149 #ifdef HAVE_PTHREAD_H
151 * Implement per-thread globals...
154 # include <pthread.h>
161 static pthread_key_t raster_key
= 0; /* Thread local storage key */
162 static pthread_once_t raster_key_once
= PTHREAD_ONCE_INIT
;
163 /* One-time initialization object */
170 static void raster_init(void);
171 static void raster_destructor(void *value
);
175 * 'get_error_buffer()' - Return a pointer to thread local storage.
178 _cups_raster_error_t
* /* O - Pointer to error buffer */
179 get_error_buffer(void)
181 _cups_raster_error_t
*buf
; /* Pointer to error buffer */
185 * Initialize the global data exactly once...
188 DEBUG_puts("get_error_buffer()");
190 pthread_once(&raster_key_once
, raster_init
);
193 * See if we have allocated the data yet...
196 if ((buf
= (_cups_raster_error_t
*)pthread_getspecific(raster_key
))
199 DEBUG_puts("get_error_buffer: allocating memory for thread...");
202 * No, allocate memory as set the pointer for the key...
205 buf
= calloc(1, sizeof(_cups_raster_error_t
));
206 pthread_setspecific(raster_key
, buf
);
208 DEBUG_printf((" buf=%p\n", buf
));
212 * Return the pointer to the data...
220 * 'raster_init()' - Initialize error buffer once.
226 pthread_key_create(&raster_key
, raster_destructor
);
228 DEBUG_printf(("raster_init(): raster_key=%x(%u)\n", (unsigned)raster_key
,
229 (unsigned)raster_key
));
234 * 'raster_destructor()' - Free memory allocated by get_error_buffer().
238 raster_destructor(void *value
) /* I - Data to free */
240 _cups_raster_error_t
*buf
= (_cups_raster_error_t
*)value
;
244 DEBUG_printf(("raster_destructor(value=%p)\n", value
));
255 * Implement static globals...
259 * 'get_error_buffer()' - Return a pointer to thread local storage.
262 _cups_raster_error_t
* /* O - Pointer to error buffer */
263 get_error_buffer(void)
265 static _cups_raster_error_t buf
= { 0, 0, 0 };
271 #endif /* HAVE_PTHREAD_H */