1 /*-------------------------------------------------------------------------
4 * Fundamental C definitions. This is included by every .c file in
8 * Copyright (c) 1994, Regents of the University of California
10 * $Id: c.h,v 1.29 1998/01/26 01:41:49 scrappy Exp $
12 *-------------------------------------------------------------------------
17 * When adding stuff to this file, please try and put stuff
18 * into the relevant section, or add new sections as appropriate.
21 * ------- ------------------------------------------------
22 * 1) bool, true, false, TRUE, FALSE
23 * 2) __STDC__, non-ansi C definitions:
24 * Pointer typedef, NULL
26 * 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
52 /* ----------------------------------------------------------------
53 * Section 1: bool, true, false, TRUE, FALSE
54 * ----------------------------------------------------------------
58 * Boolean value, either true or false.
61 #define false ((char) 0)
62 #define true ((char) 1)
67 typedef bool *BoolPtr;
77 /* ----------------------------------------------------------------
78 * Section 2: __STDC__, non-ansi C definitions:
81 * Pointer typedef, NULL
82 * type prefixes: const, signed, volatile, inline
83 * ----------------------------------------------------------------
86 #ifdef __STDC__ /* ANSI C */
90 * Variable holding address of any memory resident object.
94 * XXX Pointer arithmetic is done with this, so it can't be void *
95 * under "true" ANSI compilers.
97 typedef char *Pointer;
104 #define NULL ((void *) 0)
105 #endif /* !defined(NULL) */
107 #define HAVE_ANSI_CPP /* all ANSI C compilers must have this! */
108 #if defined(NEED_STD_HDRS)
109 #undef NEED_STD_HDRS /* all ANSI systems must have
111 #endif /* NEED_STD_HDRS */
113 #else /* !defined(__STDC__) *//* NOT ANSI C */
117 * Variable containing address of any memory resident object.
119 typedef char *Pointer;
127 #endif /* !defined(NULL) */
131 * Type modifier. Identifies read only variables.
134 * extern const Version RomVersion;
136 #define const /* const */
140 * Type modifier. Identifies signed integral types.
142 #define signed /* signed */
146 * Type modifier. Identifies variables which may change in ways not
147 * noticeable by the compiler, e.g. via asynchronous interrupts.
150 * extern volatile unsigned int NumberOfInterrupts;
152 #define volatile /* volatile */
154 #endif /* !defined(__STDC__) *//* NOT ANSI C */
158 * Convert the argument to a string, using the C preprocessor.
160 * Concatenate two arguments together, using the C preprocessor.
162 #if defined(HAVE_ANSI_CPP)
164 #define CppAsString(identifier) #identifier
165 #define CppConcat(x, y) x##y
167 #else /* !HAVE_ANSI_CPP */
169 #define CppAsString(identifier) "identifier"
172 * CppIdentity -- On Reiser based cpp's this is used to concatenate
173 * two tokens. That is
174 * CppIdentity(A)B ==> AB
175 * We renamed it to _private_CppIdentity because it should not
176 * be referenced outside this file. On other cpp's it
179 #define _priv_CppIdentity(x)x
180 #define CppConcat(x, y) _priv_CppIdentity(x)y
182 #endif /* !HAVE_ANSI_CPP */
184 #ifndef __GNUC__ /* GNU cc */
188 #if defined(NEED_STD_HDRS)
190 * You're doomed. We've removed almost all of our own C library
191 * extern declarations because they conflict on the different
192 * systems. You'll have to write your own stdlib.h.
195 #else /* NEED_STD_HDRS */
198 #endif /* NEED_STD_HDRS */
200 /* ----------------------------------------------------------------
201 * Section 3: standard system types
202 * ----------------------------------------------------------------
207 * Signed integer, EXACTLY N BITS IN SIZE,
208 * used for numerical computations and the
209 * frontend/backend protocol.
211 typedef signed char int8; /* == 8 bits */
212 typedef signed short int16; /* == 16 bits */
213 typedef signed int int32; /* == 32 bits */
217 * Unsigned integer, EXACTLY N BITS IN SIZE,
218 * used for numerical computations and the
219 * frontend/backend protocol.
221 typedef unsigned char uint8; /* == 8 bits */
222 typedef unsigned short uint16; /* == 16 bits */
223 typedef unsigned int uint32; /* == 32 bits */
227 * Floating point number, AT LEAST N BITS IN SIZE,
228 * used for numerical computations.
230 * Since sizeof(floatN) may be > sizeof(char *), always pass
231 * floatN by reference.
233 typedef float float32data;
234 typedef double float64data;
235 typedef float *float32;
236 typedef double *float64;
240 * Boolean value, AT LEAST N BITS IN SIZE.
242 typedef uint8 bool8; /* >= 8 bits */
243 typedef uint16 bool16; /* >= 16 bits */
244 typedef uint32 bool32; /* >= 32 bits */
248 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
250 typedef uint8 bits8; /* >= 8 bits */
251 typedef uint16 bits16; /* >= 16 bits */
252 typedef uint32 bits32; /* >= 32 bits */
256 * Unit of storage, AT LEAST N BITS IN SIZE,
257 * used to fetch/store data.
259 typedef uint8 word8; /* >= 8 bits */
260 typedef uint16 word16; /* >= 16 bits */
261 typedef uint32 word32; /* >= 32 bits */
265 * Size of any memory resident object, as returned by sizeof.
267 typedef unsigned int Size;
271 * Index into any memory resident array.
274 * Indices are non negative.
276 typedef unsigned int Index;
286 * Offset into any memory resident array.
289 * This differs from an Index in that an Index is always
290 * non negative, whereas Offset may be negative.
292 typedef signed int Offset;
294 /* ----------------------------------------------------------------
295 * Section 4: datum type + support macros
296 * ----------------------------------------------------------------
300 * POSTGRES abstract data type datum representation definitions.
305 * Postgres makes the following assumption about machines:
307 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
309 * Postgres also assumes that
317 * If your machine meets these requirements, Datums should also be checked
318 * to see if the positioning is correct.
320 * This file is MACHINE AND COMPILER dependent!!!
323 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
324 typedef Datum *DatumPtr;
326 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
327 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
328 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
329 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
330 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
331 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
335 * Returns character value of a datum.
338 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
342 * Returns datum representation for a character.
345 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
349 * Returns datum representation for an 8-bit integer.
352 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
356 * Returns 8-bit unsigned integer value of a datum.
359 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
363 * Returns datum representation for an 8-bit unsigned integer.
366 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
370 * Returns 16-bit integer value of a datum.
373 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
377 * Returns datum representation for a 16-bit integer.
380 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
384 * Returns 16-bit unsigned integer value of a datum.
387 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
391 * Returns datum representation for a 16-bit unsigned integer.
394 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
398 * Returns 32-bit integer value of a datum.
401 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
405 * Returns datum representation for a 32-bit integer.
408 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
412 * Returns 32-bit unsigned integer value of a datum.
415 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
419 * Returns datum representation for a 32-bit unsigned integer.
422 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
425 * DatumGetObjectId --
426 * Returns object identifier value of a datum.
429 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
432 * ObjectIdGetDatum --
433 * Returns datum representation for an object identifier.
436 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
440 * Returns pointer value of a datum.
443 #define DatumGetPointer(X) ((Pointer) X)
447 * Returns datum representation for a pointer.
450 #define PointerGetDatum(X) ((Datum) X)
454 * Returns name value of a datum.
457 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
461 * Returns datum representation for a name.
464 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
469 * Returns 32-bit floating point value of a datum.
470 * This is really a pointer, of course.
473 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
477 * Returns datum representation for a 32-bit floating point number.
478 * This is really a pointer, of course.
481 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
485 * Returns 64-bit floating point value of a datum.
486 * This is really a pointer, of course.
489 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
493 * Returns datum representation for a 64-bit floating point number.
494 * This is really a pointer, of course.
497 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
499 /* ----------------------------------------------------------------
500 * Section 5: IsValid macros for system types
501 * ----------------------------------------------------------------
505 * True iff bool is valid.
507 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
511 * True iff pointer is valid.
513 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
516 * PointerIsInBounds --
517 * True iff pointer is within given bounds.
520 * Assumes the bounded interval to be [min,max),
521 * i.e. closed on the left and open on the right.
523 #define PointerIsInBounds(pointer, min, max) \
524 ((min) <= (pointer) && (pointer) < (max))
527 * PointerIsAligned --
528 * True iff pointer is properly aligned to point to the given type.
530 #define PointerIsAligned(pointer, type) \
531 (((long)(pointer) % (sizeof (type))) == 0)
533 /* ----------------------------------------------------------------
534 * Section 6: offsetof, lengthof, endof
535 * ----------------------------------------------------------------
539 * Offset of a structure/union field within that structure/union.
541 * XXX This is supposed to be part of stddef.h, but isn't on
542 * some systems (like SunOS 4).
545 #define offsetof(type, field) ((long) &((type *)0)->field)
546 #endif /* offsetof */
550 * Number of elements in an array.
552 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
556 * Address of the element one past the last in an array.
558 #define endof(array) (&array[lengthof(array)])
560 /* ----------------------------------------------------------------
561 * Section 7: exception handling definitions
562 * Assert, Trap, etc macros
563 * ----------------------------------------------------------------
566 * Exception Handling definitions
569 typedef char *ExcMessage;
570 typedef struct Exception
576 * NO_ASSERT_CHECKING, if defined, turns off all the assertions.
579 * It should _NOT_ be undef'ed in releases or in benchmark copies
581 * #undef NO_ASSERT_CHECKING
586 * Generates an exception if the given condition is true.
589 #define Trap(condition, exception) \
591 ExceptionalCondition(CppAsString(condition), &(exception), \
592 (char*)NULL, __FILE__, __LINE__); }
595 * TrapMacro is the same as Trap but it's intended for use in macros:
597 * #define foo(x) (AssertM(x != 0) && bar(x))
601 #define TrapMacro(condition, exception) \
602 ((bool) ((! condition) || \
603 (ExceptionalCondition(CppAsString(condition), \
605 (char*) NULL, __FILE__, __LINE__))))
607 #ifdef NO_ASSERT_CHECKING
608 #define Assert(condition)
609 #define AssertMacro(condition) true
610 #define AssertArg(condition)
611 #define AssertState(condition)
613 #define Assert(condition) \
614 Trap(!(condition), FailedAssertion)
616 #define AssertMacro(condition) \
617 TrapMacro(!(condition), FailedAssertion)
619 #define AssertArg(condition) \
620 Trap(!(condition), BadArg)
622 #define AssertState(condition) \
623 Trap(!(condition), BadState)
625 #endif /* NO_ASSERT_CHECKING */
629 * Generates an exception with a message if the given condition is true.
632 #define LogTrap(condition, exception, printArgs) \
634 ExceptionalCondition(CppAsString(condition), &(exception), \
635 form printArgs, __FILE__, __LINE__); }
638 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
640 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
642 #define LogTrapMacro(condition, exception, printArgs) \
643 ((bool) ((! condition) || \
644 (ExceptionalCondition(CppAsString(condition), \
646 form printArgs, __FILE__, __LINE__))))
648 #ifdef NO_ASSERT_CHECKING
649 #define LogAssert(condition, printArgs)
650 #define LogAssertMacro(condition, printArgs) true
651 #define LogAssertArg(condition, printArgs)
652 #define LogAssertState(condition, printArgs)
654 #define LogAssert(condition, printArgs) \
655 LogTrap(!(condition), FailedAssertion, printArgs)
657 #define LogAssertMacro(condition, printArgs) \
658 LogTrapMacro(!(condition), FailedAssertion, printArgs)
660 #define LogAssertArg(condition, printArgs) \
661 LogTrap(!(condition), BadArg, printArgs)
663 #define LogAssertState(condition, printArgs) \
664 LogTrap(!(condition), BadState, printArgs)
666 #endif /* NO_ASSERT_CHECKING */
668 /* ----------------------------------------------------------------
669 * Section 8: Min, Max, Abs macros
670 * ----------------------------------------------------------------
674 * Return the maximum of two numbers.
676 #define Max(x, y) ((x) > (y) ? (x) : (y))
680 * Return the minimum of two numbers.
682 #define Min(x, y) ((x) < (y) ? (x) : (y))
686 * Return the absolute value of the argument.
688 #define Abs(x) ((x) >= 0 ? (x) : -(x))
692 * Does string copy, and forces terminating NULL
694 /* we do this so if the macro is used in an if action, it will work */
695 #define StrNCpy(dst,src,len) \
696 (strncpy((dst),(src),(len)),(len > 0) ? *((dst)+(len)-1)='\0' : (void)NULL,(void)(dst))
698 /* Get a bit mask of the bits set in non-int32 aligned addresses */
699 #define INT_ALIGN_MASK (sizeof(int32) - 1)
701 /* This function gets call too often, so we inline it if we can */
702 #define MemSet(start, val, len) do \
703 { /* are we aligned for int32? */ \
704 /* We have to cast the pointer to int \
705 so we can do the AND */ \
706 if (((int)(start) & INT_ALIGN_MASK) == 0 && \
707 ((len) & INT_ALIGN_MASK) == 0 && \
710 * We got this number by testing this \
711 * against the stock memset() on \
712 * bsd/os 3.0. Larger values were \
717 int32 *_i = (int32 *)(start); \
718 int32 *_stop = (int32 *)((char *)(start) + (len)); \
724 memset((start), (val), (len)); \
727 /* ----------------------------------------------------------------
729 * ----------------------------------------------------------------
732 extern Exception FailedAssertion;
733 extern Exception BadArg;
734 extern Exception BadState;
736 /* in utils/error/assert.c */
737 extern int ExceptionalCondition(char *conditionName,
738 Exception *exceptionP, char *details,
739 char *fileName, int lineNumber);
743 * form is used by assert and the exception handling stuff
746 extern char *form(const char *fmt,...);
750 /* ----------------------------------------------------------------
751 * Section 10: berkeley-specific configuration
753 * this section contains settings which are only relevant to the UC Berkeley
754 * sites. Other sites can ignore this
755 * ----------------------------------------------------------------
761 * These are experimental and are not supported in the code that
762 * we distribute to other sites.
771 /* ----------------------------------------------------------------
772 * Section 11: system-specific hacks
774 * This should be limited to things that absolutely have to be
775 * included in every source file. The changes should be factored
776 * into a separate file so that changes to one port don't require
777 * changes to c.h (and everyone recompiling their whole system).
778 * ----------------------------------------------------------------
783 #include "port/hpux/fixade.h" /* for unaligned access fixup */
788 #define memmove(d, s, l) bcopy(s, d, l)
792 /* These are for things that are one way on Unix and another on NT */
793 #define NULL_DEV "/dev/null"
794 #define COPY_CMD "cp"