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.17 1997/09/07 04:55:35 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)
67 typedef bool *BoolPtr;
77 /* ----------------------------------------------------------------
78 * Section 2: __STDC__, non-ansi C definitions:
81 * Pointer typedef, NULL
82 * type prefixes: const, signed, volatile, inline
83 * ----------------------------------------------------------------
86 #ifdef __STDC__ /* ANSI C */
90 * Variable holding address of any memory resident object.
94 * XXX Pointer arithmetic is done with this, so it can't be void *
95 * under "true" ANSI compilers.
97 typedef char *Pointer;
104 #define NULL ((void *) 0)
105 #endif /* !defined(NULL) */
107 #define HAVE_ANSI_CPP /* all ANSI C compilers must have this! */
108 #if defined(NEED_STD_HDRS)
109 #undef NEED_STD_HDRS /* all ANSI systems must have
111 #endif /* NEED_STD_HDRS */
113 #else /* !defined(__STDC__) *//* NOT ANSI C */
117 * Variable containing address of any memory resident object.
119 typedef char *Pointer;
127 #endif /* !defined(NULL) */
131 * Type modifier. Identifies read only variables.
134 * extern const Version RomVersion;
136 #define const /* const */
140 * Type modifier. Identifies signed integral types.
142 #define signed /* signed */
146 * Type modifier. Identifies variables which may change in ways not
147 * noticeable by the compiler, e.g. via asynchronous interrupts.
150 * extern volatile unsigned int NumberOfInterrupts;
152 #define volatile /* volatile */
154 #endif /* !defined(__STDC__) *//* NOT ANSI C */
158 * Convert the argument to a string, using the C preprocessor.
160 * Concatenate two arguments together, using the C preprocessor.
162 #if defined(HAVE_ANSI_CPP)
164 #define CppAsString(identifier) #identifier
165 #define CppConcat(x, y) x##y
167 #else /* !HAVE_ANSI_CPP */
169 #define CppAsString(identifier) "identifier"
172 * CppIdentity -- On Reiser based cpp's this is used to concatenate
173 * two tokens. That is
174 * CppIdentity(A)B ==> AB
175 * We renamed it to _private_CppIdentity because it should not
176 * be referenced outside this file. On other cpp's it
179 #define _priv_CppIdentity(x)x
180 #define CppConcat(x, y) _priv_CppIdentity(x)y
182 #endif /* !HAVE_ANSI_CPP */
184 #ifndef __GNUC__ /* GNU cc */
188 #if defined(NEED_STD_HDRS)
190 * You're doomed. We've removed almost all of our own C library
191 * extern declarations because they conflict on the different
192 * systems. You'll have to write your own stdlib.h.
195 #else /* NEED_STD_HDRS */
198 #endif /* NEED_STD_HDRS */
200 /* ----------------------------------------------------------------
201 * Section 3: standard system types
202 * ----------------------------------------------------------------
207 * Signed integer, AT LEAST N BITS IN SIZE,
208 * used for numerical computations.
210 typedef signed char int8; /* >= 8 bits */
211 typedef signed short int16; /* >= 16 bits */
212 typedef signed int int32; /* >= 32 bits */
216 * Unsigned integer, AT LEAST N BITS IN SIZE,
217 * used for numerical computations.
219 typedef unsigned char uint8; /* >= 8 bits */
220 typedef unsigned short uint16; /* >= 16 bits */
221 typedef unsigned int uint32; /* >= 32 bits */
225 * Floating point number, AT LEAST N BITS IN SIZE,
226 * used for numerical computations.
228 * Since sizeof(floatN) may be > sizeof(char *), always pass
229 * floatN by reference.
231 typedef float float32data;
232 typedef double float64data;
233 typedef float *float32;
234 typedef double *float64;
238 * Boolean value, AT LEAST N BITS IN SIZE.
240 typedef uint8 bool8; /* >= 8 bits */
241 typedef uint16 bool16; /* >= 16 bits */
242 typedef uint32 bool32; /* >= 32 bits */
246 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
248 typedef uint8 bits8; /* >= 8 bits */
249 typedef uint16 bits16; /* >= 16 bits */
250 typedef uint32 bits32; /* >= 32 bits */
254 * Unit of storage, AT LEAST N BITS IN SIZE,
255 * used to fetch/store data.
257 typedef uint8 word8; /* >= 8 bits */
258 typedef uint16 word16; /* >= 16 bits */
259 typedef uint32 word32; /* >= 32 bits */
263 * Size of any memory resident object, as returned by sizeof.
265 typedef unsigned int Size;
269 * Index into any memory resident array.
272 * Indices are non negative.
274 typedef unsigned int Index;
284 * Offset into any memory resident array.
287 * This differs from an Index in that an Index is always
288 * non negative, whereas Offset may be negative.
290 typedef signed int Offset;
292 /* ----------------------------------------------------------------
293 * Section 4: datum type + support macros
294 * ----------------------------------------------------------------
298 * POSTGRES abstract data type datum representation definitions.
303 * Postgres makes the following assumption about machines:
305 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
307 * Postgres also assumes that
315 * If your machine meets these requirements, Datums should also be checked
316 * to see if the positioning is correct.
318 * This file is MACHINE AND COMPILER dependent!!!
321 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
322 typedef Datum *DatumPtr;
324 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
325 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
326 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
327 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
328 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
329 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
333 * Returns character value of a datum.
336 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
340 * Returns datum representation for a character.
343 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
347 * Returns datum representation for an 8-bit integer.
350 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
354 * Returns 8-bit unsigned integer value of a datum.
357 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
361 * Returns datum representation for an 8-bit unsigned integer.
364 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
368 * Returns 16-bit integer value of a datum.
371 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
375 * Returns datum representation for a 16-bit integer.
378 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
382 * Returns 16-bit unsigned integer value of a datum.
385 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
389 * Returns datum representation for a 16-bit unsigned integer.
392 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
396 * Returns 32-bit integer value of a datum.
399 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
403 * Returns datum representation for a 32-bit integer.
406 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
410 * Returns 32-bit unsigned integer value of a datum.
413 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
417 * Returns datum representation for a 32-bit unsigned integer.
420 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
423 * DatumGetObjectId --
424 * Returns object identifier value of a datum.
427 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
430 * ObjectIdGetDatum --
431 * Returns datum representation for an object identifier.
434 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
438 * Returns pointer value of a datum.
441 #define DatumGetPointer(X) ((Pointer) X)
445 * Returns datum representation for a pointer.
448 #define PointerGetDatum(X) ((Datum) X)
452 * Returns name value of a datum.
455 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
459 * Returns datum representation for a name.
462 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
467 * Returns 32-bit floating point value of a datum.
468 * This is really a pointer, of course.
471 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
475 * Returns datum representation for a 32-bit floating point number.
476 * This is really a pointer, of course.
479 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
483 * Returns 64-bit floating point value of a datum.
484 * This is really a pointer, of course.
487 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
491 * Returns datum representation for a 64-bit floating point number.
492 * This is really a pointer, of course.
495 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
497 /* ----------------------------------------------------------------
498 * Section 5: IsValid macros for system types
499 * ----------------------------------------------------------------
503 * True iff bool is valid.
505 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
509 * True iff pointer is valid.
511 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
514 * PointerIsInBounds --
515 * True iff pointer is within given bounds.
518 * Assumes the bounded interval to be [min,max),
519 * i.e. closed on the left and open on the right.
521 #define PointerIsInBounds(pointer, min, max) \
522 ((min) <= (pointer) && (pointer) < (max))
525 * PointerIsAligned --
526 * True iff pointer is properly aligned to point to the given type.
528 #define PointerIsAligned(pointer, type) \
529 (((long)(pointer) % (sizeof (type))) == 0)
531 /* ----------------------------------------------------------------
532 * Section 6: offsetof, lengthof, endof
533 * ----------------------------------------------------------------
537 * Offset of a structure/union field within that structure/union.
539 * XXX This is supposed to be part of stddef.h, but isn't on
540 * some systems (like SunOS 4).
543 #define offsetof(type, field) ((long) &((type *)0)->field)
544 #endif /* offsetof */
548 * Number of elements in an array.
550 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
554 * Address of the element one past the last in an array.
556 #define endof(array) (&array[lengthof(array)])
558 /* ----------------------------------------------------------------
559 * Section 7: exception handling definitions
560 * Assert, Trap, etc macros
561 * ----------------------------------------------------------------
564 * Exception Handling definitions
567 typedef char *ExcMessage;
568 typedef struct Exception
574 * NO_ASSERT_CHECKING, if defined, turns off all the assertions.
577 * It should _NOT_ be undef'ed in releases or in benchmark copies
579 * #undef NO_ASSERT_CHECKING
584 * Generates an exception if the given condition is true.
587 #define Trap(condition, exception) \
589 ExceptionalCondition(CppAsString(condition), &(exception), \
590 (char*)NULL, __FILE__, __LINE__); }
593 * TrapMacro is the same as Trap but it's intended for use in macros:
595 * #define foo(x) (AssertM(x != 0) && bar(x))
599 #define TrapMacro(condition, exception) \
600 ((bool) ((! condition) || \
601 (ExceptionalCondition(CppAsString(condition), \
603 (char*) NULL, __FILE__, __LINE__))))
605 #ifdef NO_ASSERT_CHECKING
606 #define Assert(condition)
607 #define AssertMacro(condition) true
608 #define AssertArg(condition)
609 #define AssertState(condition)
611 #define Assert(condition) \
612 Trap(!(condition), FailedAssertion)
614 #define AssertMacro(condition) \
615 TrapMacro(!(condition), FailedAssertion)
617 #define AssertArg(condition) \
618 Trap(!(condition), BadArg)
620 #define AssertState(condition) \
621 Trap(!(condition), BadState)
623 #endif /* NO_ASSERT_CHECKING */
627 * Generates an exception with a message if the given condition is true.
630 #define LogTrap(condition, exception, printArgs) \
632 ExceptionalCondition(CppAsString(condition), &(exception), \
633 form printArgs, __FILE__, __LINE__); }
636 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
638 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
640 #define LogTrapMacro(condition, exception, printArgs) \
641 ((bool) ((! condition) || \
642 (ExceptionalCondition(CppAsString(condition), \
644 form printArgs, __FILE__, __LINE__))))
646 #ifdef NO_ASSERT_CHECKING
647 #define LogAssert(condition, printArgs)
648 #define LogAssertMacro(condition, printArgs) true
649 #define LogAssertArg(condition, printArgs)
650 #define LogAssertState(condition, printArgs)
652 #define LogAssert(condition, printArgs) \
653 LogTrap(!(condition), FailedAssertion, printArgs)
655 #define LogAssertMacro(condition, printArgs) \
656 LogTrapMacro(!(condition), FailedAssertion, printArgs)
658 #define LogAssertArg(condition, printArgs) \
659 LogTrap(!(condition), BadArg, printArgs)
661 #define LogAssertState(condition, printArgs) \
662 LogTrap(!(condition), BadState, printArgs)
664 #endif /* NO_ASSERT_CHECKING */
666 /* ----------------------------------------------------------------
667 * Section 8: Min, Max, Abs macros
668 * ----------------------------------------------------------------
672 * Return the maximum of two numbers.
674 #define Max(x, y) ((x) > (y) ? (x) : (y))
678 * Return the minimum of two numbers.
680 #define Min(x, y) ((x) < (y) ? (x) : (y))
684 * Return the absolute value of the argument.
686 #define Abs(x) ((x) >= 0 ? (x) : -(x))
690 * Does string copy, and forces terminating NULL
692 /* we do this so if the macro is used in an if action, it will work */
693 #define strNcpy(dst,src,len) (strncpy((dst),(src),(len)),*((dst)+(len))='\0')
695 /* ----------------------------------------------------------------
697 * ----------------------------------------------------------------
700 extern Exception FailedAssertion;
701 extern Exception BadArg;
702 extern Exception BadState;
704 /* in utils/error/assert.c */
706 ExceptionalCondition(char *conditionName,
707 Exception * exceptionP, char *details,
708 char *fileName, int lineNumber);
712 * form is used by assert and the exception handling stuff
715 extern char *form(const char *fmt,...);
719 /* ----------------------------------------------------------------
720 * Section 10: berkeley-specific configuration
722 * this section contains settings which are only relevant to the UC Berkeley
723 * sites. Other sites can ignore this
724 * ----------------------------------------------------------------
730 * These are experimental and are not supported in the code that
731 * we distribute to other sites.
740 /* ----------------------------------------------------------------
741 * Section 11: system-specific hacks
743 * This should be limited to things that absolutely have to be
744 * included in every source file. The changes should be factored
745 * into a separate file so that changes to one port don't require
746 * changes to c.h (and everyone recompiling their whole system).
747 * ----------------------------------------------------------------
752 #include "port/hpux/fixade.h" /* for unaligned access fixup */
757 #define memmove(d, s, l) bcopy(s, d, l)
761 /* These are for things that are one way on Unix and another on NT */
762 #define NULL_DEV "/dev/null"
763 #define COPY_CMD "cp"