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.51 1999/02/13 23:20:44 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
48 have its own. The same goes for stddef and stdarg if present.
60 /* ----------------------------------------------------------------
61 * Section 1: bool, true, false, TRUE, FALSE
62 * ----------------------------------------------------------------
66 * Boolean value, either true or false.
69 #define false ((char) 0)
70 #define true ((char) 1)
74 #endif /* ndef bool */
76 typedef bool *BoolPtr;
86 /* ----------------------------------------------------------------
87 * Section 2: __STDC__, non-ansi C definitions:
90 * Pointer typedef, NULL
91 * type prefixes: const, signed, volatile, inline
92 * ----------------------------------------------------------------
95 #ifdef __STDC__ /* ANSI C */
99 * Variable holding address of any memory resident object.
103 * XXX Pointer arithmetic is done with this, so it can't be void *
104 * under "true" ANSI compilers.
106 typedef char *Pointer;
113 #define NULL ((void *) 0)
114 #endif /* !defined(NULL) */
116 #define HAVE_ANSI_CPP /* all ANSI C compilers must have this! */
117 #if defined(NEED_STD_HDRS)
118 #undef NEED_STD_HDRS /* all ANSI systems must have
120 #endif /* NEED_STD_HDRS */
122 #else /* !defined(__STDC__) *//* NOT ANSI C */
126 * Variable containing address of any memory resident object.
128 typedef char *Pointer;
136 #endif /* !defined(NULL) */
140 * Type modifier. Identifies read only variables.
143 * extern const Version RomVersion;
146 #define const /* const */
151 * Type modifier. Identifies signed integral types.
153 #define signed /* signed */
157 * Type modifier. Identifies variables which may change in ways not
158 * noticeable by the compiler, e.g. via asynchronous interrupts.
161 * extern volatile unsigned int NumberOfInterrupts;
163 #define volatile /* volatile */
165 #endif /* !defined(__STDC__) */ /* NOT ANSI C */
169 * Convert the argument to a string, using the C preprocessor.
171 * Concatenate two arguments together, using the C preprocessor.
173 #if defined(HAVE_ANSI_CPP)
175 #define CppAsString(identifier) #identifier
176 #define CppConcat(x, y) x##y
178 #else /* !HAVE_ANSI_CPP */
180 #define CppAsString(identifier) "identifier"
183 * CppIdentity -- On Reiser based cpp's this is used to concatenate
184 * two tokens. That is
185 * CppIdentity(A)B ==> AB
186 * We renamed it to _private_CppIdentity because it should not
187 * be referenced outside this file. On other cpp's it
190 #define _priv_CppIdentity(x)x
191 #define CppConcat(x, y) _priv_CppIdentity(x)y
193 #endif /* !HAVE_ANSI_CPP */
195 #ifndef __GNUC__ /* GNU cc */
198 #ifndef __GNUC__ /* GNU cc */
201 * dummyret is used to set return values in macros that use ?: to make
202 * assignments. gcc wants these to be void, other compilers like char
204 #define dummyret char
206 #define dummyret void
209 #if defined(NEED_STD_HDRS)
211 * You're doomed. We've removed almost all of our own C library
212 * extern declarations because they conflict on the different
213 * systems. You'll have to write your own stdlib.h.
216 #else /* NEED_STD_HDRS */
219 #endif /* NEED_STD_HDRS */
221 /* ----------------------------------------------------------------
222 * Section 3: standard system types
223 * ----------------------------------------------------------------
228 * Signed integer, EXACTLY N BITS IN SIZE,
229 * used for numerical computations and the
230 * frontend/backend protocol.
232 typedef signed char int8; /* == 8 bits */
233 typedef signed short int16; /* == 16 bits */
234 typedef signed int int32; /* == 32 bits */
238 * Unsigned integer, EXACTLY N BITS IN SIZE,
239 * used for numerical computations and the
240 * frontend/backend protocol.
242 typedef unsigned char uint8; /* == 8 bits */
243 typedef unsigned short uint16; /* == 16 bits */
244 typedef unsigned int uint32; /* == 32 bits */
248 * Floating point number, AT LEAST N BITS IN SIZE,
249 * used for numerical computations.
251 * Since sizeof(floatN) may be > sizeof(char *), always pass
252 * floatN by reference.
254 typedef float float32data;
255 typedef double float64data;
256 typedef float *float32;
257 typedef double *float64;
261 * Boolean value, AT LEAST N BITS IN SIZE.
263 typedef uint8 bool8; /* >= 8 bits */
264 typedef uint16 bool16; /* >= 16 bits */
265 typedef uint32 bool32; /* >= 32 bits */
269 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
271 typedef uint8 bits8; /* >= 8 bits */
272 typedef uint16 bits16; /* >= 16 bits */
273 typedef uint32 bits32; /* >= 32 bits */
277 * Unit of storage, AT LEAST N BITS IN SIZE,
278 * used to fetch/store data.
280 typedef uint8 word8; /* >= 8 bits */
281 typedef uint16 word16; /* >= 16 bits */
282 typedef uint32 word32; /* >= 32 bits */
286 * Size of any memory resident object, as returned by sizeof.
292 * Index into any memory resident array.
295 * Indices are non negative.
297 typedef unsigned int Index;
307 * Offset into any memory resident array.
310 * This differs from an Index in that an Index is always
311 * non negative, whereas Offset may be negative.
313 typedef signed int Offset;
315 /* ----------------------------------------------------------------
316 * Section 4: datum type + support macros
317 * ----------------------------------------------------------------
321 * POSTGRES abstract data type datum representation definitions.
326 * Postgres makes the following assumption about machines:
328 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
330 * Postgres also assumes that
338 * If your machine meets these requirements, Datums should also be checked
339 * to see if the positioning is correct.
341 * This file is MACHINE AND COMPILER dependent!!!
344 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
345 typedef Datum *DatumPtr;
347 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
348 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
349 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
350 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
351 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
352 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
356 * Returns character value of a datum.
359 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
363 * Returns datum representation for a character.
366 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
370 * Returns datum representation for an 8-bit integer.
373 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
377 * Returns 8-bit unsigned integer value of a datum.
380 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
384 * Returns datum representation for an 8-bit unsigned integer.
387 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
391 * Returns 16-bit integer value of a datum.
394 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
398 * Returns datum representation for a 16-bit integer.
401 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
405 * Returns 16-bit unsigned integer value of a datum.
408 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
412 * Returns datum representation for a 16-bit unsigned integer.
415 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
419 * Returns 32-bit integer value of a datum.
422 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
426 * Returns datum representation for a 32-bit integer.
429 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
433 * Returns 32-bit unsigned integer value of a datum.
436 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
440 * Returns datum representation for a 32-bit unsigned integer.
443 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
447 * Returns object identifier value of a datum.
450 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
454 * Returns datum representation for an object identifier.
457 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
461 * Returns pointer value of a datum.
464 #define DatumGetPointer(X) ((Pointer) X)
468 * Returns datum representation for a pointer.
471 #define PointerGetDatum(X) ((Datum) X)
475 * Returns name value of a datum.
478 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
482 * Returns datum representation for a name.
485 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
490 * Returns 32-bit floating point value of a datum.
491 * This is really a pointer, of course.
494 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
498 * Returns datum representation for a 32-bit floating point number.
499 * This is really a pointer, of course.
502 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
506 * Returns 64-bit floating point value of a datum.
507 * This is really a pointer, of course.
510 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
514 * Returns datum representation for a 64-bit floating point number.
515 * This is really a pointer, of course.
518 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
520 /* ----------------------------------------------------------------
521 * Section 5: IsValid macros for system types
522 * ----------------------------------------------------------------
526 * True iff bool is valid.
528 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
532 * True iff pointer is valid.
534 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
538 * True iff pointer is within given bounds.
541 * Assumes the bounded interval to be [min,max),
542 * i.e. closed on the left and open on the right.
544 #define PointerIsInBounds(pointer, min, max) \
545 ((min) <= (pointer) && (pointer) < (max))
549 * True iff pointer is properly aligned to point to the given type.
551 #define PointerIsAligned(pointer, type) \
552 (((long)(pointer) % (sizeof (type))) == 0)
554 /* ----------------------------------------------------------------
555 * Section 6: offsetof, lengthof, endof
556 * ----------------------------------------------------------------
560 * Offset of a structure/union field within that structure/union.
562 * XXX This is supposed to be part of stddef.h, but isn't on
563 * some systems (like SunOS 4).
566 #define offsetof(type, field) ((long) &((type *)0)->field)
567 #endif /* offsetof */
571 * Number of elements in an array.
573 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
577 * Address of the element one past the last in an array.
579 #define endof(array) (&array[lengthof(array)])
581 /* ----------------------------------------------------------------
582 * Section 7: exception handling definitions
583 * Assert, Trap, etc macros
584 * ----------------------------------------------------------------
587 * Exception Handling definitions
590 typedef char *ExcMessage;
591 typedef struct Exception
597 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
600 * It should _NOT_ be defined in releases or in benchmark copies
605 * Generates an exception if the given condition is true.
608 #define Trap(condition, exception) \
609 { if ((assert_enabled) && (condition)) \
610 ExceptionalCondition(CppAsString(condition), &(exception), \
611 (char*)NULL, __FILE__, __LINE__); }
614 * TrapMacro is the same as Trap but it's intended for use in macros:
616 * #define foo(x) (AssertM(x != 0) && bar(x))
620 #define TrapMacro(condition, exception) \
621 ((bool) ((! assert_enabled) || (! condition) || \
622 (ExceptionalCondition(CppAsString(condition), \
624 (char*) NULL, __FILE__, __LINE__))))
626 #ifndef USE_ASSERT_CHECKING
627 #define Assert(condition)
628 #define AssertMacro(condition) (void)true
629 #define AssertArg(condition)
630 #define AssertState(condition)
631 #define assert_enabled 0
633 #define Assert(condition) \
634 Trap(!(condition), FailedAssertion)
636 #define AssertMacro(condition) \
637 (void)TrapMacro(!(condition), FailedAssertion)
639 #define AssertArg(condition) \
640 Trap(!(condition), BadArg)
642 #define AssertState(condition) \
643 Trap(!(condition), BadState)
645 extern int assert_enabled;
647 #endif /* USE_ASSERT_CHECKING */
651 * Generates an exception with a message if the given condition is true.
654 #define LogTrap(condition, exception, printArgs) \
655 { if ((assert_enabled) && (condition)) \
656 ExceptionalCondition(CppAsString(condition), &(exception), \
657 form printArgs, __FILE__, __LINE__); }
660 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
662 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
664 #define LogTrapMacro(condition, exception, printArgs) \
665 ((bool) ((! assert_enabled) || (! condition) || \
666 (ExceptionalCondition(CppAsString(condition), \
668 form printArgs, __FILE__, __LINE__))))
670 #ifndef USE_ASSERT_CHECKING
671 #define LogAssert(condition, printArgs)
672 #define LogAssertMacro(condition, printArgs) true
673 #define LogAssertArg(condition, printArgs)
674 #define LogAssertState(condition, printArgs)
676 #define LogAssert(condition, printArgs) \
677 LogTrap(!(condition), FailedAssertion, printArgs)
679 #define LogAssertMacro(condition, printArgs) \
680 LogTrapMacro(!(condition), FailedAssertion, printArgs)
682 #define LogAssertArg(condition, printArgs) \
683 LogTrap(!(condition), BadArg, printArgs)
685 #define LogAssertState(condition, printArgs) \
686 LogTrap(!(condition), BadState, printArgs)
688 extern int assertEnable(int val);
690 #ifdef ASSERT_CHECKING_TEST
691 extern int assertTest(int val);
694 #endif /* USE_ASSERT_CHECKING */
696 /* ----------------------------------------------------------------
697 * Section 8: Min, Max, Abs macros
698 * ----------------------------------------------------------------
702 * Return the maximum of two numbers.
704 #define Max(x, y) ((x) > (y) ? (x) : (y))
708 * Return the minimum of two numbers.
710 #define Min(x, y) ((x) < (y) ? (x) : (y))
714 * Return the absolute value of the argument.
716 #define Abs(x) ((x) >= 0 ? (x) : -(x))
720 * Does string copy, and forces terminating NULL
722 /* we do this so if the macro is used in an if action, it will work */
723 #define StrNCpy(dst,src,len) \
727 strncpy((dst),(src),(len)-1), \
728 *((dst)+(len)-1)='\0' \
731 (dummyret)NULL,(void)(dst) \
734 /* Get a bit mask of the bits set in non-int32 aligned addresses */
735 #define INT_ALIGN_MASK (sizeof(int32) - 1)
738 * This function gets call too often, so we inline it if we can.
739 * Are we aligned for int32?
740 * We have to cast the pointer to int so we can do the AND
741 * We got the 64 number by testing this against the stock memset() on
742 * BSD/OS 3.0. Larger values were slower.
744 #define MemSet(start, val, len) do \
746 if (((long)(start) & INT_ALIGN_MASK) == 0 && \
747 ((len) & INT_ALIGN_MASK) == 0 && \
751 int32 *_i = (int32 *)(start); \
752 int32 *_stop = (int32 *)((char *)(start) + (len)); \
758 memset((start), (val), (len)); \
761 /* ----------------------------------------------------------------
763 * ----------------------------------------------------------------
766 extern Exception FailedAssertion;
767 extern Exception BadArg;
768 extern Exception BadState;
770 /* in utils/error/assert.c */
771 extern int ExceptionalCondition(char *conditionName,
772 Exception *exceptionP, char *details,
773 char *fileName, int lineNumber);
777 * form is used by assert and the exception handling stuff
780 extern char *form(const char *fmt,...);
784 /* ----------------------------------------------------------------
785 * Section 10: berkeley-specific configuration
787 * this section contains settings which are only relevant to the UC Berkeley
788 * sites. Other sites can ignore this
789 * ----------------------------------------------------------------
795 * These are experimental and are not supported in the code that
796 * we distribute to other sites.
800 #define STABLE_MEMORY_STORAGE
805 /* ----------------------------------------------------------------
806 * Section 11: system-specific hacks
808 * This should be limited to things that absolutely have to be
809 * included in every source file. The changes should be factored
810 * into a separate file so that changes to one port don't require
811 * changes to c.h (and everyone recompiling their whole system).
812 * ----------------------------------------------------------------
817 #include "port/hpux/fixade.h" /* for unaligned access fixup */
821 #if defined(sun) && defined(sparc) && !defined(__SVR4)
822 #define memmove(d, s, l) bcopy(s, d, l)
827 /* These are for things that are one way on Unix and another on NT */
828 #define NULL_DEV "/dev/null"
829 #define COPY_CMD "cp"
832 /* Provide prototypes for routines not present in a particular machine's
833 * standard C library. It'd be better to put these in config.h, but
834 * in config.h we haven't yet included anything that defines size_t...
837 #ifndef HAVE_SNPRINTF
838 extern int snprintf(char *str, size_t count, const char *fmt, ...);
841 #ifndef HAVE_VSNPRINTF
842 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);