1 /*-------------------------------------------------------------------------
4 * Fundamental C definitions. This is included by every .c file in
8 * Copyright (c) 1994, Regents of the University of California
10 * $Id: c.h,v 1.4 1996/11/14 06:06:39 bryanh Exp $
12 *-------------------------------------------------------------------------
17 * When adding stuff to this file, please try and put stuff
18 * into the relevant section, or add new sections as appropriate.
21 * ------- ------------------------------------------------
22 * 1) bool, true, false, TRUE, FALSE
23 * 2) __STDC__, non-ansi C definitions:
24 * Pointer typedef, NULL
26 * type prefixes: const, signed, volatile, inline
27 * 3) standard system types
29 * 5) IsValid macros for system types
30 * 6) offsetof, lengthof, endof
31 * 7) exception handling definitions, Assert, Trap, etc macros
32 * 8) Min, Max, Abs macros
34 * 10) Berkeley-specific defs
35 * 11) system-specific hacks
39 * This file is MACHINE AND COMPILER dependent!!! (For now.)
41 * ----------------------------------------------------------------
46 /* We have to include stdlib.h here because it defines many of these macros
47 on some platforms, and we only want our definitions used if stdlib.h doesn't
52 /* ----------------------------------------------------------------
53 * Section 1: bool, true, false, TRUE, FALSE
54 * ----------------------------------------------------------------
58 * Boolean value, either true or false.
61 #define false ((char) 0)
62 #define true ((char) 1)
64 typedef bool *BoolPtr;
74 /* ----------------------------------------------------------------
75 * Section 2: __STDC__, non-ansi C definitions:
78 * Pointer typedef, NULL
79 * type prefixes: const, signed, volatile, inline
80 * ----------------------------------------------------------------
83 #ifdef __STDC__ /* ANSI C */
87 * Variable holding address of any memory resident object.
91 * XXX Pointer arithmetic is done with this, so it can't be void *
92 * under "true" ANSI compilers.
94 typedef char *Pointer;
101 #define NULL ((void *) 0)
102 #endif /* !defined(NULL) */
104 #define HAVE_ANSI_CPP /* all ANSI C compilers must have this! */
105 #if defined(NEED_STD_HDRS)
106 #undef NEED_STD_HDRS /* all ANSI systems must have stddef/stdlib */
107 #endif /* NEED_STD_HDRS */
109 #else /* !defined(__STDC__) */ /* NOT ANSI C */
113 * Variable containing address of any memory resident object.
115 typedef char *Pointer;
123 #endif /* !defined(NULL) */
127 * Type modifier. Identifies read only variables.
130 * extern const Version RomVersion;
132 #define const /* const */
136 * Type modifier. Identifies signed integral types.
138 #define signed /* signed */
142 * Type modifier. Identifies variables which may change in ways not
143 * noticeable by the compiler, e.g. via asynchronous interrupts.
146 * extern volatile unsigned int NumberOfInterrupts;
148 #define volatile /* volatile */
150 #endif /* !defined(__STDC__) */ /* NOT ANSI C */
154 * Convert the argument to a string, using the C preprocessor.
156 * Concatenate two arguments together, using the C preprocessor.
158 #if defined(HAVE_ANSI_CPP)
160 #define CppAsString(identifier) #identifier
161 #define CppConcat(x, y) x##y
162 #define CppConcat0(x, y) x##y
163 #define CppConcat1(x, y) x##y
164 #define CppConcat2(x, y) x##y
165 #define CppConcat3(x, y) x##y
166 #define CppConcat4(x, y) x##y
168 #else /* !HAVE_ANSI_CPP */
170 #define CppAsString(identifier) "identifier"
173 * CppIdentity -- On Reiser based cpp's this is used to concatenate
174 * two tokens. That is
175 * CppIdentity(A)B ==> AB
176 * We renamed it to _private_CppIdentity because it should not
177 * be referenced outside this file. On other cpp's it
180 #define _priv_CppIdentity(x)x
181 #define CppConcat(x, y) _priv_CppIdentity(x)y
182 #define CppConcat0(x, y) _priv_CppIdentity(x)y
183 #define CppConcat1(x, y) _priv_CppIdentity(x)y
184 #define CppConcat2(x, y) _priv_CppIdentity(x)y
185 #define CppConcat3(x, y) _priv_CppIdentity(x)y
186 #define CppConcat4(x, y) _priv_CppIdentity(x)y
188 #endif /* !HAVE_ANSI_CPP */
190 #ifndef __GNUC__ /* GNU cc */
194 #if defined(NEED_STD_HDRS)
196 * You're doomed. We've removed almost all of our own C library
197 * extern declarations because they conflict on the different
198 * systems. You'll have to write your own stdlib.h.
201 #else /* NEED_STD_HDRS */
204 #endif /* NEED_STD_HDRS */
206 /* ----------------------------------------------------------------
207 * Section 3: standard system types
208 * ----------------------------------------------------------------
213 * Signed integer, AT LEAST N BITS IN SIZE,
214 * used for numerical computations.
216 typedef signed char int8; /* >= 8 bits */
217 typedef signed short int16; /* >= 16 bits */
218 typedef signed int int32; /* >= 32 bits */
222 * Unsigned integer, AT LEAST N BITS IN SIZE,
223 * used for numerical computations.
225 typedef unsigned char uint8; /* >= 8 bits */
226 typedef unsigned short uint16; /* >= 16 bits */
227 typedef unsigned int uint32; /* >= 32 bits */
231 * Floating point number, AT LEAST N BITS IN SIZE,
232 * used for numerical computations.
234 * Since sizeof(floatN) may be > sizeof(char *), always pass
235 * floatN by reference.
237 typedef float float32data;
238 typedef double float64data;
239 typedef float *float32;
240 typedef double *float64;
244 * Boolean value, AT LEAST N BITS IN SIZE.
246 typedef uint8 bool8; /* >= 8 bits */
247 typedef uint16 bool16; /* >= 16 bits */
248 typedef uint32 bool32; /* >= 32 bits */
252 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
254 typedef uint8 bits8; /* >= 8 bits */
255 typedef uint16 bits16; /* >= 16 bits */
256 typedef uint32 bits32; /* >= 32 bits */
260 * Unit of storage, AT LEAST N BITS IN SIZE,
261 * used to fetch/store data.
263 typedef uint8 word8; /* >= 8 bits */
264 typedef uint16 word16; /* >= 16 bits */
265 typedef uint32 word32; /* >= 32 bits */
269 * Size of any memory resident object, as returned by sizeof.
271 typedef unsigned int Size;
275 * Index into any memory resident array.
278 * Indices are non negative.
280 typedef unsigned int Index;
289 * Offset into any memory resident array.
292 * This differs from an Index in that an Index is always
293 * non negative, whereas Offset may be negative.
295 typedef signed int Offset;
297 /* ----------------------------------------------------------------
298 * Section 4: datum type + support macros
299 * ----------------------------------------------------------------
303 * POSTGRES abstract data type datum representation definitions.
308 * Postgres makes the following assumption about machines:
310 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
312 * Postgres also assumes that
320 * If your machine meets these requirements, Datums should also be checked
321 * to see if the positioning is correct.
323 * This file is MACHINE AND COMPILER dependent!!!
326 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
327 typedef Datum * DatumPtr;
329 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
330 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
331 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
332 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
333 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
334 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
338 * Returns character value of a datum.
341 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
345 * Returns datum representation for a character.
348 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
352 * Returns datum representation for an 8-bit integer.
355 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
359 * Returns 8-bit unsigned integer value of a datum.
362 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
366 * Returns datum representation for an 8-bit unsigned integer.
369 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
373 * Returns 16-bit integer value of a datum.
376 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
380 * Returns datum representation for a 16-bit integer.
383 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
387 * Returns 16-bit unsigned integer value of a datum.
390 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
394 * Returns datum representation for a 16-bit unsigned integer.
397 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
401 * Returns 32-bit integer value of a datum.
404 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
408 * Returns datum representation for a 32-bit integer.
411 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
415 * Returns 32-bit unsigned integer value of a datum.
418 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
422 * Returns datum representation for a 32-bit unsigned integer.
425 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
428 * DatumGetObjectId --
429 * Returns object identifier value of a datum.
432 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
435 * ObjectIdGetDatum --
436 * Returns datum representation for an object identifier.
439 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
443 * Returns pointer value of a datum.
446 #define DatumGetPointer(X) ((Pointer) X)
450 * Returns datum representation for a pointer.
453 #define PointerGetDatum(X) ((Datum) X)
457 * Returns name value of a datum.
460 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
464 * Returns datum representation for a name.
467 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
472 * Returns 32-bit floating point value of a datum.
473 * This is really a pointer, of course.
476 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
480 * Returns datum representation for a 32-bit floating point number.
481 * This is really a pointer, of course.
484 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
488 * Returns 64-bit floating point value of a datum.
489 * This is really a pointer, of course.
492 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
496 * Returns datum representation for a 64-bit floating point number.
497 * This is really a pointer, of course.
500 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
502 /* ----------------------------------------------------------------
503 * Section 5: IsValid macros for system types
504 * ----------------------------------------------------------------
508 * True iff bool is valid.
510 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
514 * True iff pointer is valid.
516 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
519 * PointerIsInBounds --
520 * True iff pointer is within given bounds.
523 * Assumes the bounded interval to be [min,max),
524 * i.e. closed on the left and open on the right.
526 #define PointerIsInBounds(pointer, min, max) \
527 ((min) <= (pointer) && (pointer) < (max))
530 * PointerIsAligned --
531 * True iff pointer is properly aligned to point to the given type.
533 #define PointerIsAligned(pointer, type) \
534 (((long)(pointer) % (sizeof (type))) == 0)
536 /* ----------------------------------------------------------------
537 * Section 6: offsetof, lengthof, endof
538 * ----------------------------------------------------------------
542 * Offset of a structure/union field within that structure/union.
544 * XXX This is supposed to be part of stddef.h, but isn't on
545 * some systems (like SunOS 4).
548 #define offsetof(type, field) ((long) &((type *)0)->field)
549 #endif /* offsetof */
553 * Number of elements in an array.
555 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
559 * Address of the element one past the last in an array.
561 #define endof(array) (&array[lengthof(array)])
563 /* ----------------------------------------------------------------
564 * Section 7: exception handling definitions
565 * Assert, Trap, etc macros
566 * ----------------------------------------------------------------
569 * Exception Handling definitions
572 typedef char *ExcMessage;
573 typedef struct Exception {
578 * NO_ASSERT_CHECKING, if defined, turns off all the assertions.
581 * It should _NOT_ be undef'ed in releases or in benchmark copies
583 * #undef NO_ASSERT_CHECKING
588 * Generates an exception if the given condition is true.
591 #define Trap(condition, exception) \
593 ExceptionalCondition(CppAsString(condition), &(exception), \
594 (char*)NULL, __FILE__, __LINE__); }
597 * TrapMacro is the same as Trap but it's intended for use in macros:
599 * #define foo(x) (AssertM(x != 0) && bar(x))
603 #define TrapMacro(condition, exception) \
604 ((bool) ((! condition) || \
605 (ExceptionalCondition(CppAsString(condition), \
607 (char*) NULL, __FILE__, __LINE__))))
609 #ifdef NO_ASSERT_CHECKING
610 #define Assert(condition)
611 #define AssertMacro(condition) true
612 #define AssertArg(condition)
613 #define AssertState(condition)
615 #define Assert(condition) \
616 Trap(!(condition), FailedAssertion)
618 #define AssertMacro(condition) \
619 TrapMacro(!(condition), FailedAssertion)
621 #define AssertArg(condition) \
622 Trap(!(condition), BadArg)
624 #define AssertState(condition) \
625 Trap(!(condition), BadState)
627 #endif /* NO_ASSERT_CHECKING */
631 * Generates an exception with a message if the given condition is true.
634 #define LogTrap(condition, exception, printArgs) \
636 ExceptionalCondition(CppAsString(condition), &(exception), \
637 form printArgs, __FILE__, __LINE__); }
640 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
642 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
644 #define LogTrapMacro(condition, exception, printArgs) \
645 ((bool) ((! condition) || \
646 (ExceptionalCondition(CppAsString(condition), \
648 form printArgs, __FILE__, __LINE__))))
650 #ifdef NO_ASSERT_CHECKING
651 #define LogAssert(condition, printArgs)
652 #define LogAssertMacro(condition, printArgs) true
653 #define LogAssertArg(condition, printArgs)
654 #define LogAssertState(condition, printArgs)
656 #define LogAssert(condition, printArgs) \
657 LogTrap(!(condition), FailedAssertion, printArgs)
659 #define LogAssertMacro(condition, printArgs) \
660 LogTrapMacro(!(condition), FailedAssertion, printArgs)
662 #define LogAssertArg(condition, printArgs) \
663 LogTrap(!(condition), BadArg, printArgs)
665 #define LogAssertState(condition, printArgs) \
666 LogTrap(!(condition), BadState, printArgs)
668 #endif /* NO_ASSERT_CHECKING */
670 /* ----------------------------------------------------------------
671 * Section 8: Min, Max, Abs macros
672 * ----------------------------------------------------------------
676 * Return the maximum of two numbers.
678 #define Max(x, y) ((x) > (y) ? (x) : (y))
682 * Return the minimum of two numbers.
684 #define Min(x, y) ((x) < (y) ? (x) : (y))
688 * Return the absolute value of the argument.
690 #define Abs(x) ((x) >= 0 ? (x) : -(x))
692 /* ----------------------------------------------------------------
694 * ----------------------------------------------------------------
697 extern Exception FailedAssertion;
698 extern Exception BadArg;
699 extern Exception BadState;
701 /* in utils/error/assert.c */
702 extern int ExceptionalCondition(char *conditionName,
703 Exception *exceptionP, char *details,
704 char *fileName, int lineNumber);
708 * form is used by assert and the exception handling stuff
711 extern char *form(char *fmt, ...);
715 /* ----------------------------------------------------------------
716 * Section 10: berkeley-specific configuration
718 * this section contains settings which are only relevant to the UC Berkeley
719 * sites. Other sites can ignore this
720 * ----------------------------------------------------------------
726 * These are experimental and are not supported in the code that
727 * we distribute to other sites.
736 /* ----------------------------------------------------------------
737 * Section 11: system-specific hacks
739 * This should be limited to things that absolutely have to be
740 * included in every source file. The changes should be factored
741 * into a separate file so that changes to one port don't require
742 * changes to c.h (and everyone recompiling their whole system).
743 * ----------------------------------------------------------------
747 #include "port/hpux/fixade.h" /* for 8.07 unaligned access fixup */
750 #if defined(sparc) && !defined(sparc_solaris)
751 #define memmove(d, s, l) bcopy(s, d, l)
754 /* These are for things that are one way on Unix and another on NT */
756 #define NULL_DEV "/dev/null"
757 #define COPY_CMD "cp"
760 #define NULL_DEV "NUL"
761 #define COPY_CMD "copy"
762 #define SEP_CHAR '\\'
766 #include "port/win32/nt.h"
767 #include "port/win32/machine.h"