1
/* Copyright (C) 2001-2007 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: stdpre.h 8250 2007-09-25 13:31:24Z giles $ */
15
/* Standard definitions for Ghostscript code not needing arch.h */
17
#ifndef stdpre_INCLUDED
18
# define stdpre_INCLUDED
20
/* Ghostscript uses transitional LFS functions. */
21
#define _LARGEFILE64_SOURCE 1
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__.
33
* We arrange to define __MSDOS__ on all the MS-DOS platforms.
35
#if (defined(MSDOS) || defined(_MSDOS)) && !defined(__MSDOS__)
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.
50
* We make fairly heroic efforts to confine all uses of these flags to
51
* header files, and never to use them in code.
53
#if defined(__osf__) && !defined(__OSF__)
54
# define __OSF__ /* */
56
#if defined(M_SYSV) && !defined(SYSV)
59
#if defined(M_SYS3) && !defined(__SVR3)
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__ /* */
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. */
73
# define __FILE__ NULL
79
/* Disable 'const' and 'volatile' if the compiler can't handle them. */
80
#ifndef __PROTOTYPES__
84
# define volatile /* */
87
/* Disable 'inline' if the compiler can't handle it. */
90
# define inline __inline
93
/* Define inline as __inline__ so -pedantic won't produce a warning. */
95
# define inline __inline__
97
# if !(defined(__MWERKS__) || defined(inline))
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.
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.
114
* #define extern_inline inline
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.
121
# define extern_inline extern inline
125
* To include an inline procedure xyz in a header file abc.h, use the
126
* following template in the header file:
128
extern_inline int xyz(<<parameters>>)
129
#if HAVE_EXTERN_INLINE || defined(INLINE_INCLUDE_xyz)
137
* And use the following in whichever .c file takes responsibility for
138
* including the closed copy of xyz:
140
#define EXTERN_INCLUDE_xyz // must precede all #includes
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.
150
* The following is generic code that does not need per-compiler
154
# define HAVE_EXTERN_INLINE 1
156
# define extern_inline /* */
157
# define HAVE_EXTERN_INLINE 0
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)
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.
179
# pragma disable_message(124);
183
* Some versions of gcc have a bug such that after
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.
192
# if __GNUC__ == 2 & (7 < __GNUC_MINOR__ <= 95)
193
# define ALIGNMENT_ALIASING_BUG
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:
204
#define size_of(x) ((int)(sizeof(x)))
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.
213
#define far_data /* */
216
* Get the number of elements of a statically dimensioned array.
217
* Note that this also works on array members of structures.
219
#define countof(a) (sizeof(a) / sizeof((a)[0]))
220
#define count_of(a) (size_of(a) / size_of((a)[0]))
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).
231
#define offset_of(type, memb)\
232
((int) &((type *) 0)->memb)
234
#define offset_of(type, memb)\
235
((int) ( (char *)&((type *)0)->memb - (char *)((type *)0) ))
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.
243
#define ALIGNMENT_MOD(ptr, modu)\
244
((uint)( ((const char *)(ptr) - (const char *)0) & ((modu) - 1) ))
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;
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.
258
* NOTE: This requires that you include std.h *before* any other
259
* header file that includes sys/types.h.
262
#define bool bool_ /* (maybe not needed) */
265
#define ushort ushort_
267
#include <sys/types.h>
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
286
typedef unsigned char bool;
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.
298
#define false ((bool)0)
300
#define true ((bool)1)
301
#endif /* __MACOS__ */
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.
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.
322
#if defined(__TURBOC__) || defined(_MSC_VER)
323
typedef unsigned long ptr_ord_t;
325
typedef const char *ptr_ord_t;
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))
336
/* Define min and max, but make sure to use the identical definition */
337
/* to the one that all the compilers seem to have.... */
339
# define min(a, b) (((a) < (b)) ? (a) : (b))
342
# define max(a, b) (((a) > (b)) ? (a) : (b))
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)
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.
366
typedef double floatp;
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.
379
#define END } while (0)
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.
386
# define DO_NOTHING BEGIN END
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.
394
typedef const char *client_name_t;
395
/****** WHAT TO DO ABOUT client_name_string ? ******/
396
#define client_name_string(cname) (cname)
399
* Define the now-deprecated Pn macros for pre-ANSI compiler compatibility.
400
* The double-inclusion check is replicated here because of the way that
401
* jconfig.h is constructed.
403
#ifndef stdpn_INCLUDED
404
# define stdpn_INCLUDED
406
#endif /* stdpn_INCLUDED */
409
* Define success and failure codes for 'exit'. The only system on which
410
* they are different is VMS with older DEC C versions. We aren't sure
411
* in what version DEC C started being compatible with the rest of the
412
* world, and we don't know what the story is with VAX C. If you have
413
* problems, uncomment the following line or add -DOLD_VMS_C to the C
416
/*#define OLD_VMS_C*/
418
# define exit_FAILED 18
419
# if (defined(OLD_VMS_C) || !defined(__DECC))
426
# define exit_FAILED 1
429
#endif /* stdpre_INCLUDED */