1 /*-------------------------------------------------------------------------
4 * Fundamental C definitions. This is included by every .c file in
8 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Id: c.h,v 1.76 2000/07/07 21:12:47 tgl Exp $
13 *-------------------------------------------------------------------------
18 * When adding stuff to this file, please try to put stuff
19 * into the relevant section, or add new sections as appropriate.
22 * ------- ------------------------------------------------
23 * 1) bool, true, false, TRUE, FALSE, NULL
24 * 2) non-ansi C definitions:
25 * 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
37 * ----------------------------------------------------------------
42 /* We have to include stdlib.h here because it defines many of these macros
43 on some platforms, and we only want our definitions used if stdlib.h doesn't
44 have its own. The same goes for stddef and stdarg if present.
53 #include <sys/fcntl.h>
63 /* ----------------------------------------------------------------
64 * Section 1: bool, true, false, TRUE, FALSE, NULL
65 * ----------------------------------------------------------------
69 * Boolean value, either true or false.
76 #endif /* ndef bool */
79 #define true ((bool) 1)
82 #define false ((bool) 0)
84 typedef bool *BoolPtr;
99 #define NULL ((void *) 0)
100 #endif /* !defined(NULL) */
102 /* ----------------------------------------------------------------
103 * Section 2: non-ansi C definitions:
105 * type prefixes: const, signed, volatile, inline
107 * ----------------------------------------------------------------
112 * Convert the argument to a string, using the C preprocessor.
114 * Concatenate two arguments together, using the C preprocessor.
116 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
117 * whether #identifier works, but if we have that we likely have ## too.
119 #if defined(HAVE_STRINGIZE)
121 #define CppAsString(identifier) #identifier
122 #define CppConcat(x, y) x##y
124 #else /* !HAVE_STRINGIZE */
126 #define CppAsString(identifier) "identifier"
129 * CppIdentity -- On Reiser based cpp's this is used to concatenate
130 * two tokens. That is
131 * CppIdentity(A)B ==> AB
132 * We renamed it to _private_CppIdentity because it should not
133 * be referenced outside this file. On other cpp's it
136 #define _priv_CppIdentity(x)x
137 #define CppConcat(x, y) _priv_CppIdentity(x)y
139 #endif /* !HAVE_STRINGIZE */
142 * dummyret is used to set return values in macros that use ?: to make
143 * assignments. gcc wants these to be void, other compilers like char
145 #ifdef __GNUC__ /* GNU cc */
146 #define dummyret void
148 #define dummyret char
151 /* ----------------------------------------------------------------
152 * Section 3: standard system types
153 * ----------------------------------------------------------------
158 * Variable holding address of any memory resident object.
160 * XXX Pointer arithmetic is done with this, so it can't be void *
161 * under "true" ANSI compilers.
163 typedef char *Pointer;
167 * Signed integer, EXACTLY N BITS IN SIZE,
168 * used for numerical computations and the
169 * frontend/backend protocol.
171 typedef signed char int8; /* == 8 bits */
172 typedef signed short int16; /* == 16 bits */
173 typedef signed int int32; /* == 32 bits */
177 * Unsigned integer, EXACTLY N BITS IN SIZE,
178 * used for numerical computations and the
179 * frontend/backend protocol.
181 typedef unsigned char uint8; /* == 8 bits */
182 typedef unsigned short uint16; /* == 16 bits */
183 typedef unsigned int uint32; /* == 32 bits */
187 * Floating point number, AT LEAST N BITS IN SIZE,
188 * used for numerical computations.
190 * Since sizeof(floatN) may be > sizeof(char *), always pass
191 * floatN by reference.
193 typedef float float32data;
194 typedef double float64data;
195 typedef float *float32;
196 typedef double *float64;
200 * Boolean value, AT LEAST N BITS IN SIZE.
202 typedef uint8 bool8; /* >= 8 bits */
203 typedef uint16 bool16; /* >= 16 bits */
204 typedef uint32 bool32; /* >= 32 bits */
208 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
210 typedef uint8 bits8; /* >= 8 bits */
211 typedef uint16 bits16; /* >= 16 bits */
212 typedef uint32 bits32; /* >= 32 bits */
216 * Unit of storage, AT LEAST N BITS IN SIZE,
217 * used to fetch/store data.
219 typedef uint8 word8; /* >= 8 bits */
220 typedef uint16 word16; /* >= 16 bits */
221 typedef uint32 word32; /* >= 32 bits */
225 * Size of any memory resident object, as returned by sizeof.
231 * Index into any memory resident array.
234 * Indices are non negative.
236 typedef unsigned int Index;
246 * Offset into any memory resident array.
249 * This differs from an Index in that an Index is always
250 * non negative, whereas Offset may be negative.
252 typedef signed int Offset;
255 * Common Postgres datatypes.
259 typedef float float4;
260 typedef double float8;
262 #ifdef HAVE_LONG_INT_64
263 /* Plain "long int" fits, use it */
264 typedef long int int64;
266 #ifdef HAVE_LONG_LONG_INT_64
267 /* We have working support for "long long int", use that */
268 typedef long long int int64;
270 /* Won't actually work, but fall back to long int so that code compiles */
271 typedef long int int64;
272 #define INT64_IS_BUSTED
276 /* ----------------------------------------------------------------
277 * Section 4: datum type + support macros
278 * ----------------------------------------------------------------
282 * POSTGRES abstract data type datum representation definitions.
287 * Postgres makes the following assumption about machines:
289 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
291 * Postgres also assumes that
299 * If your machine meets these requirements, Datums should also be checked
300 * to see if the positioning is correct.
303 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
304 typedef Datum *DatumPtr;
306 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
307 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
308 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
309 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
310 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
311 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
315 * Returns boolean value of a datum.
317 * Note: any nonzero value will be considered TRUE.
320 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
324 * Returns datum representation for a boolean.
326 * Note: any nonzero value will be considered TRUE.
329 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
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))
424 * Returns object identifier value of a datum.
427 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
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 C string (null-terminated string) value of a datum.
454 * Note: C string is not a full-fledged Postgres type at present,
455 * but type input functions use this conversion for their inputs.
458 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
462 * Returns datum representation for a C string (null-terminated string).
464 * Note: C string is not a full-fledged Postgres type at present,
465 * but type output functions use this conversion for their outputs.
466 * Note: CString is pass-by-reference; caller must ensure the pointed-to
467 * value has adequate lifetime.
470 #define CStringGetDatum(X) PointerGetDatum(X)
474 * Returns name value of a datum.
477 #define DatumGetName(X) ((Name) DatumGetPointer(X))
481 * Returns datum representation for a name.
483 * Note: Name is pass-by-reference; caller must ensure the pointed-to
484 * value has adequate lifetime.
487 #define NameGetDatum(X) PointerGetDatum(X)
491 * Returns 64-bit integer value of a datum.
493 * Note: this macro hides the fact that int64 is currently a
494 * pass-by-reference type. Someday it may be pass-by-value,
495 * at least on some platforms.
498 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
502 * Returns datum representation for a 64-bit integer.
504 * Note: this routine returns a reference to palloc'd space.
507 extern Datum Int64GetDatum(int64 X);
511 * Returns 4-byte floating point value of a datum.
513 * Note: this macro hides the fact that float4 is currently a
514 * pass-by-reference type. Someday it may be pass-by-value.
517 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
521 * Returns datum representation for a 4-byte floating point number.
523 * Note: this routine returns a reference to palloc'd space.
526 extern Datum Float4GetDatum(float4 X);
530 * Returns 8-byte floating point value of a datum.
532 * Note: this macro hides the fact that float8 is currently a
533 * pass-by-reference type. Someday it may be pass-by-value,
534 * at least on some platforms.
537 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
541 * Returns datum representation for an 8-byte floating point number.
543 * Note: this routine returns a reference to palloc'd space.
546 extern Datum Float8GetDatum(float8 X);
551 * Returns 32-bit floating point value of a datum.
552 * This is really a pointer, of course.
554 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
555 * It will eventually go away.
558 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
562 * Returns datum representation for a 32-bit floating point number.
563 * This is really a pointer, of course.
565 * XXX: this macro is now deprecated in favor of Float4GetDatum.
566 * It will eventually go away.
569 #define Float32GetDatum(X) PointerGetDatum(X)
573 * Returns 64-bit floating point value of a datum.
574 * This is really a pointer, of course.
576 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
577 * It will eventually go away.
580 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
584 * Returns datum representation for a 64-bit floating point number.
585 * This is really a pointer, of course.
587 * XXX: this macro is now deprecated in favor of Float8GetDatum.
588 * It will eventually go away.
591 #define Float64GetDatum(X) PointerGetDatum(X)
593 /* ----------------------------------------------------------------
594 * Section 5: IsValid macros for system types
595 * ----------------------------------------------------------------
599 * True iff bool is valid.
601 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
605 * True iff pointer is valid.
607 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
611 * True iff pointer is properly aligned to point to the given type.
613 #define PointerIsAligned(pointer, type) \
614 (((long)(pointer) % (sizeof (type))) == 0)
616 /* ----------------------------------------------------------------
617 * Section 6: offsetof, lengthof, endof
618 * ----------------------------------------------------------------
622 * Offset of a structure/union field within that structure/union.
624 * XXX This is supposed to be part of stddef.h, but isn't on
625 * some systems (like SunOS 4).
628 #define offsetof(type, field) ((long) &((type *)0)->field)
629 #endif /* offsetof */
633 * Number of elements in an array.
635 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
639 * Address of the element one past the last in an array.
641 #define endof(array) (&array[lengthof(array)])
643 /* ----------------------------------------------------------------
644 * Section 7: exception handling definitions
645 * Assert, Trap, etc macros
646 * ----------------------------------------------------------------
649 * Exception Handling definitions
652 typedef char *ExcMessage;
653 typedef struct Exception
659 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
662 * It should _NOT_ be defined in releases or in benchmark copies
667 * Generates an exception if the given condition is true.
670 #define Trap(condition, exception) \
672 if ((assert_enabled) && (condition)) \
673 ExceptionalCondition(CppAsString(condition), &(exception), \
674 (char*)NULL, __FILE__, __LINE__); \
678 * TrapMacro is the same as Trap but it's intended for use in macros:
680 * #define foo(x) (AssertM(x != 0) && bar(x))
684 #define TrapMacro(condition, exception) \
685 ((bool) ((! assert_enabled) || ! (condition) || \
686 (ExceptionalCondition(CppAsString(condition), \
688 (char*) NULL, __FILE__, __LINE__))))
690 #ifndef USE_ASSERT_CHECKING
691 #define Assert(condition)
692 #define AssertMacro(condition) ((void)true)
693 #define AssertArg(condition)
694 #define AssertState(condition)
695 #define assert_enabled 0
697 #define Assert(condition) \
698 Trap(!(condition), FailedAssertion)
700 #define AssertMacro(condition) \
701 ((void) TrapMacro(!(condition), FailedAssertion))
703 #define AssertArg(condition) \
704 Trap(!(condition), BadArg)
706 #define AssertState(condition) \
707 Trap(!(condition), BadState)
709 extern int assert_enabled;
711 #endif /* USE_ASSERT_CHECKING */
715 * Generates an exception with a message if the given condition is true.
718 #define LogTrap(condition, exception, printArgs) \
720 if ((assert_enabled) && (condition)) \
721 ExceptionalCondition(CppAsString(condition), &(exception), \
722 vararg_format printArgs, __FILE__, __LINE__); \
726 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
728 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
730 #define LogTrapMacro(condition, exception, printArgs) \
731 ((bool) ((! assert_enabled) || ! (condition) || \
732 (ExceptionalCondition(CppAsString(condition), \
734 vararg_format printArgs, __FILE__, __LINE__))))
736 #ifndef USE_ASSERT_CHECKING
737 #define LogAssert(condition, printArgs)
738 #define LogAssertMacro(condition, printArgs) true
739 #define LogAssertArg(condition, printArgs)
740 #define LogAssertState(condition, printArgs)
742 #define LogAssert(condition, printArgs) \
743 LogTrap(!(condition), FailedAssertion, printArgs)
745 #define LogAssertMacro(condition, printArgs) \
746 LogTrapMacro(!(condition), FailedAssertion, printArgs)
748 #define LogAssertArg(condition, printArgs) \
749 LogTrap(!(condition), BadArg, printArgs)
751 #define LogAssertState(condition, printArgs) \
752 LogTrap(!(condition), BadState, printArgs)
754 #ifdef ASSERT_CHECKING_TEST
755 extern int assertTest(int val);
758 #endif /* USE_ASSERT_CHECKING */
760 /* ----------------------------------------------------------------
761 * Section 8: Min, Max, Abs macros
762 * ----------------------------------------------------------------
766 * Return the maximum of two numbers.
768 #define Max(x, y) ((x) > (y) ? (x) : (y))
772 * Return the minimum of two numbers.
774 #define Min(x, y) ((x) < (y) ? (x) : (y))
778 * Return the absolute value of the argument.
780 #define Abs(x) ((x) >= 0 ? (x) : -(x))
784 * Like standard library function strncpy(), except that result string
785 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
786 * of the source string will be kept.
787 * Also, the macro returns no result (too hard to do that without
788 * evaluating the arguments multiple times, which seems worse).
790 * BTW: when you need to copy a non-null-terminated string (like a text
791 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
792 * might seem to work, but it fetches one byte more than there is in the
793 * text object. One fine day you'll have a SIGSEGV because there isn't
794 * another byte before the end of memory. Don't laugh, we've had real
795 * live bug reports from real live users over exactly this mistake.
796 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
798 #define StrNCpy(dst,src,len) \
801 char * _dst = (dst); \
806 strncpy(_dst, (src), _len); \
807 _dst[_len-1] = '\0'; \
812 /* Get a bit mask of the bits set in non-int32 aligned addresses */
813 #define INT_ALIGN_MASK (sizeof(int32) - 1)
817 * Exactly the same as standard library function memset(), but considerably
818 * faster for zeroing small word-aligned structures (such as parsetree nodes).
819 * This has to be a macro because the main point is to avoid function-call
822 * We got the 64 number by testing this against the stock memset() on
823 * BSD/OS 3.0. Larger values were slower. bjm 1997/09/11
825 * I think the crossover point could be a good deal higher for
826 * most platforms, actually. tgl 2000-03-19
828 #define MemSet(start, val, len) \
831 int32 * _start = (int32 *) (start); \
835 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
836 (_len & INT_ALIGN_MASK) == 0 && \
838 _len <= MEMSET_LOOP_LIMIT) \
840 int32 * _stop = (int32 *) ((char *) _start + _len); \
841 while (_start < _stop) \
845 memset((char *) _start, _val, _len); \
848 #define MEMSET_LOOP_LIMIT 64
851 /* ----------------------------------------------------------------
853 * ----------------------------------------------------------------
856 extern Exception FailedAssertion;
857 extern Exception BadArg;
858 extern Exception BadState;
860 /* in utils/error/assert.c */
861 extern int ExceptionalCondition(char *conditionName,
862 Exception *exceptionP, char *details,
863 char *fileName, int lineNumber);
867 * vararg_format is used by assert and the exception handling stuff
870 extern char *vararg_format(const char *fmt,...);
874 /* ----------------------------------------------------------------
875 * Section 10: berkeley-specific configuration
877 * this section contains settings which are only relevant to the UC Berkeley
878 * sites. Other sites can ignore this
879 * ----------------------------------------------------------------
885 * These are experimental and are not supported in the code that
886 * we distribute to other sites.
890 #define STABLE_MEMORY_STORAGE
895 /* ----------------------------------------------------------------
896 * Section 11: system-specific hacks
898 * This should be limited to things that absolutely have to be
899 * included in every source file. The changes should be factored
900 * into a separate file so that changes to one port don't require
901 * changes to c.h (and everyone recompiling their whole system).
902 * ----------------------------------------------------------------
906 #define PG_BINARY O_BINARY
907 #define PG_BINARY_R "rb"
908 #define PG_BINARY_W "wb"
911 #define PG_BINARY_R "r"
912 #define PG_BINARY_W "w"
915 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
916 #define memmove(d, s, l) bcopy(s, d, l)
921 /* These are for things that are one way on Unix and another on NT */
922 #define NULL_DEV "/dev/null"
925 /* defines for dynamic linking on Win32 platform */
927 #if __GNUC__ && ! defined (__declspec)
928 #error You need egcs 1.1 or newer for compiling!
931 #define DLLIMPORT __declspec (dllexport)
932 #else /* not BUILDING_DLL */
933 #define DLLIMPORT __declspec (dllimport)
935 #else /* not CYGWIN */
939 /* Provide prototypes for routines not present in a particular machine's
940 * standard C library. It'd be better to put these in config.h, but
941 * in config.h we haven't yet included anything that defines size_t...
944 #ifndef HAVE_SNPRINTF_DECL
945 extern int snprintf(char *str, size_t count, const char *fmt,...);
949 #ifndef HAVE_VSNPRINTF_DECL
950 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
955 #include <regex/utils.h>