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.9 1997/02/14 04:18:27 momjian 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
163 #else /* !HAVE_ANSI_CPP */
165 #define CppAsString(identifier) "identifier"
168 * CppIdentity -- On Reiser based cpp's this is used to concatenate
169 * two tokens. That is
170 * CppIdentity(A)B ==> AB
171 * We renamed it to _private_CppIdentity because it should not
172 * be referenced outside this file. On other cpp's it
175 #define _priv_CppIdentity(x)x
176 #define CppConcat(x, y) _priv_CppIdentity(x)y
178 #endif /* !HAVE_ANSI_CPP */
180 #ifndef __GNUC__ /* GNU cc */
184 #if defined(NEED_STD_HDRS)
186 * You're doomed. We've removed almost all of our own C library
187 * extern declarations because they conflict on the different
188 * systems. You'll have to write your own stdlib.h.
191 #else /* NEED_STD_HDRS */
194 #endif /* NEED_STD_HDRS */
196 /* ----------------------------------------------------------------
197 * Section 3: standard system types
198 * ----------------------------------------------------------------
203 * Signed integer, AT LEAST N BITS IN SIZE,
204 * used for numerical computations.
206 typedef signed char int8; /* >= 8 bits */
207 typedef signed short int16; /* >= 16 bits */
208 typedef signed int int32; /* >= 32 bits */
212 * Unsigned integer, AT LEAST N BITS IN SIZE,
213 * used for numerical computations.
215 typedef unsigned char uint8; /* >= 8 bits */
216 typedef unsigned short uint16; /* >= 16 bits */
217 typedef unsigned int uint32; /* >= 32 bits */
221 * Floating point number, AT LEAST N BITS IN SIZE,
222 * used for numerical computations.
224 * Since sizeof(floatN) may be > sizeof(char *), always pass
225 * floatN by reference.
227 typedef float float32data;
228 typedef double float64data;
229 typedef float *float32;
230 typedef double *float64;
234 * Boolean value, AT LEAST N BITS IN SIZE.
236 typedef uint8 bool8; /* >= 8 bits */
237 typedef uint16 bool16; /* >= 16 bits */
238 typedef uint32 bool32; /* >= 32 bits */
242 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
244 typedef uint8 bits8; /* >= 8 bits */
245 typedef uint16 bits16; /* >= 16 bits */
246 typedef uint32 bits32; /* >= 32 bits */
250 * Unit of storage, AT LEAST N BITS IN SIZE,
251 * used to fetch/store data.
253 typedef uint8 word8; /* >= 8 bits */
254 typedef uint16 word16; /* >= 16 bits */
255 typedef uint32 word32; /* >= 32 bits */
259 * Size of any memory resident object, as returned by sizeof.
261 typedef unsigned int Size;
265 * Index into any memory resident array.
268 * Indices are non negative.
270 typedef unsigned int Index;
279 * Offset into any memory resident array.
282 * This differs from an Index in that an Index is always
283 * non negative, whereas Offset may be negative.
285 typedef signed int Offset;
287 /* ----------------------------------------------------------------
288 * Section 4: datum type + support macros
289 * ----------------------------------------------------------------
293 * POSTGRES abstract data type datum representation definitions.
298 * Postgres makes the following assumption about machines:
300 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
302 * Postgres also assumes that
310 * If your machine meets these requirements, Datums should also be checked
311 * to see if the positioning is correct.
313 * This file is MACHINE AND COMPILER dependent!!!
316 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
317 typedef Datum * DatumPtr;
319 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
320 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
321 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
322 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
323 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
324 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
328 * Returns character value of a datum.
331 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
335 * Returns datum representation for a character.
338 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
342 * Returns datum representation for an 8-bit integer.
345 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
349 * Returns 8-bit unsigned integer value of a datum.
352 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
356 * Returns datum representation for an 8-bit unsigned integer.
359 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
363 * Returns 16-bit integer value of a datum.
366 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
370 * Returns datum representation for a 16-bit integer.
373 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
377 * Returns 16-bit unsigned integer value of a datum.
380 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
384 * Returns datum representation for a 16-bit unsigned integer.
387 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
391 * Returns 32-bit integer value of a datum.
394 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
398 * Returns datum representation for a 32-bit integer.
401 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
405 * Returns 32-bit unsigned integer value of a datum.
408 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
412 * Returns datum representation for a 32-bit unsigned integer.
415 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
418 * DatumGetObjectId --
419 * Returns object identifier value of a datum.
422 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
425 * ObjectIdGetDatum --
426 * Returns datum representation for an object identifier.
429 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
433 * Returns pointer value of a datum.
436 #define DatumGetPointer(X) ((Pointer) X)
440 * Returns datum representation for a pointer.
443 #define PointerGetDatum(X) ((Datum) X)
447 * Returns name value of a datum.
450 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
454 * Returns datum representation for a name.
457 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
462 * Returns 32-bit floating point value of a datum.
463 * This is really a pointer, of course.
466 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
470 * Returns datum representation for a 32-bit floating point number.
471 * This is really a pointer, of course.
474 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
478 * Returns 64-bit floating point value of a datum.
479 * This is really a pointer, of course.
482 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
486 * Returns datum representation for a 64-bit floating point number.
487 * This is really a pointer, of course.
490 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
492 /* ----------------------------------------------------------------
493 * Section 5: IsValid macros for system types
494 * ----------------------------------------------------------------
498 * True iff bool is valid.
500 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
504 * True iff pointer is valid.
506 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
509 * PointerIsInBounds --
510 * True iff pointer is within given bounds.
513 * Assumes the bounded interval to be [min,max),
514 * i.e. closed on the left and open on the right.
516 #define PointerIsInBounds(pointer, min, max) \
517 ((min) <= (pointer) && (pointer) < (max))
520 * PointerIsAligned --
521 * True iff pointer is properly aligned to point to the given type.
523 #define PointerIsAligned(pointer, type) \
524 (((long)(pointer) % (sizeof (type))) == 0)
526 /* ----------------------------------------------------------------
527 * Section 6: offsetof, lengthof, endof
528 * ----------------------------------------------------------------
532 * Offset of a structure/union field within that structure/union.
534 * XXX This is supposed to be part of stddef.h, but isn't on
535 * some systems (like SunOS 4).
538 #define offsetof(type, field) ((long) &((type *)0)->field)
539 #endif /* offsetof */
543 * Number of elements in an array.
545 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
549 * Address of the element one past the last in an array.
551 #define endof(array) (&array[lengthof(array)])
553 /* ----------------------------------------------------------------
554 * Section 7: exception handling definitions
555 * Assert, Trap, etc macros
556 * ----------------------------------------------------------------
559 * Exception Handling definitions
562 typedef char *ExcMessage;
563 typedef struct Exception {
568 * NO_ASSERT_CHECKING, if defined, turns off all the assertions.
571 * It should _NOT_ be undef'ed in releases or in benchmark copies
573 * #undef NO_ASSERT_CHECKING
578 * Generates an exception if the given condition is true.
581 #define Trap(condition, exception) \
583 ExceptionalCondition(CppAsString(condition), &(exception), \
584 (char*)NULL, __FILE__, __LINE__); }
587 * TrapMacro is the same as Trap but it's intended for use in macros:
589 * #define foo(x) (AssertM(x != 0) && bar(x))
593 #define TrapMacro(condition, exception) \
594 ((bool) ((! condition) || \
595 (ExceptionalCondition(CppAsString(condition), \
597 (char*) NULL, __FILE__, __LINE__))))
599 #ifdef NO_ASSERT_CHECKING
600 #define Assert(condition)
601 #define AssertMacro(condition) true
602 #define AssertArg(condition)
603 #define AssertState(condition)
605 #define Assert(condition) \
606 Trap(!(condition), FailedAssertion)
608 #define AssertMacro(condition) \
609 TrapMacro(!(condition), FailedAssertion)
611 #define AssertArg(condition) \
612 Trap(!(condition), BadArg)
614 #define AssertState(condition) \
615 Trap(!(condition), BadState)
617 #endif /* NO_ASSERT_CHECKING */
621 * Generates an exception with a message if the given condition is true.
624 #define LogTrap(condition, exception, printArgs) \
626 ExceptionalCondition(CppAsString(condition), &(exception), \
627 form printArgs, __FILE__, __LINE__); }
630 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
632 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
634 #define LogTrapMacro(condition, exception, printArgs) \
635 ((bool) ((! condition) || \
636 (ExceptionalCondition(CppAsString(condition), \
638 form printArgs, __FILE__, __LINE__))))
640 #ifdef NO_ASSERT_CHECKING
641 #define LogAssert(condition, printArgs)
642 #define LogAssertMacro(condition, printArgs) true
643 #define LogAssertArg(condition, printArgs)
644 #define LogAssertState(condition, printArgs)
646 #define LogAssert(condition, printArgs) \
647 LogTrap(!(condition), FailedAssertion, printArgs)
649 #define LogAssertMacro(condition, printArgs) \
650 LogTrapMacro(!(condition), FailedAssertion, printArgs)
652 #define LogAssertArg(condition, printArgs) \
653 LogTrap(!(condition), BadArg, printArgs)
655 #define LogAssertState(condition, printArgs) \
656 LogTrap(!(condition), BadState, printArgs)
658 #endif /* NO_ASSERT_CHECKING */
660 /* ----------------------------------------------------------------
661 * Section 8: Min, Max, Abs macros
662 * ----------------------------------------------------------------
666 * Return the maximum of two numbers.
668 #define Max(x, y) ((x) > (y) ? (x) : (y))
672 * Return the minimum of two numbers.
674 #define Min(x, y) ((x) < (y) ? (x) : (y))
678 * Return the absolute value of the argument.
680 #define Abs(x) ((x) >= 0 ? (x) : -(x))
682 /* ----------------------------------------------------------------
684 * ----------------------------------------------------------------
687 extern Exception FailedAssertion;
688 extern Exception BadArg;
689 extern Exception BadState;
691 /* in utils/error/assert.c */
692 extern int ExceptionalCondition(char *conditionName,
693 Exception *exceptionP, char *details,
694 char *fileName, int lineNumber);
698 * form is used by assert and the exception handling stuff
701 extern char *form(char *fmt, ...);
705 /* ----------------------------------------------------------------
706 * Section 10: berkeley-specific configuration
708 * this section contains settings which are only relevant to the UC Berkeley
709 * sites. Other sites can ignore this
710 * ----------------------------------------------------------------
716 * These are experimental and are not supported in the code that
717 * we distribute to other sites.
726 /* ----------------------------------------------------------------
727 * Section 11: system-specific hacks
729 * This should be limited to things that absolutely have to be
730 * included in every source file. The changes should be factored
731 * into a separate file so that changes to one port don't require
732 * changes to c.h (and everyone recompiling their whole system).
733 * ----------------------------------------------------------------
737 #include "port/hpux/fixade.h" /* for unaligned access fixup */
741 #define memmove(d, s, l) bcopy(s, d, l)
744 /* These are for things that are one way on Unix and another on NT */
745 #define NULL_DEV "/dev/null"
746 #define COPY_CMD "cp"