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.88 2001/01/21 00:59:24 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.
54 #ifdef STRING_H_WITH_STRINGS_H
60 #include <sys/fcntl.h> /* ensure O_BINARY is available */
62 #ifdef HAVE_SUPPORTDEFS_H
63 #include <SupportDefs.h>
66 /* ----------------------------------------------------------------
67 * Section 1: bool, true, false, TRUE, FALSE, NULL
68 * ----------------------------------------------------------------
72 * Boolean value, either true or false.
76 /* BeOS defines bool already, but the compiler chokes on the
77 * #ifndef unless we wrap it in this check.
84 #endif /* ndef bool */
87 #define true ((bool) 1)
90 #define false ((bool) 0)
93 typedef bool *BoolPtr;
108 #define NULL ((void *) 0)
109 #endif /* !defined(NULL) */
111 /* ----------------------------------------------------------------
112 * Section 2: non-ansi C definitions:
114 * type prefixes: const, signed, volatile, inline
116 * ----------------------------------------------------------------
121 * Convert the argument to a string, using the C preprocessor.
123 * Concatenate two arguments together, using the C preprocessor.
125 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
126 * whether #identifier works, but if we have that we likely have ## too.
128 #if defined(HAVE_STRINGIZE)
130 #define CppAsString(identifier) #identifier
131 #define CppConcat(x, y) x##y
133 #else /* !HAVE_STRINGIZE */
135 #define CppAsString(identifier) "identifier"
138 * CppIdentity -- On Reiser based cpp's this is used to concatenate
139 * two tokens. That is
140 * CppIdentity(A)B ==> AB
141 * We renamed it to _private_CppIdentity because it should not
142 * be referenced outside this file. On other cpp's it
145 #define _priv_CppIdentity(x)x
146 #define CppConcat(x, y) _priv_CppIdentity(x)y
148 #endif /* !HAVE_STRINGIZE */
151 * dummyret is used to set return values in macros that use ?: to make
152 * assignments. gcc wants these to be void, other compilers like char
154 #ifdef __GNUC__ /* GNU cc */
155 #define dummyret void
157 #define dummyret char
160 /* ----------------------------------------------------------------
161 * Section 3: standard system types
162 * ----------------------------------------------------------------
167 * Variable holding address of any memory resident object.
169 * XXX Pointer arithmetic is done with this, so it can't be void *
170 * under "true" ANSI compilers.
172 typedef char *Pointer;
176 * Signed integer, EXACTLY N BITS IN SIZE,
177 * used for numerical computations and the
178 * frontend/backend protocol.
180 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
181 typedef signed char int8; /* == 8 bits */
182 typedef signed short int16; /* == 16 bits */
183 typedef signed int int32; /* == 32 bits */
184 #endif /* __BEOS__ */
187 * Unsigned integer, EXACTLY N BITS IN SIZE,
188 * used for numerical computations and the
189 * frontend/backend protocol.
191 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
192 typedef unsigned char uint8; /* == 8 bits */
193 typedef unsigned short uint16; /* == 16 bits */
194 typedef unsigned int uint32; /* == 32 bits */
195 #endif /* __BEOS__ */
198 * Floating point number, AT LEAST N BITS IN SIZE,
199 * used for numerical computations.
201 * Since sizeof(floatN) may be > sizeof(char *), always pass
202 * floatN by reference.
204 * XXX: these typedefs are now deprecated in favor of float4 and float8.
205 * They will eventually go away.
207 typedef float float32data;
208 typedef double float64data;
209 typedef float *float32;
210 typedef double *float64;
214 * Boolean value, AT LEAST N BITS IN SIZE.
216 typedef uint8 bool8; /* >= 8 bits */
217 typedef uint16 bool16; /* >= 16 bits */
218 typedef uint32 bool32; /* >= 32 bits */
222 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
224 typedef uint8 bits8; /* >= 8 bits */
225 typedef uint16 bits16; /* >= 16 bits */
226 typedef uint32 bits32; /* >= 32 bits */
230 * Unit of storage, AT LEAST N BITS IN SIZE,
231 * used to fetch/store data.
233 typedef uint8 word8; /* >= 8 bits */
234 typedef uint16 word16; /* >= 16 bits */
235 typedef uint32 word32; /* >= 32 bits */
239 * Size of any memory resident object, as returned by sizeof.
245 * Index into any memory resident array.
248 * Indices are non negative.
250 typedef unsigned int Index;
260 * Offset into any memory resident array.
263 * This differs from an Index in that an Index is always
264 * non negative, whereas Offset may be negative.
266 typedef signed int Offset;
269 * Common Postgres datatypes.
273 typedef float float4;
274 typedef double float8;
276 #ifndef __BEOS__ /* this is already defined on BeOS */
277 #ifdef HAVE_LONG_INT_64
278 /* Plain "long int" fits, use it */
279 typedef long int int64;
280 typedef unsigned long int uint64;
282 #ifdef HAVE_LONG_LONG_INT_64
283 /* We have working support for "long long int", use that */
284 typedef long long int int64;
285 typedef unsigned long long int uint64;
287 /* Won't actually work, but fall back to long int so that code compiles */
288 typedef long int int64;
289 typedef unsigned long int uint64;
290 #define INT64_IS_BUSTED
293 #endif /* __BEOS__ */
295 /* ----------------------------------------------------------------
296 * Section 4: datum type + support macros
297 * ----------------------------------------------------------------
301 * POSTGRES abstract data type datum representation definitions.
306 * Postgres makes the following assumption about machines:
308 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
310 * Postgres also assumes that
318 * If your machine meets these requirements, Datums should also be checked
319 * to see if the positioning is correct.
322 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
323 typedef Datum *DatumPtr;
325 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
326 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
327 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
328 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
329 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
330 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
334 * Returns boolean value of a datum.
336 * Note: any nonzero value will be considered TRUE.
339 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
343 * Returns datum representation for a boolean.
345 * Note: any nonzero value will be considered TRUE.
348 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
352 * Returns character value of a datum.
355 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
359 * Returns datum representation for a character.
362 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
366 * Returns datum representation for an 8-bit integer.
369 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
373 * Returns 8-bit unsigned integer value of a datum.
376 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
380 * Returns datum representation for an 8-bit unsigned integer.
383 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
387 * Returns 16-bit integer value of a datum.
390 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
394 * Returns datum representation for a 16-bit integer.
397 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
401 * Returns 16-bit unsigned integer value of a datum.
404 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
408 * Returns datum representation for a 16-bit unsigned integer.
411 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
415 * Returns 32-bit integer value of a datum.
418 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
422 * Returns datum representation for a 32-bit integer.
425 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
429 * Returns 32-bit unsigned integer value of a datum.
432 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
436 * Returns datum representation for a 32-bit unsigned integer.
439 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
443 * Returns object identifier value of a datum.
446 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
450 * Returns datum representation for an object identifier.
453 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
457 * Returns pointer value of a datum.
460 #define DatumGetPointer(X) ((Pointer) (X))
464 * Returns datum representation for a pointer.
467 #define PointerGetDatum(X) ((Datum) (X))
471 * Returns C string (null-terminated string) value of a datum.
473 * Note: C string is not a full-fledged Postgres type at present,
474 * but type input functions use this conversion for their inputs.
477 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
481 * Returns datum representation for a C string (null-terminated string).
483 * Note: C string is not a full-fledged Postgres type at present,
484 * but type output functions use this conversion for their outputs.
485 * Note: CString is pass-by-reference; caller must ensure the pointed-to
486 * value has adequate lifetime.
489 #define CStringGetDatum(X) PointerGetDatum(X)
493 * Returns name value of a datum.
496 #define DatumGetName(X) ((Name) DatumGetPointer(X))
500 * Returns datum representation for a name.
502 * Note: Name is pass-by-reference; caller must ensure the pointed-to
503 * value has adequate lifetime.
506 #define NameGetDatum(X) PointerGetDatum(X)
510 * Returns 64-bit integer value of a datum.
512 * Note: this macro hides the fact that int64 is currently a
513 * pass-by-reference type. Someday it may be pass-by-value,
514 * at least on some platforms.
517 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
521 * Returns datum representation for a 64-bit integer.
523 * Note: this routine returns a reference to palloc'd space.
526 extern Datum Int64GetDatum(int64 X);
530 * Returns 4-byte floating point value of a datum.
532 * Note: this macro hides the fact that float4 is currently a
533 * pass-by-reference type. Someday it may be pass-by-value.
536 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
540 * Returns datum representation for a 4-byte floating point number.
542 * Note: this routine returns a reference to palloc'd space.
545 extern Datum Float4GetDatum(float4 X);
549 * Returns 8-byte floating point value of a datum.
551 * Note: this macro hides the fact that float8 is currently a
552 * pass-by-reference type. Someday it may be pass-by-value,
553 * at least on some platforms.
556 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
560 * Returns datum representation for an 8-byte floating point number.
562 * Note: this routine returns a reference to palloc'd space.
565 extern Datum Float8GetDatum(float8 X);
570 * Returns 32-bit floating point value of a datum.
571 * This is really a pointer, of course.
573 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
574 * It will eventually go away.
577 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
581 * Returns datum representation for a 32-bit floating point number.
582 * This is really a pointer, of course.
584 * XXX: this macro is now deprecated in favor of Float4GetDatum.
585 * It will eventually go away.
588 #define Float32GetDatum(X) PointerGetDatum(X)
592 * Returns 64-bit floating point value of a datum.
593 * This is really a pointer, of course.
595 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
596 * It will eventually go away.
599 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
603 * Returns datum representation for a 64-bit floating point number.
604 * This is really a pointer, of course.
606 * XXX: this macro is now deprecated in favor of Float8GetDatum.
607 * It will eventually go away.
610 #define Float64GetDatum(X) PointerGetDatum(X)
617 * These macros are intended to allow writing code that does not depend on
618 * whether int64, float4, float8 are pass-by-reference types, while not
619 * sacrificing performance when they are. The argument must be a variable
620 * that will exist and have the same value for as long as the Datum is needed.
621 * In the pass-by-ref case, the address of the variable is taken to use as
622 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
626 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
627 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
628 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
631 /* ----------------------------------------------------------------
632 * Section 5: IsValid macros for system types
633 * ----------------------------------------------------------------
637 * True iff bool is valid.
639 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
643 * True iff pointer is valid.
645 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
649 * True iff pointer is properly aligned to point to the given type.
651 #define PointerIsAligned(pointer, type) \
652 (((long)(pointer) % (sizeof (type))) == 0)
654 /* ----------------------------------------------------------------
655 * Section 6: offsetof, lengthof, endof
656 * ----------------------------------------------------------------
660 * Offset of a structure/union field within that structure/union.
662 * XXX This is supposed to be part of stddef.h, but isn't on
663 * some systems (like SunOS 4).
666 #define offsetof(type, field) ((long) &((type *)0)->field)
667 #endif /* offsetof */
671 * Number of elements in an array.
673 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
677 * Address of the element one past the last in an array.
679 #define endof(array) (&array[lengthof(array)])
681 /* ----------------------------------------------------------------
682 * Section 7: exception handling definitions
683 * Assert, Trap, etc macros
684 * ----------------------------------------------------------------
687 * Exception Handling definitions
690 typedef char *ExcMessage;
691 typedef struct Exception
697 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
700 * It should _NOT_ be defined in releases or in benchmark copies
705 * Generates an exception if the given condition is true.
708 #define Trap(condition, exception) \
710 if ((assert_enabled) && (condition)) \
711 ExceptionalCondition(CppAsString(condition), &(exception), \
712 (char*)NULL, __FILE__, __LINE__); \
716 * TrapMacro is the same as Trap but it's intended for use in macros:
718 * #define foo(x) (AssertM(x != 0) && bar(x))
722 #define TrapMacro(condition, exception) \
723 ((bool) ((! assert_enabled) || ! (condition) || \
724 (ExceptionalCondition(CppAsString(condition), \
726 (char*) NULL, __FILE__, __LINE__))))
728 #ifndef USE_ASSERT_CHECKING
729 #define Assert(condition)
730 #define AssertMacro(condition) ((void)true)
731 #define AssertArg(condition)
732 #define AssertState(condition)
733 #define assert_enabled 0
735 #define Assert(condition) \
736 Trap(!(condition), FailedAssertion)
738 #define AssertMacro(condition) \
739 ((void) TrapMacro(!(condition), FailedAssertion))
741 #define AssertArg(condition) \
742 Trap(!(condition), BadArg)
744 #define AssertState(condition) \
745 Trap(!(condition), BadState)
747 extern bool assert_enabled;
749 #endif /* USE_ASSERT_CHECKING */
753 * Generates an exception with a message if the given condition is true.
756 #define LogTrap(condition, exception, printArgs) \
758 if ((assert_enabled) && (condition)) \
759 ExceptionalCondition(CppAsString(condition), &(exception), \
760 vararg_format printArgs, __FILE__, __LINE__); \
764 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
766 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
768 #define LogTrapMacro(condition, exception, printArgs) \
769 ((bool) ((! assert_enabled) || ! (condition) || \
770 (ExceptionalCondition(CppAsString(condition), \
772 vararg_format printArgs, __FILE__, __LINE__))))
774 #ifndef USE_ASSERT_CHECKING
775 #define LogAssert(condition, printArgs)
776 #define LogAssertMacro(condition, printArgs) true
777 #define LogAssertArg(condition, printArgs)
778 #define LogAssertState(condition, printArgs)
780 #define LogAssert(condition, printArgs) \
781 LogTrap(!(condition), FailedAssertion, printArgs)
783 #define LogAssertMacro(condition, printArgs) \
784 LogTrapMacro(!(condition), FailedAssertion, printArgs)
786 #define LogAssertArg(condition, printArgs) \
787 LogTrap(!(condition), BadArg, printArgs)
789 #define LogAssertState(condition, printArgs) \
790 LogTrap(!(condition), BadState, printArgs)
792 #ifdef ASSERT_CHECKING_TEST
793 extern int assertTest(int val);
796 #endif /* USE_ASSERT_CHECKING */
798 /* ----------------------------------------------------------------
799 * Section 8: Min, Max, Abs macros
800 * ----------------------------------------------------------------
804 * Return the maximum of two numbers.
806 #define Max(x, y) ((x) > (y) ? (x) : (y))
810 * Return the minimum of two numbers.
812 #define Min(x, y) ((x) < (y) ? (x) : (y))
816 * Return the absolute value of the argument.
818 #define Abs(x) ((x) >= 0 ? (x) : -(x))
822 * Like standard library function strncpy(), except that result string
823 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
824 * of the source string will be kept.
825 * Also, the macro returns no result (too hard to do that without
826 * evaluating the arguments multiple times, which seems worse).
828 * BTW: when you need to copy a non-null-terminated string (like a text
829 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
830 * might seem to work, but it fetches one byte more than there is in the
831 * text object. One fine day you'll have a SIGSEGV because there isn't
832 * another byte before the end of memory. Don't laugh, we've had real
833 * live bug reports from real live users over exactly this mistake.
834 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
836 #define StrNCpy(dst,src,len) \
839 char * _dst = (dst); \
844 strncpy(_dst, (src), _len); \
845 _dst[_len-1] = '\0'; \
850 /* Get a bit mask of the bits set in non-int32 aligned addresses */
851 #define INT_ALIGN_MASK (sizeof(int32) - 1)
855 * Exactly the same as standard library function memset(), but considerably
856 * faster for zeroing small word-aligned structures (such as parsetree nodes).
857 * This has to be a macro because the main point is to avoid function-call
860 * We got the 64 number by testing this against the stock memset() on
861 * BSD/OS 3.0. Larger values were slower. bjm 1997/09/11
863 * I think the crossover point could be a good deal higher for
864 * most platforms, actually. tgl 2000-03-19
866 #define MemSet(start, val, len) \
869 int32 * _start = (int32 *) (start); \
873 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
874 (_len & INT_ALIGN_MASK) == 0 && \
876 _len <= MEMSET_LOOP_LIMIT) \
878 int32 * _stop = (int32 *) ((char *) _start + _len); \
879 while (_start < _stop) \
883 memset((char *) _start, _val, _len); \
886 #define MEMSET_LOOP_LIMIT 64
889 /* ----------------------------------------------------------------
891 * ----------------------------------------------------------------
894 extern Exception FailedAssertion;
895 extern Exception BadArg;
896 extern Exception BadState;
898 /* in utils/error/assert.c */
899 extern int ExceptionalCondition(char *conditionName,
900 Exception *exceptionP, char *details,
901 char *fileName, int lineNumber);
905 * vararg_format is used by assert and the exception handling stuff
908 extern char *vararg_format(const char *fmt,...);
912 /* ----------------------------------------------------------------
913 * Section 10: berkeley-specific configuration
915 * this section contains settings which are only relevant to the UC Berkeley
916 * sites. Other sites can ignore this
917 * ----------------------------------------------------------------
923 * These are experimental and are not supported in the code that
924 * we distribute to other sites.
928 #define STABLE_MEMORY_STORAGE
933 /* ----------------------------------------------------------------
934 * Section 11: system-specific hacks
936 * This should be limited to things that absolutely have to be
937 * included in every source file. The changes should be factored
938 * into a separate file so that changes to one port don't require
939 * changes to c.h (and everyone recompiling their whole system).
940 * ----------------------------------------------------------------
944 #define PG_BINARY O_BINARY
945 #define PG_BINARY_R "rb"
946 #define PG_BINARY_W "wb"
949 #define PG_BINARY_R "r"
950 #define PG_BINARY_W "w"
953 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
954 #define memmove(d, s, l) bcopy(s, d, l)
959 /* These are for things that are one way on Unix and another on NT */
960 #define NULL_DEV "/dev/null"
963 /* defines for dynamic linking on Win32 platform */
965 #if __GNUC__ && ! defined (__declspec)
966 #error You need egcs 1.1 or newer for compiling!
969 #define DLLIMPORT __declspec (dllexport)
970 #else /* not BUILDING_DLL */
971 #define DLLIMPORT __declspec (dllimport)
973 #else /* not CYGWIN */
977 /* Provide prototypes for routines not present in a particular machine's
978 * standard C library. It'd be better to put these in config.h, but
979 * in config.h we haven't yet included anything that defines size_t...
982 #ifndef HAVE_SNPRINTF_DECL
983 extern int snprintf(char *str, size_t count, const char *fmt,...);
987 #ifndef HAVE_VSNPRINTF_DECL
988 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
993 #include <regex/utils.h>