xref: /plan9/sys/src/cmd/gs/src/stdpre.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1993-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: stdpre.h,v 1.22 2003/12/09 21:17:59 giles Exp $ */
18 /* Standard definitions for Ghostscript code not needing arch.h */
19 
20 #ifndef stdpre_INCLUDED
21 #  define stdpre_INCLUDED
22 
23 /*
24  * Here we deal with the vagaries of various C compilers.  We assume that:
25  *      ANSI-standard Unix compilers define __STDC__.
26  *      gcc defines __GNUC__.
27  *      Borland Turbo C and Turbo C++ define __MSDOS__ and __TURBOC__.
28  *      Borland C++ defines __BORLANDC__, __MSDOS__, and __TURBOC__.
29  *      Microsoft C/C++ defines _MSC_VER and _MSDOS.
30  *      Watcom C defines __WATCOMC__ and MSDOS.
31  *      MetroWerks C defines __MWERKS__.
32  *
33  * We arrange to define __MSDOS__ on all the MS-DOS platforms.
34  */
35 #if (defined(MSDOS) || defined(_MSDOS)) && !defined(__MSDOS__)
36 #  define __MSDOS__
37 #endif
38 /*
39  * Also, not used much here, but used in other header files, we assume:
40  *      Unix System V environments define SYSV.
41  *      The SCO ODT compiler defines M_SYSV and M_SYS3.
42  *      VMS systems define VMS.
43  *      OSF/1 compilers define __osf__ or __OSF__.
44  *        (The VMS and OSF/1 C compilers handle prototypes and const,
45  *        but do not define __STDC__.)
46  *      bsd 4.2 or 4.3 systems define BSD4_2.
47  *      POSIX-compliant environments define _POSIX_SOURCE.
48  *      Motorola 88K BCS/OCS systems defined m88k.
49  *
50  * We make fairly heroic efforts to confine all uses of these flags to
51  * header files, and never to use them in code.
52  */
53 #if defined(__osf__) && !defined(__OSF__)
54 #  define __OSF__		/* */
55 #endif
56 #if defined(M_SYSV) && !defined(SYSV)
57 #  define SYSV			/* */
58 #endif
59 #if defined(M_SYS3) && !defined(__SVR3)
60 #  define __SVR3		/* */
61 #endif
62 
63 #if defined(__STDC__) || defined(__MSDOS__) || defined(__convex__) || defined(VMS) || defined(__OSF__) || defined(__WIN32__) || defined(__IBMC__) || defined(M_UNIX) || defined(__GNUC__) || defined(__BORLANDC__)
64 # if !(defined(M_XENIX) && !defined(__GNUC__))	/* SCO Xenix cc is broken */
65 #  define __PROTOTYPES__	/* */
66 # endif
67 #endif
68 
69 /* Define dummy values for __FILE__ and __LINE__ if the compiler */
70 /* doesn't provide these.  Note that places that use __FILE__ */
71 /* must check explicitly for a null pointer. */
72 #ifndef __FILE__
73 #  define __FILE__ NULL
74 #endif
75 #ifndef __LINE__
76 #  define __LINE__ 0
77 #endif
78 
79 /* Disable 'const' and 'volatile' if the compiler can't handle them. */
80 #ifndef __PROTOTYPES__
81 #  undef const
82 #  define const			/* */
83 #  undef volatile
84 #  define volatile		/* */
85 #endif
86 
87 /* Disable 'inline' if the compiler can't handle it. */
88 #ifdef __DECC
89 #  undef inline
90 #  define inline __inline
91 #else
92 #  ifdef __GNUC__
93 /* Define inline as __inline__ so -pedantic won't produce a warning. */
94 #    undef inline
95 #    define inline __inline__
96 #  else
97 #    if !(defined(__MWERKS__) || defined(inline))
98 #      define inline		/* */
99 #    endif
100 #  endif
101 #endif
102 
103 /*
104  * Provide a way to include inline procedures in header files, regardless of
105  * whether the compiler (A) doesn't support inline at all, (B) supports it
106  * but also always compiles a closed copy, (C) supports it but somehow only
107  * includes a single closed copy in the executable, or (D) supports it and
108  * also supports a different syntax if no closed copy is desired.
109  *
110  * The code that appears just after this comment indicates which compilers
111  * are of which kind.  (Eventually this might be determined automatically.)
112  *	(A) and (B) require nothing here.
113  *	(C) requires
114  *		#define extern_inline inline
115  *	(D) requires
116  *		#define extern_inline extern inline  // or whatever
117  * Note that for case (B), the procedure will only be declared inline in
118  * the .c file where its closed copy is compiled.
119  */
120 #ifdef __GNUC__
121 #  define extern_inline extern inline
122 #endif
123 
124 /*
125  * To include an inline procedure xyz in a header file abc.h, use the
126  * following template in the header file:
127 
128 extern_inline int xyz(<<parameters>>)
129 #if HAVE_EXTERN_INLINE || defined(INLINE_INCLUDE_xyz)
130 {
131     <<body>>
132 }
133 #else
134 ;
135 #endif
136 
137  * And use the following in whichever .c file takes responsibility for
138  * including the closed copy of xyz:
139 
140 #define EXTERN_INCLUDE_xyz	// must precede all #includes
141 #include "abc.h"
142 
143  * The definitions of the EXTERN_INCLUDE_ macros must precede *all* includes
144  * because there is no way to know whether some other .h file #includes abc.h
145  * indirectly, and because of the protection against double #includes, the
146  * EXTERN_INCLUDE_s must be defined before the first inclusion of abc.h.
147  */
148 
149 /*
150  * The following is generic code that does not need per-compiler
151  * customization.
152  */
153 #ifdef extern_inline
154 #  define HAVE_EXTERN_INLINE 1
155 #else
156 #  define extern_inline /* */
157 #  define HAVE_EXTERN_INLINE 0
158 #endif
159 
160 /*
161  * Some compilers give a warning if a function call that returns a value
162  * is used as a statement; a few compilers give an error for the construct
163  * (void)0, which is contrary to the ANSI standard.  Since we don't know of
164  * any compilers that do both, we define a macro here for discarding
165  * the value of an expression statement, which can be defined as either
166  * including or not including the cast.  (We don't conditionalize this here,
167  * because no commercial compiler gives the error on (void)0, although
168  * some give warnings.)  */
169 #define DISCARD(expr) ((void)(expr))
170 /* Backward compatibility */
171 #define discard(expr) DISCARD(expr)
172 
173 /*
174  * Some versions of the Watcom compiler give a "Comparison result always
175  * 0/1" message that we want to suppress because it gets in the way of
176  * meaningful warnings.
177  */
178 #ifdef __WATCOMC__
179 #  pragma disable_message(124);
180 #endif
181 
182 /*
183  * Some versions of gcc have a bug such that after
184 	byte *p;
185 	...
186 	x = *(long *)p;
187  * the compiler then thinks that p always points to long-aligned data.
188  * Detect this here so it can be handled appropriately in the few places
189  * that (we think) matter.
190  */
191 #ifdef __GNUC__
192 # if __GNUC__ == 2 & (7 < __GNUC_MINOR__ <= 95)
193 #  define ALIGNMENT_ALIASING_BUG
194 # endif
195 #endif
196 
197 /*
198  * The SVR4.2 C compiler incorrectly considers the result of << and >>
199  * to be unsigned if the left operand is signed and the right operand is
200  * unsigned.  We believe this only causes trouble in Ghostscript code when
201  * the right operand is a sizeof(...), which is unsigned for this compiler.
202  * Therefore, we replace the relevant uses of sizeof with size_of:
203  */
204 #define size_of(x) ((int)(sizeof(x)))
205 
206 /*
207  * far_data was formerly used for static data that had to be assigned its
208  * own segment on PCs with 64K segments.  This was supported in Borland C++,
209  * but none of the other compilers.  Since we no longer support
210  * small-segment systems, far_data is vacuous.
211  */
212 #undef far_data
213 #define far_data /* */
214 
215 /*
216  * Get the number of elements of a statically dimensioned array.
217  * Note that this also works on array members of structures.
218  */
219 #define countof(a) (sizeof(a) / sizeof((a)[0]))
220 #define count_of(a) (size_of(a) / size_of((a)[0]))
221 
222 /*
223  * Get the offset of a structure member.  Amazingly enough, the simpler
224  * definition works on all compilers except for one broken MIPS compiler
225  * and the IBM RS/6000.  Unfortunately, because of these two compilers,
226  * we have to use the more complex definition.  Even more unfortunately,
227  * the more complex definition doesn't work on the MetroWerks
228  * CodeWarrior compiler (Macintosh and BeOS).
229  */
230 #ifdef __MWERKS__
231 #define offset_of(type, memb)\
232  ((int) &((type *) 0)->memb)
233 #else
234 #define offset_of(type, memb)\
235  ((int) ( (char *)&((type *)0)->memb - (char *)((type *)0) ))
236 #endif
237 
238 /*
239  * Get the alignment of a pointer modulo a given power of 2.
240  * There is no portable way to do this, but the following definition
241  * works on all reasonable systems.
242  */
243 #define ALIGNMENT_MOD(ptr, modu)\
244   ((uint)( ((const char *)(ptr) - (const char *)0) & ((modu) - 1) ))
245 
246 /* Define short names for the unsigned types. */
247 typedef unsigned char byte;
248 typedef unsigned char uchar;
249 typedef unsigned short ushort;
250 typedef unsigned int uint;
251 typedef unsigned long ulong;
252 
253 /* Since sys/types.h may define one or more of these (depending on
254  * the platform), we have to take steps to prevent name clashes.
255  * Unfortunately this can clobber valid definitions for the size-
256  * specific types, but there's no simple solution.
257  *
258  * NOTE: This requires that you include std.h *before* any other
259  * header file that includes sys/types.h.
260  *
261  */
262 #define bool bool_		/* (maybe not needed) */
263 #define uchar uchar_
264 #define uint uint_
265 #define ushort ushort_
266 #define ulong ulong_
267 #include <sys/types.h>
268 #undef bool
269 #undef uchar
270 #undef uint
271 #undef ushort
272 #undef ulong
273 
274 /*
275  * Define a Boolean type.  Even though we would like it to be
276  * unsigned char, it pretty well has to be int, because
277  * that's what all the relational operators and && and || produce.
278  * We can't make it an enumerated type, because ints don't coerce
279  * freely to enums (although the opposite is true).
280  * Unfortunately, at least some C++ compilers have a built-in bool type,
281  * and the MetroWerks C++ compiler insists that bool be equivalent to
282  * unsigned char.
283  */
284 #ifndef __cplusplus
285 #ifdef __BEOS__
286 typedef unsigned char bool;
287 #else
288 typedef int bool;
289 #endif
290 #endif
291 /*
292  * Older versions of MetroWerks CodeWarrior defined true and false, but they're now
293  * an enum in the (MacOS) Universal Interfaces. The only way around this is to escape
294  * our own definitions wherever MacTypes.h is included.
295  */
296 #ifndef __MACOS__
297 #undef false
298 #define false ((bool)0)
299 #undef true
300 #define true ((bool)1)
301 #endif /* __MACOS__ */
302 
303 /*
304  * Compilers disagree as to whether macros used in macro arguments
305  * should be expanded at the time of the call, or at the time of
306  * final expansion.  Even authoritative documents disagree: the ANSI
307  * standard says the former, but Harbison and Steele's book says the latter.
308  * In order to work around this discrepancy, we have to do some very
309  * ugly things in a couple of places.  We mention it here because
310  * it might well trip up future developers.
311  */
312 
313 /*
314  * Define the type to be used for ordering pointers (<, >=, etc.).
315  * The Borland and Microsoft large models only compare the offset part
316  * of segmented pointers.  Semantically, the right type to use for the
317  * comparison is char huge *, but we have no idea how expensive comparing
318  * such pointers is, and any type that compares all the bits of the pointer,
319  * gives the right result for pointers in the same segment, and keeps
320  * different segments disjoint will do.
321  */
322 #if defined(__TURBOC__) || defined(_MSC_VER)
323 typedef unsigned long ptr_ord_t;
324 #else
325 typedef const char *ptr_ord_t;
326 #endif
327 /* Define all the pointer comparison operations. */
328 #define _PTR_CMP(p1, rel, p2)  ((ptr_ord_t)(p1) rel (ptr_ord_t)(p2))
329 #define PTR_LE(p1, p2) _PTR_CMP(p1, <=, p2)
330 #define PTR_LT(p1, p2) _PTR_CMP(p1, <, p2)
331 #define PTR_GE(p1, p2) _PTR_CMP(p1, >=, p2)
332 #define PTR_GT(p1, p2) _PTR_CMP(p1, >, p2)
333 #define PTR_BETWEEN(ptr, lo, hi)\
334   (PTR_GE(ptr, lo) && PTR_LT(ptr, hi))
335 
336 /* Define  min and max, but make sure to use the identical definition */
337 /* to the one that all the compilers seem to have.... */
338 #ifndef min
339 #  define min(a, b) (((a) < (b)) ? (a) : (b))
340 #endif
341 #ifndef max
342 #  define max(a, b) (((a) > (b)) ? (a) : (b))
343 #endif
344 
345 /* Define a standard way to round values to a (constant) modulus. */
346 #define ROUND_DOWN(value, modulus)\
347   ( (modulus) & ((modulus) - 1) ?	/* not a power of 2 */\
348     (value) - (value) % (modulus) :\
349     (value) & -(modulus) )
350 #define ROUND_UP(value, modulus)\
351   ( (modulus) & ((modulus) - 1) ?	/* not a power of 2 */\
352     ((value) + ((modulus) - 1)) / (modulus) * (modulus) :\
353     ((value) + ((modulus) - 1)) & -(modulus) )
354 /* Backward compatibility */
355 #define round_up(v, m) ROUND_UP(v, m)
356 #define round_down(v, m) ROUND_DOWN(v, m)
357 
358 /*
359  * In pre-ANSI C, float parameters get converted to double.
360  * However, if we pass a float to a function that has been declared
361  * with a prototype, and the parameter has been declared as float,
362  * the ANSI standard specifies that the parameter is left as float.
363  * To avoid problems caused by missing prototypes,
364  * we declare almost all float parameters as double.
365  */
366 typedef double floatp;
367 
368 /*
369  * Because of C's strange insistence that ; is a terminator and not a
370  * separator, compound statements {...} are not syntactically equivalent to
371  * single statements.  Therefore, we define here a compound-statement
372  * construct that *is* syntactically equivalent to a single statement.
373  * Usage is
374  *      BEGIN
375  *        ...statements...
376  *      END
377  */
378 #define BEGIN	do {
379 #define END	} while (0)
380 
381 /*
382  * Define a handy macro for a statement that does nothing.
383  * We can't just use an empty statement, since this upsets some compilers.
384  */
385 #ifndef DO_NOTHING
386 #  define DO_NOTHING BEGIN END
387 #endif
388 
389 /*
390  * For accountability, debugging, and error messages, we pass a client
391  * identification string to alloc and free, and possibly other places as
392  * well.  Define the type for these strings.
393  */
394 typedef const char *client_name_t;
395 /****** WHAT TO DO ABOUT client_name_string ? ******/
396 #define client_name_string(cname) (cname)
397 
398 /*
399  * If we are debugging, make all static variables and procedures public
400  * so they get passed through the linker.
401  */
402 #define public			/* */
403 /*
404  * We separate out the definition of private this way so that
405  * we can temporarily #undef it to handle the X Windows headers,
406  * which define a member named private.
407  */
408 #ifdef NOPRIVATE
409 # define private_		/* */
410 #else
411 # define private_ static
412 #endif
413 #define private private_
414 
415 /*
416  * Define the now-deprecated Pn macros for pre-ANSI compiler compatibility.
417  * The double-inclusion check is replicated here because of the way that
418  * jconfig.h is constructed.
419  */
420 #ifndef stdpn_INCLUDED
421 #  define stdpn_INCLUDED
422 #include "stdpn.h"
423 #endif /* stdpn_INCLUDED */
424 
425 /*
426  * Define success and failure codes for 'exit'.  The only system on which
427  * they are different is VMS with older DEC C versions.  We aren't sure
428  * in what version DEC C started being compatible with the rest of the
429  * world, and we don't know what the story is with VAX C.  If you have
430  * problems, uncomment the following line or add -DOLD_VMS_C to the C
431  * command line.
432  */
433 /*#define OLD_VMS_C*/
434 #if defined(VMS)
435 #  define exit_FAILED 18
436 #  if (defined(OLD_VMS_C) || !defined(__DECC))
437 #    define exit_OK 1
438 #  else
439 #    define exit_OK 0
440 #  endif
441 #else
442 #  define exit_OK 0
443 #  define exit_FAILED 1
444 #endif
445 
446 #endif /* stdpre_INCLUDED */
447