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.64 2000/01/26 05:57:46 momjian Exp $
13 *-------------------------------------------------------------------------
18 * When adding stuff to this file, please try and 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
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.
66 /* ----------------------------------------------------------------
67 * Section 1: bool, true, false, TRUE, FALSE, NULL
68 * ----------------------------------------------------------------
72 * Boolean value, either true or false.
79 #endif /* ndef bool */
82 #define true ((bool) 1)
85 #define false ((bool) 0)
87 typedef bool *BoolPtr;
102 #define NULL ((void *) 0)
103 #endif /* !defined(NULL) */
105 /* ----------------------------------------------------------------
106 * Section 2: non-ansi C definitions:
108 * type prefixes: const, signed, volatile, inline
110 * ----------------------------------------------------------------
115 * Convert the argument to a string, using the C preprocessor.
117 * Concatenate two arguments together, using the C preprocessor.
119 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
120 * whether #identifier works, but if we have that we likely have ## too.
122 #if defined(HAVE_STRINGIZE)
124 #define CppAsString(identifier) #identifier
125 #define CppConcat(x, y) x##y
127 #else /* !HAVE_STRINGIZE */
129 #define CppAsString(identifier) "identifier"
132 * CppIdentity -- On Reiser based cpp's this is used to concatenate
133 * two tokens. That is
134 * CppIdentity(A)B ==> AB
135 * We renamed it to _private_CppIdentity because it should not
136 * be referenced outside this file. On other cpp's it
139 #define _priv_CppIdentity(x)x
140 #define CppConcat(x, y) _priv_CppIdentity(x)y
142 #endif /* !HAVE_STRINGIZE */
145 * dummyret is used to set return values in macros that use ?: to make
146 * assignments. gcc wants these to be void, other compilers like char
148 #ifdef __GNUC__ /* GNU cc */
149 #define dummyret void
151 #define dummyret char
154 /* ----------------------------------------------------------------
155 * Section 3: standard system types
156 * ----------------------------------------------------------------
161 * Variable holding address of any memory resident object.
163 * XXX Pointer arithmetic is done with this, so it can't be void *
164 * under "true" ANSI compilers.
166 typedef char *Pointer;
170 * Signed integer, EXACTLY N BITS IN SIZE,
171 * used for numerical computations and the
172 * frontend/backend protocol.
174 typedef signed char int8; /* == 8 bits */
175 typedef signed short int16; /* == 16 bits */
176 typedef signed int int32; /* == 32 bits */
180 * Unsigned integer, EXACTLY N BITS IN SIZE,
181 * used for numerical computations and the
182 * frontend/backend protocol.
184 typedef unsigned char uint8; /* == 8 bits */
185 typedef unsigned short uint16; /* == 16 bits */
186 typedef unsigned int uint32; /* == 32 bits */
190 * Floating point number, AT LEAST N BITS IN SIZE,
191 * used for numerical computations.
193 * Since sizeof(floatN) may be > sizeof(char *), always pass
194 * floatN by reference.
196 typedef float float32data;
197 typedef double float64data;
198 typedef float *float32;
199 typedef double *float64;
203 * Boolean value, AT LEAST N BITS IN SIZE.
205 typedef uint8 bool8; /* >= 8 bits */
206 typedef uint16 bool16; /* >= 16 bits */
207 typedef uint32 bool32; /* >= 32 bits */
211 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
213 typedef uint8 bits8; /* >= 8 bits */
214 typedef uint16 bits16; /* >= 16 bits */
215 typedef uint32 bits32; /* >= 32 bits */
219 * Unit of storage, AT LEAST N BITS IN SIZE,
220 * used to fetch/store data.
222 typedef uint8 word8; /* >= 8 bits */
223 typedef uint16 word16; /* >= 16 bits */
224 typedef uint32 word32; /* >= 32 bits */
228 * Size of any memory resident object, as returned by sizeof.
234 * Index into any memory resident array.
237 * Indices are non negative.
239 typedef unsigned int Index;
249 * Offset into any memory resident array.
252 * This differs from an Index in that an Index is always
253 * non negative, whereas Offset may be negative.
255 typedef signed int Offset;
257 /* ----------------------------------------------------------------
258 * Section 4: datum type + support macros
259 * ----------------------------------------------------------------
263 * POSTGRES abstract data type datum representation definitions.
268 * Postgres makes the following assumption about machines:
270 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
272 * Postgres also assumes that
280 * If your machine meets these requirements, Datums should also be checked
281 * to see if the positioning is correct.
283 * This file is MACHINE AND COMPILER dependent!!!
286 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
287 typedef Datum *DatumPtr;
289 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
290 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
291 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
292 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
293 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
294 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
298 * Returns character value of a datum.
301 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
305 * Returns datum representation for a character.
308 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
312 * Returns datum representation for an 8-bit integer.
315 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
319 * Returns 8-bit unsigned integer value of a datum.
322 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
326 * Returns datum representation for an 8-bit unsigned integer.
329 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
333 * Returns 16-bit integer value of a datum.
336 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
340 * Returns datum representation for a 16-bit integer.
343 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
347 * Returns 16-bit unsigned integer value of a datum.
350 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
354 * Returns datum representation for a 16-bit unsigned integer.
357 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
361 * Returns 32-bit integer value of a datum.
364 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
368 * Returns datum representation for a 32-bit integer.
371 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
375 * Returns 32-bit unsigned integer value of a datum.
378 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
382 * Returns datum representation for a 32-bit unsigned integer.
385 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
389 * Returns object identifier value of a datum.
392 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
396 * Returns datum representation for an object identifier.
399 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
403 * Returns pointer value of a datum.
406 #define DatumGetPointer(X) ((Pointer) X)
410 * Returns datum representation for a pointer.
413 #define PointerGetDatum(X) ((Datum) X)
417 * Returns name value of a datum.
420 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
424 * Returns datum representation for a name.
427 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
432 * Returns 32-bit floating point value of a datum.
433 * This is really a pointer, of course.
436 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
440 * Returns datum representation for a 32-bit floating point number.
441 * This is really a pointer, of course.
444 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
448 * Returns 64-bit floating point value of a datum.
449 * This is really a pointer, of course.
452 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
456 * Returns datum representation for a 64-bit floating point number.
457 * This is really a pointer, of course.
460 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
462 /* ----------------------------------------------------------------
463 * Section 5: IsValid macros for system types
464 * ----------------------------------------------------------------
468 * True iff bool is valid.
470 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
474 * True iff pointer is valid.
476 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
480 * True iff pointer is within given bounds.
483 * Assumes the bounded interval to be [min,max),
484 * i.e. closed on the left and open on the right.
486 #define PointerIsInBounds(pointer, min, max) \
487 ((min) <= (pointer) && (pointer) < (max))
491 * True iff pointer is properly aligned to point to the given type.
493 #define PointerIsAligned(pointer, type) \
494 (((long)(pointer) % (sizeof (type))) == 0)
496 /* ----------------------------------------------------------------
497 * Section 6: offsetof, lengthof, endof
498 * ----------------------------------------------------------------
502 * Offset of a structure/union field within that structure/union.
504 * XXX This is supposed to be part of stddef.h, but isn't on
505 * some systems (like SunOS 4).
508 #define offsetof(type, field) ((long) &((type *)0)->field)
509 #endif /* offsetof */
513 * Number of elements in an array.
515 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
519 * Address of the element one past the last in an array.
521 #define endof(array) (&array[lengthof(array)])
523 /* ----------------------------------------------------------------
524 * Section 7: exception handling definitions
525 * Assert, Trap, etc macros
526 * ----------------------------------------------------------------
529 * Exception Handling definitions
532 typedef char *ExcMessage;
533 typedef struct Exception
539 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
542 * It should _NOT_ be defined in releases or in benchmark copies
547 * Generates an exception if the given condition is true.
550 #define Trap(condition, exception) \
551 { if ((assert_enabled) && (condition)) \
552 ExceptionalCondition(CppAsString(condition), &(exception), \
553 (char*)NULL, __FILE__, __LINE__); }
556 * TrapMacro is the same as Trap but it's intended for use in macros:
558 * #define foo(x) (AssertM(x != 0) && bar(x))
562 #define TrapMacro(condition, exception) \
563 ((bool) ((! assert_enabled) || (! condition) || \
564 (ExceptionalCondition(CppAsString(condition), \
566 (char*) NULL, __FILE__, __LINE__))))
568 #ifndef USE_ASSERT_CHECKING
569 #define Assert(condition)
570 #define AssertMacro(condition) (void)true
571 #define AssertArg(condition)
572 #define AssertState(condition)
573 #define assert_enabled 0
575 #define Assert(condition) \
576 Trap(!(condition), FailedAssertion)
578 #define AssertMacro(condition) \
579 (void)TrapMacro(!(condition), FailedAssertion)
581 #define AssertArg(condition) \
582 Trap(!(condition), BadArg)
584 #define AssertState(condition) \
585 Trap(!(condition), BadState)
587 extern int assert_enabled;
589 #endif /* USE_ASSERT_CHECKING */
593 * Generates an exception with a message if the given condition is true.
596 #define LogTrap(condition, exception, printArgs) \
597 { if ((assert_enabled) && (condition)) \
598 ExceptionalCondition(CppAsString(condition), &(exception), \
599 vararg_format printArgs, __FILE__, __LINE__); }
602 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
604 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
606 #define LogTrapMacro(condition, exception, printArgs) \
607 ((bool) ((! assert_enabled) || (! condition) || \
608 (ExceptionalCondition(CppAsString(condition), \
610 vararg_format printArgs, __FILE__, __LINE__))))
612 #ifndef USE_ASSERT_CHECKING
613 #define LogAssert(condition, printArgs)
614 #define LogAssertMacro(condition, printArgs) true
615 #define LogAssertArg(condition, printArgs)
616 #define LogAssertState(condition, printArgs)
618 #define LogAssert(condition, printArgs) \
619 LogTrap(!(condition), FailedAssertion, printArgs)
621 #define LogAssertMacro(condition, printArgs) \
622 LogTrapMacro(!(condition), FailedAssertion, printArgs)
624 #define LogAssertArg(condition, printArgs) \
625 LogTrap(!(condition), BadArg, printArgs)
627 #define LogAssertState(condition, printArgs) \
628 LogTrap(!(condition), BadState, printArgs)
630 extern int assertEnable(int val);
632 #ifdef ASSERT_CHECKING_TEST
633 extern int assertTest(int val);
636 #endif /* USE_ASSERT_CHECKING */
638 /* ----------------------------------------------------------------
639 * Section 8: Min, Max, Abs macros
640 * ----------------------------------------------------------------
644 * Return the maximum of two numbers.
646 #define Max(x, y) ((x) > (y) ? (x) : (y))
650 * Return the minimum of two numbers.
652 #define Min(x, y) ((x) < (y) ? (x) : (y))
656 * Return the absolute value of the argument.
658 #define Abs(x) ((x) >= 0 ? (x) : -(x))
662 * Does string copy, and forces terminating NULL
664 /* we do this so if the macro is used in an if action, it will work */
665 #define StrNCpy(dst,src,len) \
669 strncpy((dst),(src),(len)-1), \
670 *((dst)+(len)-1)='\0' \
673 (dummyret)NULL,(void)(dst) \
676 /* Get a bit mask of the bits set in non-int32 aligned addresses */
677 #define INT_ALIGN_MASK (sizeof(int32) - 1)
680 * This function gets call too often, so we inline it if we can.
681 * Are we aligned for int32?
682 * We have to cast the pointer to int so we can do the AND
683 * We got the 64 number by testing this against the stock memset() on
684 * BSD/OS 3.0. Larger values were slower.
686 #define MemSet(start, val, len) do \
688 if (((long)(start) & INT_ALIGN_MASK) == 0 && \
689 ((len) & INT_ALIGN_MASK) == 0 && \
693 int32 *_i = (int32 *)(start); \
694 int32 *_stop = (int32 *)((char *)(start) + (len)); \
700 memset((start), (val), (len)); \
703 /* ----------------------------------------------------------------
705 * ----------------------------------------------------------------
708 extern Exception FailedAssertion;
709 extern Exception BadArg;
710 extern Exception BadState;
712 /* in utils/error/assert.c */
713 extern int ExceptionalCondition(char *conditionName,
714 Exception *exceptionP, char *details,
715 char *fileName, int lineNumber);
719 * vararg_format is used by assert and the exception handling stuff
722 extern char *vararg_format(const char *fmt,...);
726 /* ----------------------------------------------------------------
727 * Section 10: berkeley-specific configuration
729 * this section contains settings which are only relevant to the UC Berkeley
730 * sites. Other sites can ignore this
731 * ----------------------------------------------------------------
737 * These are experimental and are not supported in the code that
738 * we distribute to other sites.
742 #define STABLE_MEMORY_STORAGE
747 /* ----------------------------------------------------------------
748 * Section 11: system-specific hacks
750 * This should be limited to things that absolutely have to be
751 * included in every source file. The changes should be factored
752 * into a separate file so that changes to one port don't require
753 * changes to c.h (and everyone recompiling their whole system).
754 * ----------------------------------------------------------------
759 #include "port/hpux/fixade.h" /* for unaligned access fixup */
763 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
764 #define memmove(d, s, l) bcopy(s, d, l)
769 /* These are for things that are one way on Unix and another on NT */
770 #define NULL_DEV "/dev/null"
773 /* defines for dynamic linking on Win32 platform */
775 #if __GNUC__ && ! defined (__declspec)
776 #error You need egcs 1.1 or newer for compiling!
779 #define DLLIMPORT __declspec (dllexport)
780 #else /* not BUILDING_DLL */
781 #define DLLIMPORT __declspec (dllimport)
783 #else /* not CYGWIN */
787 /* Provide prototypes for routines not present in a particular machine's
788 * standard C library. It'd be better to put these in config.h, but
789 * in config.h we haven't yet included anything that defines size_t...
792 #ifndef HAVE_SNPRINTF_DECL
793 extern int snprintf(char *str, size_t count, const char *fmt,...);
796 #ifndef HAVE_VSNPRINTF_DECL
797 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
801 #include <regex/utils.h>