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.84 2000/10/03 03:11:22 momjian 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.
57 #include <sys/fcntl.h> /* ensure O_BINARY is available */
59 #ifdef HAVE_SUPPORTDEFS_H
60 #include <SupportDefs.h>
63 /* ----------------------------------------------------------------
64 * Section 1: bool, true, false, TRUE, FALSE, NULL
65 * ----------------------------------------------------------------
69 * Boolean value, either true or false.
73 /* BeOS defines bool already, but the compiler chokes on the
74 * #ifndef unless we wrap it in this check.
81 #endif /* ndef bool */
84 #define true ((bool) 1)
87 #define false ((bool) 0)
90 typedef bool *BoolPtr;
105 #define NULL ((void *) 0)
106 #endif /* !defined(NULL) */
108 /* ----------------------------------------------------------------
109 * Section 2: non-ansi C definitions:
111 * type prefixes: const, signed, volatile, inline
113 * ----------------------------------------------------------------
118 * Convert the argument to a string, using the C preprocessor.
120 * Concatenate two arguments together, using the C preprocessor.
122 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
123 * whether #identifier works, but if we have that we likely have ## too.
125 #if defined(HAVE_STRINGIZE)
127 #define CppAsString(identifier) #identifier
128 #define CppConcat(x, y) x##y
130 #else /* !HAVE_STRINGIZE */
132 #define CppAsString(identifier) "identifier"
135 * CppIdentity -- On Reiser based cpp's this is used to concatenate
136 * two tokens. That is
137 * CppIdentity(A)B ==> AB
138 * We renamed it to _private_CppIdentity because it should not
139 * be referenced outside this file. On other cpp's it
142 #define _priv_CppIdentity(x)x
143 #define CppConcat(x, y) _priv_CppIdentity(x)y
145 #endif /* !HAVE_STRINGIZE */
148 * dummyret is used to set return values in macros that use ?: to make
149 * assignments. gcc wants these to be void, other compilers like char
151 #ifdef __GNUC__ /* GNU cc */
152 #define dummyret void
154 #define dummyret char
157 /* ----------------------------------------------------------------
158 * Section 3: standard system types
159 * ----------------------------------------------------------------
164 * Variable holding address of any memory resident object.
166 * XXX Pointer arithmetic is done with this, so it can't be void *
167 * under "true" ANSI compilers.
169 typedef char *Pointer;
173 * Signed integer, EXACTLY N BITS IN SIZE,
174 * used for numerical computations and the
175 * frontend/backend protocol.
177 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
178 typedef signed char int8; /* == 8 bits */
179 typedef signed short int16; /* == 16 bits */
180 typedef signed int int32; /* == 32 bits */
181 #endif /* __BEOS__ */
184 * Unsigned integer, EXACTLY N BITS IN SIZE,
185 * used for numerical computations and the
186 * frontend/backend protocol.
188 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
189 typedef unsigned char uint8; /* == 8 bits */
190 typedef unsigned short uint16; /* == 16 bits */
191 typedef unsigned int uint32; /* == 32 bits */
192 #endif /* __BEOS__ */
195 * Floating point number, AT LEAST N BITS IN SIZE,
196 * used for numerical computations.
198 * Since sizeof(floatN) may be > sizeof(char *), always pass
199 * floatN by reference.
201 * XXX: these typedefs are now deprecated in favor of float4 and float8.
202 * They will eventually go away.
204 typedef float float32data;
205 typedef double float64data;
206 typedef float *float32;
207 typedef double *float64;
211 * Boolean value, AT LEAST N BITS IN SIZE.
213 typedef uint8 bool8; /* >= 8 bits */
214 typedef uint16 bool16; /* >= 16 bits */
215 typedef uint32 bool32; /* >= 32 bits */
219 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
221 typedef uint8 bits8; /* >= 8 bits */
222 typedef uint16 bits16; /* >= 16 bits */
223 typedef uint32 bits32; /* >= 32 bits */
227 * Unit of storage, AT LEAST N BITS IN SIZE,
228 * used to fetch/store data.
230 typedef uint8 word8; /* >= 8 bits */
231 typedef uint16 word16; /* >= 16 bits */
232 typedef uint32 word32; /* >= 32 bits */
236 * Size of any memory resident object, as returned by sizeof.
242 * Index into any memory resident array.
245 * Indices are non negative.
247 typedef unsigned int Index;
257 * Offset into any memory resident array.
260 * This differs from an Index in that an Index is always
261 * non negative, whereas Offset may be negative.
263 typedef signed int Offset;
266 * Common Postgres datatypes.
270 typedef float float4;
271 typedef double float8;
273 #ifndef __BEOS__ /* this is already defined on BeOS */
274 #ifdef HAVE_LONG_INT_64
275 /* Plain "long int" fits, use it */
276 typedef long int int64;
278 #ifdef HAVE_LONG_LONG_INT_64
279 /* We have working support for "long long int", use that */
280 typedef long long int int64;
282 /* Won't actually work, but fall back to long int so that code compiles */
283 typedef long int int64;
284 #define INT64_IS_BUSTED
287 #endif /* __BEOS__ */
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.
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 boolean value of a datum.
330 * Note: any nonzero value will be considered TRUE.
333 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
337 * Returns datum representation for a boolean.
339 * Note: any nonzero value will be considered TRUE.
342 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
346 * Returns character value of a datum.
349 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
353 * Returns datum representation for a character.
356 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
360 * Returns datum representation for an 8-bit integer.
363 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
367 * Returns 8-bit unsigned integer value of a datum.
370 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
374 * Returns datum representation for an 8-bit unsigned integer.
377 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
381 * Returns 16-bit integer value of a datum.
384 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
388 * Returns datum representation for a 16-bit integer.
391 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
395 * Returns 16-bit unsigned integer value of a datum.
398 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
402 * Returns datum representation for a 16-bit unsigned integer.
405 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
409 * Returns 32-bit integer value of a datum.
412 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
416 * Returns datum representation for a 32-bit integer.
419 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
423 * Returns 32-bit unsigned integer value of a datum.
426 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
430 * Returns datum representation for a 32-bit unsigned integer.
433 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
437 * Returns object identifier value of a datum.
440 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
444 * Returns datum representation for an object identifier.
447 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
451 * Returns pointer value of a datum.
454 #define DatumGetPointer(X) ((Pointer) (X))
458 * Returns datum representation for a pointer.
461 #define PointerGetDatum(X) ((Datum) (X))
465 * Returns C string (null-terminated string) value of a datum.
467 * Note: C string is not a full-fledged Postgres type at present,
468 * but type input functions use this conversion for their inputs.
471 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
475 * Returns datum representation for a C string (null-terminated string).
477 * Note: C string is not a full-fledged Postgres type at present,
478 * but type output functions use this conversion for their outputs.
479 * Note: CString is pass-by-reference; caller must ensure the pointed-to
480 * value has adequate lifetime.
483 #define CStringGetDatum(X) PointerGetDatum(X)
487 * Returns name value of a datum.
490 #define DatumGetName(X) ((Name) DatumGetPointer(X))
494 * Returns datum representation for a name.
496 * Note: Name is pass-by-reference; caller must ensure the pointed-to
497 * value has adequate lifetime.
500 #define NameGetDatum(X) PointerGetDatum(X)
504 * Returns 64-bit integer value of a datum.
506 * Note: this macro hides the fact that int64 is currently a
507 * pass-by-reference type. Someday it may be pass-by-value,
508 * at least on some platforms.
511 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
515 * Returns datum representation for a 64-bit integer.
517 * Note: this routine returns a reference to palloc'd space.
520 extern Datum Int64GetDatum(int64 X);
524 * Returns 4-byte floating point value of a datum.
526 * Note: this macro hides the fact that float4 is currently a
527 * pass-by-reference type. Someday it may be pass-by-value.
530 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
534 * Returns datum representation for a 4-byte floating point number.
536 * Note: this routine returns a reference to palloc'd space.
539 extern Datum Float4GetDatum(float4 X);
543 * Returns 8-byte floating point value of a datum.
545 * Note: this macro hides the fact that float8 is currently a
546 * pass-by-reference type. Someday it may be pass-by-value,
547 * at least on some platforms.
550 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
554 * Returns datum representation for an 8-byte floating point number.
556 * Note: this routine returns a reference to palloc'd space.
559 extern Datum Float8GetDatum(float8 X);
564 * Returns 32-bit floating point value of a datum.
565 * This is really a pointer, of course.
567 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
568 * It will eventually go away.
571 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
575 * Returns datum representation for a 32-bit floating point number.
576 * This is really a pointer, of course.
578 * XXX: this macro is now deprecated in favor of Float4GetDatum.
579 * It will eventually go away.
582 #define Float32GetDatum(X) PointerGetDatum(X)
586 * Returns 64-bit floating point value of a datum.
587 * This is really a pointer, of course.
589 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
590 * It will eventually go away.
593 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
597 * Returns datum representation for a 64-bit floating point number.
598 * This is really a pointer, of course.
600 * XXX: this macro is now deprecated in favor of Float8GetDatum.
601 * It will eventually go away.
604 #define Float64GetDatum(X) PointerGetDatum(X)
611 * These macros are intended to allow writing code that does not depend on
612 * whether int64, float4, float8 are pass-by-reference types, while not
613 * sacrificing performance when they are. The argument must be a variable
614 * that will exist and have the same value for as long as the Datum is needed.
615 * In the pass-by-ref case, the address of the variable is taken to use as
616 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
620 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
621 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
622 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
625 /* ----------------------------------------------------------------
626 * Section 5: IsValid macros for system types
627 * ----------------------------------------------------------------
631 * True iff bool is valid.
633 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
637 * True iff pointer is valid.
639 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
643 * True iff pointer is properly aligned to point to the given type.
645 #define PointerIsAligned(pointer, type) \
646 (((long)(pointer) % (sizeof (type))) == 0)
648 /* ----------------------------------------------------------------
649 * Section 6: offsetof, lengthof, endof
650 * ----------------------------------------------------------------
654 * Offset of a structure/union field within that structure/union.
656 * XXX This is supposed to be part of stddef.h, but isn't on
657 * some systems (like SunOS 4).
660 #define offsetof(type, field) ((long) &((type *)0)->field)
661 #endif /* offsetof */
665 * Number of elements in an array.
667 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
671 * Address of the element one past the last in an array.
673 #define endof(array) (&array[lengthof(array)])
675 /* ----------------------------------------------------------------
676 * Section 7: exception handling definitions
677 * Assert, Trap, etc macros
678 * ----------------------------------------------------------------
681 * Exception Handling definitions
684 typedef char *ExcMessage;
685 typedef struct Exception
691 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
694 * It should _NOT_ be defined in releases or in benchmark copies
699 * Generates an exception if the given condition is true.
702 #define Trap(condition, exception) \
704 if ((assert_enabled) && (condition)) \
705 ExceptionalCondition(CppAsString(condition), &(exception), \
706 (char*)NULL, __FILE__, __LINE__); \
710 * TrapMacro is the same as Trap but it's intended for use in macros:
712 * #define foo(x) (AssertM(x != 0) && bar(x))
716 #define TrapMacro(condition, exception) \
717 ((bool) ((! assert_enabled) || ! (condition) || \
718 (ExceptionalCondition(CppAsString(condition), \
720 (char*) NULL, __FILE__, __LINE__))))
722 #ifndef USE_ASSERT_CHECKING
723 #define Assert(condition)
724 #define AssertMacro(condition) ((void)true)
725 #define AssertArg(condition)
726 #define AssertState(condition)
727 #define assert_enabled 0
729 #define Assert(condition) \
730 Trap(!(condition), FailedAssertion)
732 #define AssertMacro(condition) \
733 ((void) TrapMacro(!(condition), FailedAssertion))
735 #define AssertArg(condition) \
736 Trap(!(condition), BadArg)
738 #define AssertState(condition) \
739 Trap(!(condition), BadState)
741 extern bool assert_enabled;
743 #endif /* USE_ASSERT_CHECKING */
747 * Generates an exception with a message if the given condition is true.
750 #define LogTrap(condition, exception, printArgs) \
752 if ((assert_enabled) && (condition)) \
753 ExceptionalCondition(CppAsString(condition), &(exception), \
754 vararg_format printArgs, __FILE__, __LINE__); \
758 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
760 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
762 #define LogTrapMacro(condition, exception, printArgs) \
763 ((bool) ((! assert_enabled) || ! (condition) || \
764 (ExceptionalCondition(CppAsString(condition), \
766 vararg_format printArgs, __FILE__, __LINE__))))
768 #ifndef USE_ASSERT_CHECKING
769 #define LogAssert(condition, printArgs)
770 #define LogAssertMacro(condition, printArgs) true
771 #define LogAssertArg(condition, printArgs)
772 #define LogAssertState(condition, printArgs)
774 #define LogAssert(condition, printArgs) \
775 LogTrap(!(condition), FailedAssertion, printArgs)
777 #define LogAssertMacro(condition, printArgs) \
778 LogTrapMacro(!(condition), FailedAssertion, printArgs)
780 #define LogAssertArg(condition, printArgs) \
781 LogTrap(!(condition), BadArg, printArgs)
783 #define LogAssertState(condition, printArgs) \
784 LogTrap(!(condition), BadState, printArgs)
786 #ifdef ASSERT_CHECKING_TEST
787 extern int assertTest(int val);
790 #endif /* USE_ASSERT_CHECKING */
792 /* ----------------------------------------------------------------
793 * Section 8: Min, Max, Abs macros
794 * ----------------------------------------------------------------
798 * Return the maximum of two numbers.
800 #define Max(x, y) ((x) > (y) ? (x) : (y))
804 * Return the minimum of two numbers.
806 #define Min(x, y) ((x) < (y) ? (x) : (y))
810 * Return the absolute value of the argument.
812 #define Abs(x) ((x) >= 0 ? (x) : -(x))
816 * Like standard library function strncpy(), except that result string
817 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
818 * of the source string will be kept.
819 * Also, the macro returns no result (too hard to do that without
820 * evaluating the arguments multiple times, which seems worse).
822 * BTW: when you need to copy a non-null-terminated string (like a text
823 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
824 * might seem to work, but it fetches one byte more than there is in the
825 * text object. One fine day you'll have a SIGSEGV because there isn't
826 * another byte before the end of memory. Don't laugh, we've had real
827 * live bug reports from real live users over exactly this mistake.
828 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
830 #define StrNCpy(dst,src,len) \
833 char * _dst = (dst); \
838 strncpy(_dst, (src), _len); \
839 _dst[_len-1] = '\0'; \
844 /* Get a bit mask of the bits set in non-int32 aligned addresses */
845 #define INT_ALIGN_MASK (sizeof(int32) - 1)
849 * Exactly the same as standard library function memset(), but considerably
850 * faster for zeroing small word-aligned structures (such as parsetree nodes).
851 * This has to be a macro because the main point is to avoid function-call
854 * We got the 64 number by testing this against the stock memset() on
855 * BSD/OS 3.0. Larger values were slower. bjm 1997/09/11
857 * I think the crossover point could be a good deal higher for
858 * most platforms, actually. tgl 2000-03-19
860 #define MemSet(start, val, len) \
863 int32 * _start = (int32 *) (start); \
867 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
868 (_len & INT_ALIGN_MASK) == 0 && \
870 _len <= MEMSET_LOOP_LIMIT) \
872 int32 * _stop = (int32 *) ((char *) _start + _len); \
873 while (_start < _stop) \
877 memset((char *) _start, _val, _len); \
880 #define MEMSET_LOOP_LIMIT 64
883 /* ----------------------------------------------------------------
885 * ----------------------------------------------------------------
888 extern Exception FailedAssertion;
889 extern Exception BadArg;
890 extern Exception BadState;
892 /* in utils/error/assert.c */
893 extern int ExceptionalCondition(char *conditionName,
894 Exception *exceptionP, char *details,
895 char *fileName, int lineNumber);
899 * vararg_format is used by assert and the exception handling stuff
902 extern char *vararg_format(const char *fmt,...);
906 /* ----------------------------------------------------------------
907 * Section 10: berkeley-specific configuration
909 * this section contains settings which are only relevant to the UC Berkeley
910 * sites. Other sites can ignore this
911 * ----------------------------------------------------------------
917 * These are experimental and are not supported in the code that
918 * we distribute to other sites.
922 #define STABLE_MEMORY_STORAGE
927 /* ----------------------------------------------------------------
928 * Section 11: system-specific hacks
930 * This should be limited to things that absolutely have to be
931 * included in every source file. The changes should be factored
932 * into a separate file so that changes to one port don't require
933 * changes to c.h (and everyone recompiling their whole system).
934 * ----------------------------------------------------------------
938 #define PG_BINARY O_BINARY
939 #define PG_BINARY_R "rb"
940 #define PG_BINARY_W "wb"
943 #define PG_BINARY_R "r"
944 #define PG_BINARY_W "w"
947 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
948 #define memmove(d, s, l) bcopy(s, d, l)
953 /* These are for things that are one way on Unix and another on NT */
954 #define NULL_DEV "/dev/null"
957 /* defines for dynamic linking on Win32 platform */
959 #if __GNUC__ && ! defined (__declspec)
960 #error You need egcs 1.1 or newer for compiling!
963 #define DLLIMPORT __declspec (dllexport)
964 #else /* not BUILDING_DLL */
965 #define DLLIMPORT __declspec (dllimport)
967 #else /* not CYGWIN */
971 /* Provide prototypes for routines not present in a particular machine's
972 * standard C library. It'd be better to put these in config.h, but
973 * in config.h we haven't yet included anything that defines size_t...
976 #ifndef HAVE_SNPRINTF_DECL
977 extern int snprintf(char *str, size_t count, const char *fmt,...);
981 #ifndef HAVE_VSNPRINTF_DECL
982 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
987 #include <regex/utils.h>