2 * Raster error handling for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include <cups/raster-private.h>
27 typedef struct _cups_raster_error_s
/**** Error buffer structure ****/
29 char *start
, /* Start of buffer */
30 *current
, /* Current position in buffer */
31 *end
; /* End of buffer */
32 } _cups_raster_error_t
;
39 static _cups_raster_error_t
*get_error_buffer(void);
43 * '_cupsRasterAddError()' - Add an error message to the error buffer.
47 _cupsRasterAddError(const char *f
, /* I - Printf-style error message */
48 ...) /* I - Additional arguments as needed */
50 _cups_raster_error_t
*buf
= get_error_buffer();
52 va_list ap
; /* Pointer to additional arguments */
53 char s
[2048]; /* Message string */
54 ssize_t bytes
; /* Bytes in message string */
57 DEBUG_printf(("_cupsRasterAddError(f=\"%s\", ...)", f
));
60 bytes
= vsnprintf(s
, sizeof(s
), f
, ap
);
66 DEBUG_printf(("1_cupsRasterAddError: %s", s
));
70 if ((size_t)bytes
>= sizeof(s
))
73 if (bytes
> (ssize_t
)(buf
->end
- buf
->current
))
76 * Allocate more memory...
79 char *temp
; /* New buffer */
80 size_t size
; /* Size of buffer */
83 size
= (size_t)(buf
->end
- buf
->start
+ 2 * bytes
+ 1024);
86 temp
= realloc(buf
->start
, size
);
97 buf
->end
= temp
+ size
;
98 buf
->current
= temp
+ (buf
->current
- buf
->start
);
103 * Append the message to the end of the current string...
106 memcpy(buf
->current
, s
, (size_t)bytes
);
107 buf
->current
+= bytes
- 1;
112 * '_cupsRasterClearError()' - Clear the error buffer.
116 _cupsRasterClearError(void)
118 _cups_raster_error_t
*buf
= get_error_buffer();
122 buf
->current
= buf
->start
;
125 *(buf
->start
) = '\0';
130 * 'cupsRasterErrorString()' - Return the last error from a raster function.
132 * If there are no recent errors, NULL is returned.
134 * @since CUPS 1.3/macOS 10.5@
137 const char * /* O - Last error */
138 cupsRasterErrorString(void)
140 _cups_raster_error_t
*buf
= get_error_buffer();
144 if (buf
->current
== buf
->start
)
151 #ifdef HAVE_PTHREAD_H
153 * Implement per-thread globals...
156 # include <pthread.h>
163 static pthread_key_t raster_key
= 0; /* Thread local storage key */
164 static pthread_once_t raster_key_once
= PTHREAD_ONCE_INIT
;
165 /* One-time initialization object */
172 static void raster_init(void);
173 static void raster_destructor(void *value
);
177 * 'get_error_buffer()' - Return a pointer to thread local storage.
180 _cups_raster_error_t
* /* O - Pointer to error buffer */
181 get_error_buffer(void)
183 _cups_raster_error_t
*buf
; /* Pointer to error buffer */
187 * Initialize the global data exactly once...
190 DEBUG_puts("3get_error_buffer()");
192 pthread_once(&raster_key_once
, raster_init
);
195 * See if we have allocated the data yet...
198 if ((buf
= (_cups_raster_error_t
*)pthread_getspecific(raster_key
))
201 DEBUG_puts("4get_error_buffer: allocating memory for thread.");
204 * No, allocate memory as set the pointer for the key...
207 buf
= calloc(1, sizeof(_cups_raster_error_t
));
208 pthread_setspecific(raster_key
, buf
);
210 DEBUG_printf(("4get_error_buffer: buf=%p", (void *)buf
));
214 * Return the pointer to the data...
222 * 'raster_init()' - Initialize error buffer once.
228 pthread_key_create(&raster_key
, raster_destructor
);
230 DEBUG_printf(("3raster_init(): raster_key=%x(%u)", (unsigned)raster_key
, (unsigned)raster_key
));
235 * 'raster_destructor()' - Free memory allocated by get_error_buffer().
239 raster_destructor(void *value
) /* I - Data to free */
241 _cups_raster_error_t
*buf
= (_cups_raster_error_t
*)value
;
245 DEBUG_printf(("3raster_destructor(value=%p)", value
));
256 * Implement static globals...
260 * 'get_error_buffer()' - Return a pointer to thread local storage.
263 _cups_raster_error_t
* /* O - Pointer to error buffer */
264 get_error_buffer(void)
266 static _cups_raster_error_t buf
= { 0, 0, 0 };
272 #endif /* HAVE_PTHREAD_H */