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.68 2000/04/12 05:29:10 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.
62 /* ----------------------------------------------------------------
63 * Section 1: bool, true, false, TRUE, FALSE, NULL
64 * ----------------------------------------------------------------
68 * Boolean value, either true or false.
75 #endif /* ndef bool */
78 #define true ((bool) 1)
81 #define false ((bool) 0)
83 typedef bool *BoolPtr;
98 #define NULL ((void *) 0)
99 #endif /* !defined(NULL) */
101 /* ----------------------------------------------------------------
102 * Section 2: non-ansi C definitions:
104 * type prefixes: const, signed, volatile, inline
106 * ----------------------------------------------------------------
111 * Convert the argument to a string, using the C preprocessor.
113 * Concatenate two arguments together, using the C preprocessor.
115 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
116 * whether #identifier works, but if we have that we likely have ## too.
118 #if defined(HAVE_STRINGIZE)
120 #define CppAsString(identifier) #identifier
121 #define CppConcat(x, y) x##y
123 #else /* !HAVE_STRINGIZE */
125 #define CppAsString(identifier) "identifier"
128 * CppIdentity -- On Reiser based cpp's this is used to concatenate
129 * two tokens. That is
130 * CppIdentity(A)B ==> AB
131 * We renamed it to _private_CppIdentity because it should not
132 * be referenced outside this file. On other cpp's it
135 #define _priv_CppIdentity(x)x
136 #define CppConcat(x, y) _priv_CppIdentity(x)y
138 #endif /* !HAVE_STRINGIZE */
141 * dummyret is used to set return values in macros that use ?: to make
142 * assignments. gcc wants these to be void, other compilers like char
144 #ifdef __GNUC__ /* GNU cc */
145 #define dummyret void
147 #define dummyret char
150 /* ----------------------------------------------------------------
151 * Section 3: standard system types
152 * ----------------------------------------------------------------
157 * Variable holding address of any memory resident object.
159 * XXX Pointer arithmetic is done with this, so it can't be void *
160 * under "true" ANSI compilers.
162 typedef char *Pointer;
166 * Signed integer, EXACTLY N BITS IN SIZE,
167 * used for numerical computations and the
168 * frontend/backend protocol.
170 typedef signed char int8; /* == 8 bits */
171 typedef signed short int16; /* == 16 bits */
172 typedef signed int int32; /* == 32 bits */
176 * Unsigned integer, EXACTLY N BITS IN SIZE,
177 * used for numerical computations and the
178 * frontend/backend protocol.
180 typedef unsigned char uint8; /* == 8 bits */
181 typedef unsigned short uint16; /* == 16 bits */
182 typedef unsigned int uint32; /* == 32 bits */
186 * Floating point number, AT LEAST N BITS IN SIZE,
187 * used for numerical computations.
189 * Since sizeof(floatN) may be > sizeof(char *), always pass
190 * floatN by reference.
192 typedef float float32data;
193 typedef double float64data;
194 typedef float *float32;
195 typedef double *float64;
199 * Boolean value, AT LEAST N BITS IN SIZE.
201 typedef uint8 bool8; /* >= 8 bits */
202 typedef uint16 bool16; /* >= 16 bits */
203 typedef uint32 bool32; /* >= 32 bits */
207 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
209 typedef uint8 bits8; /* >= 8 bits */
210 typedef uint16 bits16; /* >= 16 bits */
211 typedef uint32 bits32; /* >= 32 bits */
215 * Unit of storage, AT LEAST N BITS IN SIZE,
216 * used to fetch/store data.
218 typedef uint8 word8; /* >= 8 bits */
219 typedef uint16 word16; /* >= 16 bits */
220 typedef uint32 word32; /* >= 32 bits */
224 * Size of any memory resident object, as returned by sizeof.
230 * Index into any memory resident array.
233 * Indices are non negative.
235 typedef unsigned int Index;
245 * Offset into any memory resident array.
248 * This differs from an Index in that an Index is always
249 * non negative, whereas Offset may be negative.
251 typedef signed int Offset;
253 /* ----------------------------------------------------------------
254 * Section 4: datum type + support macros
255 * ----------------------------------------------------------------
259 * POSTGRES abstract data type datum representation definitions.
264 * Postgres makes the following assumption about machines:
266 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
268 * Postgres also assumes that
276 * If your machine meets these requirements, Datums should also be checked
277 * to see if the positioning is correct.
280 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
281 typedef Datum *DatumPtr;
283 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
284 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
285 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
286 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
287 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
288 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
292 * Returns character value of a datum.
295 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
299 * Returns datum representation for a character.
302 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
306 * Returns datum representation for an 8-bit integer.
309 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
313 * Returns 8-bit unsigned integer value of a datum.
316 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
320 * Returns datum representation for an 8-bit unsigned integer.
323 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
327 * Returns 16-bit integer value of a datum.
330 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
334 * Returns datum representation for a 16-bit integer.
337 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
341 * Returns 16-bit unsigned integer value of a datum.
344 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
348 * Returns datum representation for a 16-bit unsigned integer.
351 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
355 * Returns 32-bit integer value of a datum.
358 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
362 * Returns datum representation for a 32-bit integer.
365 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
369 * Returns 32-bit unsigned integer value of a datum.
372 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
376 * Returns datum representation for a 32-bit unsigned integer.
379 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
383 * Returns object identifier value of a datum.
386 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
390 * Returns datum representation for an object identifier.
393 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
397 * Returns pointer value of a datum.
400 #define DatumGetPointer(X) ((Pointer) (X))
404 * Returns datum representation for a pointer.
407 #define PointerGetDatum(X) ((Datum) (X))
411 * Returns name value of a datum.
414 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) (X)))
418 * Returns datum representation for a name.
421 #define NameGetDatum(X) PointerGetDatum((Pointer) (X))
426 * Returns 32-bit floating point value of a datum.
427 * This is really a pointer, of course.
430 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
434 * Returns datum representation for a 32-bit floating point number.
435 * This is really a pointer, of course.
438 #define Float32GetDatum(X) PointerGetDatum((Pointer) (X))
442 * Returns 64-bit floating point value of a datum.
443 * This is really a pointer, of course.
446 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
450 * Returns datum representation for a 64-bit floating point number.
451 * This is really a pointer, of course.
454 #define Float64GetDatum(X) PointerGetDatum((Pointer) (X))
456 /* ----------------------------------------------------------------
457 * Section 5: IsValid macros for system types
458 * ----------------------------------------------------------------
462 * True iff bool is valid.
464 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
468 * True iff pointer is valid.
470 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
474 * True iff pointer is properly aligned to point to the given type.
476 #define PointerIsAligned(pointer, type) \
477 (((long)(pointer) % (sizeof (type))) == 0)
479 /* ----------------------------------------------------------------
480 * Section 6: offsetof, lengthof, endof
481 * ----------------------------------------------------------------
485 * Offset of a structure/union field within that structure/union.
487 * XXX This is supposed to be part of stddef.h, but isn't on
488 * some systems (like SunOS 4).
491 #define offsetof(type, field) ((long) &((type *)0)->field)
492 #endif /* offsetof */
496 * Number of elements in an array.
498 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
502 * Address of the element one past the last in an array.
504 #define endof(array) (&array[lengthof(array)])
506 /* ----------------------------------------------------------------
507 * Section 7: exception handling definitions
508 * Assert, Trap, etc macros
509 * ----------------------------------------------------------------
512 * Exception Handling definitions
515 typedef char *ExcMessage;
516 typedef struct Exception
522 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
525 * It should _NOT_ be defined in releases or in benchmark copies
530 * Generates an exception if the given condition is true.
533 #define Trap(condition, exception) \
534 { if ((assert_enabled) && (condition)) \
535 ExceptionalCondition(CppAsString(condition), &(exception), \
536 (char*)NULL, __FILE__, __LINE__); }
539 * TrapMacro is the same as Trap but it's intended for use in macros:
541 * #define foo(x) (AssertM(x != 0) && bar(x))
545 #define TrapMacro(condition, exception) \
546 ((bool) ((! assert_enabled) || ! (condition) || \
547 (ExceptionalCondition(CppAsString(condition), \
549 (char*) NULL, __FILE__, __LINE__))))
551 #ifndef USE_ASSERT_CHECKING
552 #define Assert(condition)
553 #define AssertMacro(condition) ((void)true)
554 #define AssertArg(condition)
555 #define AssertState(condition)
556 #define assert_enabled 0
558 #define Assert(condition) \
559 Trap(!(condition), FailedAssertion)
561 #define AssertMacro(condition) \
562 ((void) TrapMacro(!(condition), FailedAssertion))
564 #define AssertArg(condition) \
565 Trap(!(condition), BadArg)
567 #define AssertState(condition) \
568 Trap(!(condition), BadState)
570 extern int assert_enabled;
572 #endif /* USE_ASSERT_CHECKING */
576 * Generates an exception with a message if the given condition is true.
579 #define LogTrap(condition, exception, printArgs) \
580 { if ((assert_enabled) && (condition)) \
581 ExceptionalCondition(CppAsString(condition), &(exception), \
582 vararg_format printArgs, __FILE__, __LINE__); }
585 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
587 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
589 #define LogTrapMacro(condition, exception, printArgs) \
590 ((bool) ((! assert_enabled) || ! (condition) || \
591 (ExceptionalCondition(CppAsString(condition), \
593 vararg_format printArgs, __FILE__, __LINE__))))
595 #ifndef USE_ASSERT_CHECKING
596 #define LogAssert(condition, printArgs)
597 #define LogAssertMacro(condition, printArgs) true
598 #define LogAssertArg(condition, printArgs)
599 #define LogAssertState(condition, printArgs)
601 #define LogAssert(condition, printArgs) \
602 LogTrap(!(condition), FailedAssertion, printArgs)
604 #define LogAssertMacro(condition, printArgs) \
605 LogTrapMacro(!(condition), FailedAssertion, printArgs)
607 #define LogAssertArg(condition, printArgs) \
608 LogTrap(!(condition), BadArg, printArgs)
610 #define LogAssertState(condition, printArgs) \
611 LogTrap(!(condition), BadState, printArgs)
613 extern int assertEnable(int val);
615 #ifdef ASSERT_CHECKING_TEST
616 extern int assertTest(int val);
619 #endif /* USE_ASSERT_CHECKING */
621 /* ----------------------------------------------------------------
622 * Section 8: Min, Max, Abs macros
623 * ----------------------------------------------------------------
627 * Return the maximum of two numbers.
629 #define Max(x, y) ((x) > (y) ? (x) : (y))
633 * Return the minimum of two numbers.
635 #define Min(x, y) ((x) < (y) ? (x) : (y))
639 * Return the absolute value of the argument.
641 #define Abs(x) ((x) >= 0 ? (x) : -(x))
645 * Like standard library function strncpy(), except that result string
646 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
647 * of the source string will be kept.
648 * Also, the macro returns no result (too hard to do that without
649 * evaluating the arguments multiple times, which seems worse).
651 #define StrNCpy(dst,src,len) \
654 char * _dst = (dst); \
659 strncpy(_dst, (src), _len); \
660 _dst[_len-1] = '\0'; \
665 /* Get a bit mask of the bits set in non-int32 aligned addresses */
666 #define INT_ALIGN_MASK (sizeof(int32) - 1)
670 * Exactly the same as standard library function memset(), but considerably
671 * faster for zeroing small word-aligned structures (such as parsetree nodes).
672 * This has to be a macro because the main point is to avoid function-call
675 * We got the 64 number by testing this against the stock memset() on
676 * BSD/OS 3.0. Larger values were slower. bjm 1997/09/11
678 * I think the crossover point could be a good deal higher for
679 * most platforms, actually. tgl 2000-03-19
681 #define MemSet(start, val, len) \
684 int32 * _start = (int32 *) (start); \
688 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
689 (_len & INT_ALIGN_MASK) == 0 && \
691 _len <= MEMSET_LOOP_LIMIT) \
693 int32 * _stop = (int32 *) ((char *) _start + _len); \
694 while (_start < _stop) \
698 memset((char *) _start, _val, _len); \
701 #define MEMSET_LOOP_LIMIT 64
704 /* ----------------------------------------------------------------
706 * ----------------------------------------------------------------
709 extern Exception FailedAssertion;
710 extern Exception BadArg;
711 extern Exception BadState;
713 /* in utils/error/assert.c */
714 extern int ExceptionalCondition(char *conditionName,
715 Exception *exceptionP, char *details,
716 char *fileName, int lineNumber);
720 * vararg_format is used by assert and the exception handling stuff
723 extern char *vararg_format(const char *fmt,...);
727 /* ----------------------------------------------------------------
728 * Section 10: berkeley-specific configuration
730 * this section contains settings which are only relevant to the UC Berkeley
731 * sites. Other sites can ignore this
732 * ----------------------------------------------------------------
738 * These are experimental and are not supported in the code that
739 * we distribute to other sites.
743 #define STABLE_MEMORY_STORAGE
748 /* ----------------------------------------------------------------
749 * Section 11: system-specific hacks
751 * This should be limited to things that absolutely have to be
752 * included in every source file. The changes should be factored
753 * into a separate file so that changes to one port don't require
754 * changes to c.h (and everyone recompiling their whole system).
755 * ----------------------------------------------------------------
760 #include "port/hpux/fixade.h" /* for unaligned access fixup */
764 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
765 #define memmove(d, s, l) bcopy(s, d, l)
770 /* These are for things that are one way on Unix and another on NT */
771 #define NULL_DEV "/dev/null"
774 /* defines for dynamic linking on Win32 platform */
776 #if __GNUC__ && ! defined (__declspec)
777 #error You need egcs 1.1 or newer for compiling!
780 #define DLLIMPORT __declspec (dllexport)
781 #else /* not BUILDING_DLL */
782 #define DLLIMPORT __declspec (dllimport)
784 #else /* not CYGWIN */
788 /* Provide prototypes for routines not present in a particular machine's
789 * standard C library. It'd be better to put these in config.h, but
790 * in config.h we haven't yet included anything that defines size_t...
793 #ifndef HAVE_SNPRINTF_DECL
794 extern int snprintf(char *str, size_t count, const char *fmt,...);
797 #ifndef HAVE_VSNPRINTF_DECL
798 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
802 #include <regex/utils.h>