xref: /plan9/sys/src/cmd/gs/src/std.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1989-2003 artofcode LLC. All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: std.h,v 1.12 2004/08/04 19:36:13 stefan Exp $ */
18 /* Standard definitions for Ghostscript code */
19 
20 #ifndef std_INCLUDED
21 #  define std_INCLUDED
22 
23 #include "stdpre.h"
24 
25 /* Include the architecture definitions. */
26 #include "arch.h"
27 
28 /*
29  * Define lower-case versions of the architecture parameters for backward
30  * compatibility.
31  */
32 #define arch_align_short_mod ARCH_ALIGN_SHORT_MOD
33 #define arch_align_int_mod ARCH_ALIGN_INT_MOD
34 #define arch_align_long_mod ARCH_ALIGN_LONG_MOD
35 #define arch_align_ptr_mod ARCH_ALIGN_PTR_MOD
36 #define arch_align_float_mod ARCH_ALIGN_FLOAT_MOD
37 #define arch_align_double_mod ARCH_ALIGN_DOUBLE_MOD
38 #define arch_align_struct_mod ARCH_ALIGN_STRUCT_MOD
39 #define arch_log2_sizeof_short ARCH_LOG2_SIZEOF_SHORT
40 #define arch_log2_sizeof_int ARCH_LOG2_SIZEOF_INT
41 #define arch_log2_sizeof_long ARCH_LOG2_SIZEOF_LONG
42 #define arch_sizeof_ptr ARCH_SIZEOF_PTR
43 #define arch_sizeof_float ARCH_SIZEOF_FLOAT
44 #define arch_sizeof_double ARCH_SIZEOF_DOUBLE
45 #define arch_float_mantissa_bits ARCH_FLOAT_MANTISSA_BITS
46 #define arch_double_mantissa_bits ARCH_DOUBLE_MANTISSA_BITS
47 #define arch_max_uchar ARCH_MAX_UCHAR
48 #define arch_max_ushort ARCH_MAX_USHORT
49 #define arch_max_uint ARCH_MAX_UINT
50 #define arch_max_ulong ARCH_MAX_ULONG
51 #define arch_cache1_size ARCH_CACHE1_SIZE
52 #define arch_cache2_size ARCH_CACHE2_SIZE
53 #define arch_is_big_endian ARCH_IS_BIG_ENDIAN
54 #define arch_ptrs_are_signed ARCH_PTRS_ARE_SIGNED
55 #define arch_floats_are_IEEE ARCH_FLOATS_ARE_IEEE
56 #define arch_arith_rshift ARCH_ARITH_RSHIFT
57 #define arch_can_shift_full_long ARCH_CAN_SHIFT_FULL_LONG
58 /*
59  * Define the alignment that the memory manager must preserve.
60  * We assume all alignment moduli are powers of 2.
61  * NOTE: we require that malloc align blocks at least this strictly.
62  */
63 #define ARCH_ALIGN_MEMORY_MOD\
64   (((ARCH_ALIGN_LONG_MOD - 1) | (ARCH_ALIGN_PTR_MOD - 1) |\
65     (ARCH_ALIGN_DOUBLE_MOD - 1) | (ARCH_ALIGN_STRUCT_MOD - 1)) + 1)
66 #define arch_align_memory_mod ARCH_ALIGN_MEMORY_MOD
67 
68 /* Define integer data type sizes in terms of log2s. */
69 #define ARCH_SIZEOF_CHAR (1 << ARCH_LOG2_SIZEOF_CHAR)
70 #define ARCH_SIZEOF_SHORT (1 << ARCH_LOG2_SIZEOF_SHORT)
71 #define ARCH_SIZEOF_INT (1 << ARCH_LOG2_SIZEOF_INT)
72 #define ARCH_SIZEOF_LONG (1 << ARCH_LOG2_SIZEOF_LONG)
73 #define ARCH_SIZEOF_LONG_LONG (1 << ARCH_LOG2_SIZEOF_LONG_LONG)
74 #define ARCH_INTS_ARE_SHORT (ARCH_SIZEOF_INT == ARCH_SIZEOF_SHORT)
75 /* Backward compatibility */
76 #define arch_sizeof_short ARCH_SIZEOF_SHORT
77 #define arch_sizeof_int ARCH_SIZEOF_INT
78 #define arch_sizeof_long ARCH_SIZEOF_LONG
79 #define arch_ints_are_short ARCH_INTS_ARE_SHORT
80 
81 /* Define whether we are on a large- or small-memory machine. */
82 /* Currently, we assume small memory and 16-bit ints are synonymous. */
83 #define ARCH_SMALL_MEMORY (ARCH_SIZEOF_INT <= 2)
84 /* Backward compatibility */
85 #define arch_small_memory ARCH_SMALL_MEMORY
86 
87 /* Define unsigned 16- and 32-bit types.  These are needed in */
88 /* a surprising number of places that do bit manipulation. */
89 #if arch_sizeof_short == 2	/* no plausible alternative! */
90 typedef ushort bits16;
91 #endif
92 #if arch_sizeof_int == 4
93 typedef uint bits32;
94 #else
95 # if arch_sizeof_long == 4
96 typedef ulong bits32;
97 # endif
98 #endif
99 
100 /* Minimum and maximum values for the signed types. */
101 /* Avoid casts, to make them acceptable to strict ANSI compilers. */
102 #define min_short (-1 << (arch_sizeof_short * 8 - 1))
103 #define max_short (~min_short)
104 #define min_int (-1 << (arch_sizeof_int * 8 - 1))
105 #define max_int (~min_int)
106 #define min_long (-1L << (arch_sizeof_long * 8 - 1))
107 #define max_long (~min_long)
108 
109 /*
110  * The maximum values for the unsigned types are defined in arch.h,
111  * because so many compilers handle unsigned constants wrong.
112  * In particular, most of the DEC VMS compilers incorrectly sign-extend
113  * short unsigned constants (but not short unsigned variables) when
114  * widening them to longs.  We program around this on a case-by-case basis.
115  * Some compilers don't truncate constants when they are cast down.
116  * The UTek compiler does special weird things of its own.
117  * All the rest (including gcc on all platforms) do the right thing.
118  */
119 #define max_uchar arch_max_uchar
120 #define max_ushort arch_max_ushort
121 #define max_uint arch_max_uint
122 #define max_ulong arch_max_ulong
123 
124 /* Minimum and maximum values for pointers. */
125 #if arch_ptrs_are_signed
126 #  define min_ptr min_long
127 #  define max_ptr max_long
128 #else
129 #  define min_ptr ((ulong)0)
130 #  define max_ptr max_ulong
131 #endif
132 
133 /* Define a reliable arithmetic right shift. */
134 /* Must use arith_rshift_1 for a shift by a literal 1. */
135 #define arith_rshift_slow(x,n) ((x) < 0 ? ~(~(x) >> (n)) : (x) >> (n))
136 #if arch_arith_rshift == 2
137 #  define arith_rshift(x,n) ((x) >> (n))
138 #  define arith_rshift_1(x) ((x) >> 1)
139 #else
140 #if arch_arith_rshift == 1	/* OK except for n=1 */
141 #  define arith_rshift(x,n) ((x) >> (n))
142 #  define arith_rshift_1(x) arith_rshift_slow(x,1)
143 #else
144 #  define arith_rshift(x,n) arith_rshift_slow(x,n)
145 #  define arith_rshift_1(x) arith_rshift_slow(x,1)
146 #endif
147 #endif
148 
149 /*
150  * Standard error printing macros.
151  * Use dprintf for messages that just go to dpf;
152  * dlprintf for messages to dpf with optional with file name (and,
153  * if available, line number);
154  * eprintf for error messages to epf that include the program name;
155  * lprintf for debugging messages that should include line number info.
156  * Since we all stdout/stderr output must go via outprintf/errprintf,
157  * we have to define dputc and dputs in terms of errprintf also.
158  */
159 
160 /*
161  * We would prefer not to include stdio.h here, but we need it for
162  * the FILE * argument of the printing procedures.
163  */
164 #include <stdio.h>
165 
166 /*
167  * Not a very good place to define this, but we can't find a better one.
168  */
169 #ifndef gs_memory_DEFINED
170 #  define gs_memory_DEFINED
171 typedef struct gs_memory_s gs_memory_t;
172 #endif
173 
174 #define init_proc(proc)\
175   int proc(gs_memory_t *)
176 
177 
178 /* dpf and epf may be redefined */
179 #define dpf errprintf
180 #define epf errprintf
181 
182 /* To allow stdout and stderr to be redirected, all stdout goes
183  * though outwrite and all stderr goes through errwrite.
184  */
185 int outwrite(const gs_memory_t *mem, const char *str, int len);
186 int errwrite(const char *str, int len);
187 void outflush(const gs_memory_t *mem);
188 void errflush(void);
189 /* Formatted output to outwrite and errwrite.
190  * The maximum string length is 1023 characters.
191  */
192 #ifdef __PROTOTYPES__
193 int outprintf(const gs_memory_t *mem, const char *fmt, ...);
194 int errprintf(const char *fmt, ...);
195 #else
196 int outprintf();
197 int errprintf();
198 #endif
199 
200 /* Print the program line # for debugging. */
201 #if __LINE__			/* compiler provides it */
202 void dprintf_file_and_line(const char *, int);
203 #  define _dpl dprintf_file_and_line(__FILE__, __LINE__),
204 #else
205 void dprintf_file_only(const char *);
206 #  define _dpl dprintf_file_only(__FILE__),
207 #endif
208 
209 void dflush(void);		/* flush stderr */
210 #define dputc(chr) dprintf1("%c", chr)
211 #define dlputc(chr) dlprintf1("%c", chr)
212 #define dputs(str) dprintf1("%s", str)
213 #define dlputs(str) dlprintf1("%s", str)
214 #define dprintf(str)\
215   dpf(str)
216 #define dlprintf(str)\
217   (_dpl dpf(str))
218 #define dprintf1(str,arg1)\
219   dpf(str, arg1)
220 #define dlprintf1(str,arg1)\
221   (_dpl dprintf1(str, arg1))
222 #define dprintf2(str,arg1,arg2)\
223   dpf(str, arg1, arg2)
224 #define dlprintf2(str,arg1,arg2)\
225   (_dpl dprintf2(str, arg1, arg2))
226 #define dprintf3(str,arg1,arg2,arg3)\
227   dpf(str, arg1, arg2, arg3)
228 #define dlprintf3(str,arg1,arg2,arg3)\
229   (_dpl dprintf3(str, arg1, arg2, arg3))
230 #define dprintf4(str,arg1,arg2,arg3,arg4)\
231   dpf(str, arg1, arg2, arg3, arg4)
232 #define dlprintf4(str,arg1,arg2,arg3,arg4)\
233   (_dpl dprintf4(str, arg1, arg2, arg3, arg4))
234 #define dprintf5(str,arg1,arg2,arg3,arg4,arg5)\
235   dpf(str, arg1, arg2, arg3, arg4, arg5)
236 #define dlprintf5(str,arg1,arg2,arg3,arg4,arg5)\
237   (_dpl dprintf5(str, arg1, arg2, arg3, arg4, arg5))
238 #define dprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
239   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6)
240 #define dlprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
241   (_dpl dprintf6(str, arg1, arg2, arg3, arg4, arg5, arg6))
242 #define dprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
243   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
244 #define dlprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
245   (_dpl dprintf7(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
246 #define dprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
247   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
248 #define dlprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
249   (_dpl dprintf8(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
250 #define dprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
251   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
252 #define dlprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
253   (_dpl dprintf9(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
254 #define dprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
255   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
256 #define dlprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
257   (_dpl dprintf10(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
258 #define dprintf11(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11)\
259   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
260 #define dlprintf11(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11)\
261   (_dpl dprintf11(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11))
262 #define dprintf12(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12)\
263   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12)
264 #define dlprintf12(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12)\
265   (_dpl dprintf12(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12))
266 
267 void printf_program_ident(const gs_memory_t *mem, const char *program_name, long revision_number);
268 void eprintf_program_ident(const char *program_name, long revision_number);
269 const char *gs_program_name(void);
270 long gs_revision_number(void);
271 
272 #define _epi eprintf_program_ident(gs_program_name(), gs_revision_number()),
273 
274 #define eprintf(str)\
275   (_epi epf(str))
276 #define eprintf1(str,arg1)\
277   (_epi epf(str, arg1))
278 #define eprintf2(str,arg1,arg2)\
279   (_epi epf(str, arg1, arg2))
280 #define eprintf3(str,arg1,arg2,arg3)\
281   (_epi epf(str, arg1, arg2, arg3))
282 #define eprintf4(str,arg1,arg2,arg3,arg4)\
283   (_epi epf(str, arg1, arg2, arg3, arg4))
284 #define eprintf5(str,arg1,arg2,arg3,arg4,arg5)\
285   (_epi epf(str, arg1, arg2, arg3, arg4, arg5))
286 #define eprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
287   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6))
288 #define eprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
289   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
290 #define eprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
291   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
292 #define eprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
293   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
294 #define eprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
295   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
296 
297 #if __LINE__			/* compiler provides it */
298 void lprintf_file_and_line(const char *, int);
299 #  define _epl _epi lprintf_file_and_line(__FILE__, __LINE__),
300 #else
301 void lprintf_file_only(const char *);
302 #  define _epl _epi lprintf_file_only(__FILE__)
303 #endif
304 
305 #define lprintf(str)\
306   (_epl epf(str))
307 #define lprintf1(str,arg1)\
308   (_epl epf(str, arg1))
309 #define lprintf2(str,arg1,arg2)\
310   (_epl epf(str, arg1, arg2))
311 #define lprintf3(str,arg1,arg2,arg3)\
312   (_epl epf(str, arg1, arg2, arg3))
313 #define lprintf4(str,arg1,arg2,arg3,arg4)\
314   (_epl epf(str, arg1, arg2, arg3, arg4))
315 #define lprintf5(str,arg1,arg2,arg3,arg4,arg5)\
316   (_epl epf(str, arg1, arg2, arg3, arg4, arg5))
317 #define lprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
318   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6))
319 #define lprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
320   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
321 #define lprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
322   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
323 #define lprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
324   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
325 #define lprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
326   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
327 
328 /*
329  * Define the prototype for module initialization procedures.  This is not
330  * a very good place to define this, but we can't find a better one.
331  */
332 #ifndef gs_memory_DEFINED
333 #  define gs_memory_DEFINED
334 typedef struct gs_memory_s gs_memory_t;
335 #endif
336 #define init_proc(proc)\
337   int proc(gs_memory_t *)
338 
339 #endif /* std_INCLUDED */
340