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.15 1997/08/24 23:07:56 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, strNcpy 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)
66 typedef bool *BoolPtr;
76 /* ----------------------------------------------------------------
77 * Section 2: __STDC__, non-ansi C definitions:
80 * Pointer typedef, NULL
81 * type prefixes: const, signed, volatile, inline
82 * ----------------------------------------------------------------
85 #ifdef __STDC__ /* ANSI C */
89 * Variable holding address of any memory resident object.
93 * XXX Pointer arithmetic is done with this, so it can't be void *
94 * under "true" ANSI compilers.
96 typedef char *Pointer;
103 #define NULL ((void *) 0)
104 #endif /* !defined(NULL) */
106 #define HAVE_ANSI_CPP /* all ANSI C compilers must have this! */
107 #if defined(NEED_STD_HDRS)
108 #undef NEED_STD_HDRS /* all ANSI systems must have stddef/stdlib */
109 #endif /* NEED_STD_HDRS */
111 #else /* !defined(__STDC__) */ /* NOT ANSI C */
115 * Variable containing address of any memory resident object.
117 typedef char *Pointer;
125 #endif /* !defined(NULL) */
129 * Type modifier. Identifies read only variables.
132 * extern const Version RomVersion;
134 #define const /* const */
138 * Type modifier. Identifies signed integral types.
140 #define signed /* signed */
144 * Type modifier. Identifies variables which may change in ways not
145 * noticeable by the compiler, e.g. via asynchronous interrupts.
148 * extern volatile unsigned int NumberOfInterrupts;
150 #define volatile /* volatile */
152 #endif /* !defined(__STDC__) */ /* NOT ANSI C */
156 * Convert the argument to a string, using the C preprocessor.
158 * Concatenate two arguments together, using the C preprocessor.
160 #if defined(HAVE_ANSI_CPP)
162 #define CppAsString(identifier) #identifier
163 #define CppConcat(x, y) x##y
165 #else /* !HAVE_ANSI_CPP */
167 #define CppAsString(identifier) "identifier"
170 * CppIdentity -- On Reiser based cpp's this is used to concatenate
171 * two tokens. That is
172 * CppIdentity(A)B ==> AB
173 * We renamed it to _private_CppIdentity because it should not
174 * be referenced outside this file. On other cpp's it
177 #define _priv_CppIdentity(x)x
178 #define CppConcat(x, y) _priv_CppIdentity(x)y
180 #endif /* !HAVE_ANSI_CPP */
182 #ifndef __GNUC__ /* GNU cc */
186 #if defined(NEED_STD_HDRS)
188 * You're doomed. We've removed almost all of our own C library
189 * extern declarations because they conflict on the different
190 * systems. You'll have to write your own stdlib.h.
193 #else /* NEED_STD_HDRS */
196 #endif /* NEED_STD_HDRS */
198 /* ----------------------------------------------------------------
199 * Section 3: standard system types
200 * ----------------------------------------------------------------
205 * Signed integer, AT LEAST N BITS IN SIZE,
206 * used for numerical computations.
208 typedef signed char int8; /* >= 8 bits */
209 typedef signed short int16; /* >= 16 bits */
210 typedef signed int int32; /* >= 32 bits */
214 * Unsigned integer, AT LEAST N BITS IN SIZE,
215 * used for numerical computations.
217 typedef unsigned char uint8; /* >= 8 bits */
218 typedef unsigned short uint16; /* >= 16 bits */
219 typedef unsigned int uint32; /* >= 32 bits */
223 * Floating point number, AT LEAST N BITS IN SIZE,
224 * used for numerical computations.
226 * Since sizeof(floatN) may be > sizeof(char *), always pass
227 * floatN by reference.
229 typedef float float32data;
230 typedef double float64data;
231 typedef float *float32;
232 typedef double *float64;
236 * Boolean value, AT LEAST N BITS IN SIZE.
238 typedef uint8 bool8; /* >= 8 bits */
239 typedef uint16 bool16; /* >= 16 bits */
240 typedef uint32 bool32; /* >= 32 bits */
244 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
246 typedef uint8 bits8; /* >= 8 bits */
247 typedef uint16 bits16; /* >= 16 bits */
248 typedef uint32 bits32; /* >= 32 bits */
252 * Unit of storage, AT LEAST N BITS IN SIZE,
253 * used to fetch/store data.
255 typedef uint8 word8; /* >= 8 bits */
256 typedef uint16 word16; /* >= 16 bits */
257 typedef uint32 word32; /* >= 32 bits */
261 * Size of any memory resident object, as returned by sizeof.
263 typedef unsigned int Size;
267 * Index into any memory resident array.
270 * Indices are non negative.
272 typedef unsigned int Index;
281 * Offset into any memory resident array.
284 * This differs from an Index in that an Index is always
285 * non negative, whereas Offset may be negative.
287 typedef signed int Offset;
289 /* ----------------------------------------------------------------
290 * Section 4: datum type + support macros
291 * ----------------------------------------------------------------
295 * POSTGRES abstract data type datum representation definitions.
300 * Postgres makes the following assumption about machines:
302 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
304 * Postgres also assumes that
312 * If your machine meets these requirements, Datums should also be checked
313 * to see if the positioning is correct.
315 * This file is MACHINE AND COMPILER dependent!!!
318 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
319 typedef Datum * DatumPtr;
321 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
322 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
323 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
324 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
325 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
326 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
330 * Returns character value of a datum.
333 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
337 * Returns datum representation for a character.
340 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
344 * Returns datum representation for an 8-bit integer.
347 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
351 * Returns 8-bit unsigned integer value of a datum.
354 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
358 * Returns datum representation for an 8-bit unsigned integer.
361 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
365 * Returns 16-bit integer value of a datum.
368 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
372 * Returns datum representation for a 16-bit integer.
375 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
379 * Returns 16-bit unsigned integer value of a datum.
382 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
386 * Returns datum representation for a 16-bit unsigned integer.
389 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
393 * Returns 32-bit integer value of a datum.
396 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
400 * Returns datum representation for a 32-bit integer.
403 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
407 * Returns 32-bit unsigned integer value of a datum.
410 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
414 * Returns datum representation for a 32-bit unsigned integer.
417 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
420 * DatumGetObjectId --
421 * Returns object identifier value of a datum.
424 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
427 * ObjectIdGetDatum --
428 * Returns datum representation for an object identifier.
431 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
435 * Returns pointer value of a datum.
438 #define DatumGetPointer(X) ((Pointer) X)
442 * Returns datum representation for a pointer.
445 #define PointerGetDatum(X) ((Datum) X)
449 * Returns name value of a datum.
452 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
456 * Returns datum representation for a name.
459 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
464 * Returns 32-bit floating point value of a datum.
465 * This is really a pointer, of course.
468 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
472 * Returns datum representation for a 32-bit floating point number.
473 * This is really a pointer, of course.
476 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
480 * Returns 64-bit floating point value of a datum.
481 * This is really a pointer, of course.
484 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
488 * Returns datum representation for a 64-bit floating point number.
489 * This is really a pointer, of course.
492 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
494 /* ----------------------------------------------------------------
495 * Section 5: IsValid macros for system types
496 * ----------------------------------------------------------------
500 * True iff bool is valid.
502 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
506 * True iff pointer is valid.
508 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
511 * PointerIsInBounds --
512 * True iff pointer is within given bounds.
515 * Assumes the bounded interval to be [min,max),
516 * i.e. closed on the left and open on the right.
518 #define PointerIsInBounds(pointer, min, max) \
519 ((min) <= (pointer) && (pointer) < (max))
522 * PointerIsAligned --
523 * True iff pointer is properly aligned to point to the given type.
525 #define PointerIsAligned(pointer, type) \
526 (((long)(pointer) % (sizeof (type))) == 0)
528 /* ----------------------------------------------------------------
529 * Section 6: offsetof, lengthof, endof
530 * ----------------------------------------------------------------
534 * Offset of a structure/union field within that structure/union.
536 * XXX This is supposed to be part of stddef.h, but isn't on
537 * some systems (like SunOS 4).
540 #define offsetof(type, field) ((long) &((type *)0)->field)
541 #endif /* offsetof */
545 * Number of elements in an array.
547 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
551 * Address of the element one past the last in an array.
553 #define endof(array) (&array[lengthof(array)])
555 /* ----------------------------------------------------------------
556 * Section 7: exception handling definitions
557 * Assert, Trap, etc macros
558 * ----------------------------------------------------------------
561 * Exception Handling definitions
564 typedef char *ExcMessage;
565 typedef struct Exception {
570 * NO_ASSERT_CHECKING, if defined, turns off all the assertions.
573 * It should _NOT_ be undef'ed in releases or in benchmark copies
575 * #undef NO_ASSERT_CHECKING
580 * Generates an exception if the given condition is true.
583 #define Trap(condition, exception) \
585 ExceptionalCondition(CppAsString(condition), &(exception), \
586 (char*)NULL, __FILE__, __LINE__); }
589 * TrapMacro is the same as Trap but it's intended for use in macros:
591 * #define foo(x) (AssertM(x != 0) && bar(x))
595 #define TrapMacro(condition, exception) \
596 ((bool) ((! condition) || \
597 (ExceptionalCondition(CppAsString(condition), \
599 (char*) NULL, __FILE__, __LINE__))))
601 #ifdef NO_ASSERT_CHECKING
602 #define Assert(condition)
603 #define AssertMacro(condition) true
604 #define AssertArg(condition)
605 #define AssertState(condition)
607 #define Assert(condition) \
608 Trap(!(condition), FailedAssertion)
610 #define AssertMacro(condition) \
611 TrapMacro(!(condition), FailedAssertion)
613 #define AssertArg(condition) \
614 Trap(!(condition), BadArg)
616 #define AssertState(condition) \
617 Trap(!(condition), BadState)
619 #endif /* NO_ASSERT_CHECKING */
623 * Generates an exception with a message if the given condition is true.
626 #define LogTrap(condition, exception, printArgs) \
628 ExceptionalCondition(CppAsString(condition), &(exception), \
629 form printArgs, __FILE__, __LINE__); }
632 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
634 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
636 #define LogTrapMacro(condition, exception, printArgs) \
637 ((bool) ((! condition) || \
638 (ExceptionalCondition(CppAsString(condition), \
640 form printArgs, __FILE__, __LINE__))))
642 #ifdef NO_ASSERT_CHECKING
643 #define LogAssert(condition, printArgs)
644 #define LogAssertMacro(condition, printArgs) true
645 #define LogAssertArg(condition, printArgs)
646 #define LogAssertState(condition, printArgs)
648 #define LogAssert(condition, printArgs) \
649 LogTrap(!(condition), FailedAssertion, printArgs)
651 #define LogAssertMacro(condition, printArgs) \
652 LogTrapMacro(!(condition), FailedAssertion, printArgs)
654 #define LogAssertArg(condition, printArgs) \
655 LogTrap(!(condition), BadArg, printArgs)
657 #define LogAssertState(condition, printArgs) \
658 LogTrap(!(condition), BadState, printArgs)
660 #endif /* NO_ASSERT_CHECKING */
662 /* ----------------------------------------------------------------
663 * Section 8: Min, Max, Abs macros
664 * ----------------------------------------------------------------
668 * Return the maximum of two numbers.
670 #define Max(x, y) ((x) > (y) ? (x) : (y))
674 * Return the minimum of two numbers.
676 #define Min(x, y) ((x) < (y) ? (x) : (y))
680 * Return the absolute value of the argument.
682 #define Abs(x) ((x) >= 0 ? (x) : -(x))
686 * Does string copy, and forces terminating NULL
688 /* we do this so if the macro is used in an if action, it will work */
689 #define strNcpy(dst,src,len) (strncpy((dst),(src),(len)),*((dst)+(len))='\0',dst)
691 /* ----------------------------------------------------------------
693 * ----------------------------------------------------------------
696 extern Exception FailedAssertion;
697 extern Exception BadArg;
698 extern Exception BadState;
700 /* in utils/error/assert.c */
701 extern int ExceptionalCondition(char *conditionName,
702 Exception *exceptionP, char *details,
703 char *fileName, int lineNumber);
707 * form is used by assert and the exception handling stuff
710 extern char *form(const char *fmt, ...);
714 /* ----------------------------------------------------------------
715 * Section 10: berkeley-specific configuration
717 * this section contains settings which are only relevant to the UC Berkeley
718 * sites. Other sites can ignore this
719 * ----------------------------------------------------------------
725 * These are experimental and are not supported in the code that
726 * we distribute to other sites.
735 /* ----------------------------------------------------------------
736 * Section 11: system-specific hacks
738 * This should be limited to things that absolutely have to be
739 * included in every source file. The changes should be factored
740 * into a separate file so that changes to one port don't require
741 * changes to c.h (and everyone recompiling their whole system).
742 * ----------------------------------------------------------------
747 #include "port/hpux/fixade.h" /* for unaligned access fixup */
752 #define memmove(d, s, l) bcopy(s, d, l)
756 /* These are for things that are one way on Unix and another on NT */
757 #define NULL_DEV "/dev/null"
758 #define COPY_CMD "cp"