1 /*-------------------------------------------------------------------------
4 * Fundamental C definitions. This is included by every .c file in
5 * PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate).
7 * Note that the definitions here are not intended to be exposed to clients
8 * of the frontend interface libraries --- so we don't worry much about
9 * polluting the namespace with lots of stuff...
12 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
15 * $PostgreSQL: pgsql/src/include/c.h,v 1.159 2004/01/10 23:39:51 neilc Exp $
17 *-------------------------------------------------------------------------
20 *----------------------------------------------------------------
23 * When adding stuff to this file, please try to put stuff
24 * into the relevant section, or add new sections as appropriate.
27 * ------- ------------------------------------------------
28 * 0) pg_config.h and standard system headers
29 * 1) hacks to cope with non-ANSI C compilers
30 * 2) bool, true, false, TRUE, FALSE, NULL
31 * 3) standard system types
32 * 4) IsValid macros for system types
33 * 5) offsetof, lengthof, endof, alignment
34 * 6) widely useful macros
36 * 8) system-specific hacks
38 * NOTE: since this file is included by both frontend and backend modules, it's
39 * almost certainly wrong to put an "extern" declaration here. typedefs and
40 * macros are the kind of thing that might go here.
42 *----------------------------------------------------------------
48 * We have to include stdlib.h here because it defines many of these macros
49 * on some platforms, and we only want our definitions used if stdlib.h doesn't
50 * have its own. The same goes for stddef and stdarg if present.
53 #include "pg_config.h"
54 #include "pg_config_manual.h" /* must be after pg_config.h */
56 #include "pg_config_os.h" /* must be before any system header files */
58 #include "postgres_ext.h"
68 #include <sys/types.h>
71 #if defined(__CYGWIN__) || defined(WIN32)
72 #include <fcntl.h> /* ensure O_BINARY is available */
74 #ifdef HAVE_SUPPORTDEFS_H
75 #include <SupportDefs.h>
78 #if defined(WIN32) && !defined(_MSC_VER) && !defined(__BORLANDC__)
79 /* We have to redefine some system functions after they are included above */
80 #include "pg_config_os.h"
83 /* Must be before gettext() games below */
89 #define gettext(x) (x)
91 #define gettext_noop(x) (x)
94 /* ----------------------------------------------------------------
95 * Section 1: hacks to cope with non-ANSI C compilers
97 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
98 * ----------------------------------------------------------------
103 * Convert the argument to a string, using the C preprocessor.
105 * Concatenate two arguments together, using the C preprocessor.
107 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
108 * whether #identifier works, but if we have that we likely have ## too.
110 #if defined(HAVE_STRINGIZE)
112 #define CppAsString(identifier) #identifier
113 #define CppConcat(x, y) x##y
115 #else /* !HAVE_STRINGIZE */
117 #define CppAsString(identifier) "identifier"
120 * CppIdentity -- On Reiser based cpp's this is used to concatenate
121 * two tokens. That is
122 * CppIdentity(A)B ==> AB
123 * We renamed it to _private_CppIdentity because it should not
124 * be referenced outside this file. On other cpp's it
127 #define _priv_CppIdentity(x)x
128 #define CppConcat(x, y) _priv_CppIdentity(x)y
129 #endif /* !HAVE_STRINGIZE */
132 * dummyret is used to set return values in macros that use ?: to make
133 * assignments. gcc wants these to be void, other compilers like char
135 #ifdef __GNUC__ /* GNU cc */
136 #define dummyret void
138 #define dummyret char
142 #define __attribute__(_arg_)
145 /* ----------------------------------------------------------------
146 * Section 2: bool, true, false, TRUE, FALSE, NULL
147 * ----------------------------------------------------------------
152 * Boolean value, either true or false.
154 * XXX for C++ compilers, we assume the compiler has a compatible
155 * built-in definition of bool.
158 /* BeOS defines bool already, but the compiler chokes on the
159 * #ifndef unless we wrap it in this check.
170 #define true ((bool) 1)
174 #define false ((bool) 0)
177 #endif /* __BEOS__ */
179 typedef bool *BoolPtr;
194 #define NULL ((void *) 0)
198 /* ----------------------------------------------------------------
199 * Section 3: standard system types
200 * ----------------------------------------------------------------
205 * Variable holding address of any memory resident object.
207 * XXX Pointer arithmetic is done with this, so it can't be void *
208 * under "true" ANSI compilers.
210 typedef char *Pointer;
214 * Signed integer, EXACTLY N BITS IN SIZE,
215 * used for numerical computations and the
216 * frontend/backend protocol.
219 typedef signed char int8; /* == 8 bits */
220 typedef signed short int16; /* == 16 bits */
221 typedef signed int int32; /* == 32 bits */
222 #endif /* not HAVE_INT8 */
226 * Unsigned integer, EXACTLY N BITS IN SIZE,
227 * used for numerical computations and the
228 * frontend/backend protocol.
230 /* Also defined in interfaces/odbc/md5.h */
232 typedef unsigned char uint8; /* == 8 bits */
233 typedef unsigned short uint16; /* == 16 bits */
234 typedef unsigned int uint32; /* == 32 bits */
235 #endif /* not HAVE_UINT8 */
239 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
241 typedef uint8 bits8; /* >= 8 bits */
242 typedef uint16 bits16; /* >= 16 bits */
243 typedef uint32 bits32; /* >= 32 bits */
247 * Floating point number, AT LEAST N BITS IN SIZE,
248 * used for numerical computations.
250 * Since sizeof(floatN) may be > sizeof(char *), always pass
251 * floatN by reference.
253 * XXX: these typedefs are now deprecated in favor of float4 and float8.
254 * They will eventually go away.
256 typedef float float32data;
257 typedef double float64data;
258 typedef float *float32;
259 typedef double *float64;
264 #ifdef HAVE_LONG_INT_64
265 /* Plain "long int" fits, use it */
268 typedef long int int64;
271 typedef unsigned long int uint64;
274 #elif defined(HAVE_LONG_LONG_INT_64)
275 /* We have working support for "long long int", use that */
278 typedef long long int int64;
281 typedef unsigned long long int uint64;
284 #else /* not HAVE_LONG_INT_64 and not HAVE_LONG_LONG_INT_64 */
286 /* Won't actually work, but fall back to long int so that code compiles */
288 typedef long int int64;
291 typedef unsigned long int uint64;
294 #define INT64_IS_BUSTED
295 #endif /* not HAVE_LONG_INT_64 and not HAVE_LONG_LONG_INT_64 */
297 /* Decide if we need to decorate 64-bit constants */
298 #ifdef HAVE_LL_CONSTANTS
299 #define INT64CONST(x) ((int64) x##LL)
300 #define UINT64CONST(x) ((uint64) x##LL)
302 #define INT64CONST(x) ((int64) x)
303 #define UINT64CONST(x) ((uint64) x)
307 /* Select timestamp representation (float8 or int64) */
308 #if defined(USE_INTEGER_DATETIMES) && !defined(INT64_IS_BUSTED)
309 #define HAVE_INT64_TIMESTAMP
312 /* Global variable holding time zone information. */
313 #ifndef HAVE_UNDERSCORE_TIMEZONE
314 #define TIMEZONE_GLOBAL timezone
316 #define TIMEZONE_GLOBAL _timezone
317 #define tzname _tzname /* should be in time.h? */
320 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
321 #ifndef HAVE_SIG_ATOMIC_T
322 typedef int sig_atomic_t;
327 * Size of any memory resident object, as returned by sizeof.
333 * Index into any memory resident array.
336 * Indices are non negative.
338 typedef unsigned int Index;
342 * Offset into any memory resident array.
345 * This differs from an Index in that an Index is always
346 * non negative, whereas Offset may be negative.
348 typedef signed int Offset;
351 * Common Postgres datatype names (as used in the catalogs)
355 typedef float float4;
356 typedef double float8;
359 * Oid, RegProcedure, TransactionId, CommandId, AclId
362 /* typedef Oid is in postgres_ext.h */
365 * regproc is the type name used in the include/catalog headers, but
366 * RegProcedure is the preferred name in C code.
369 typedef regproc RegProcedure;
371 typedef uint32 TransactionId;
373 typedef uint32 CommandId;
375 #define FirstCommandId ((CommandId) 0)
377 typedef int32 AclId; /* user and group identifiers */
380 * Array indexing support
389 * Variable-length datatypes all share the 'struct varlena' header.
391 * NOTE: for TOASTable types, this is an oversimplification, since the value
392 * may be compressed or moved out-of-line. However datatype-specific routines
393 * are mostly content to deal with de-TOASTed values only, and of course
394 * client-side routines should never see a TOASTed value. See postgres.h for
395 * details of the TOASTed form.
404 #define VARHDRSZ ((int32) sizeof(int32))
407 * These widely-used datatypes are just a varlena header and the data bytes.
408 * There is no terminating null or anything like that --- the data length is
409 * always VARSIZE(ptr) - VARHDRSZ.
411 typedef struct varlena bytea;
412 typedef struct varlena text;
413 typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */
414 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
417 * Fixed-length array types (these are not varlena's!)
420 typedef int2 int2vector[INDEX_MAX_KEYS];
421 typedef Oid oidvector[INDEX_MAX_KEYS];
424 * We want NameData to have length NAMEDATALEN and int alignment,
425 * because that's how the data type 'name' is defined in pg_type.
426 * Use a union to make sure the compiler agrees. Note that NAMEDATALEN
427 * must be a multiple of sizeof(int), else sizeof(NameData) will probably
428 * not come out equal to NAMEDATALEN.
430 typedef union nameData
432 char data[NAMEDATALEN];
435 typedef NameData *Name;
437 #define NameStr(name) ((name).data)
440 /* ----------------------------------------------------------------
441 * Section 4: IsValid macros for system types
442 * ----------------------------------------------------------------
446 * True iff bool is valid.
448 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
452 * True iff pointer is valid.
454 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
458 * True iff pointer is properly aligned to point to the given type.
460 #define PointerIsAligned(pointer, type) \
461 (((long)(pointer) % (sizeof (type))) == 0)
463 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
465 #define AclIdIsValid(aclId) ((bool) ((aclId) != 0))
467 #define RegProcedureIsValid(p) OidIsValid(p)
470 /* ----------------------------------------------------------------
471 * Section 5: offsetof, lengthof, endof, alignment
472 * ----------------------------------------------------------------
476 * Offset of a structure/union field within that structure/union.
478 * XXX This is supposed to be part of stddef.h, but isn't on
479 * some systems (like SunOS 4).
482 #define offsetof(type, field) ((long) &((type *)0)->field)
483 #endif /* offsetof */
487 * Number of elements in an array.
489 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
493 * Address of the element one past the last in an array.
495 #define endof(array) (&array[lengthof(array)])
498 * Alignment macros: align a length or address appropriately for a given type.
500 * There used to be some incredibly crufty platform-dependent hackery here,
501 * but now we rely on the configure script to get the info for us. Much nicer.
503 * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
504 * That case seems extremely unlikely to occur in practice, however.
508 #define TYPEALIGN(ALIGNVAL,LEN) \
509 (((long) (LEN) + (ALIGNVAL-1)) & ~((long) (ALIGNVAL-1)))
511 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
512 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
513 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
514 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
515 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
516 /* MAXALIGN covers only built-in types, not buffers */
517 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
520 /* ----------------------------------------------------------------
521 * Section 6: widely useful macros
522 * ----------------------------------------------------------------
526 * Return the maximum of two numbers.
528 #define Max(x, y) ((x) > (y) ? (x) : (y))
532 * Return the minimum of two numbers.
534 #define Min(x, y) ((x) < (y) ? (x) : (y))
538 * Return the absolute value of the argument.
540 #define Abs(x) ((x) >= 0 ? (x) : -(x))
544 * Like standard library function strncpy(), except that result string
545 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
546 * of the source string will be kept.
547 * Also, the macro returns no result (too hard to do that without
548 * evaluating the arguments multiple times, which seems worse).
550 * BTW: when you need to copy a non-null-terminated string (like a text
551 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
552 * might seem to work, but it fetches one byte more than there is in the
553 * text object. One fine day you'll have a SIGSEGV because there isn't
554 * another byte before the end of memory. Don't laugh, we've had real
555 * live bug reports from real live users over exactly this mistake.
556 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
558 #define StrNCpy(dst,src,len) \
561 char * _dst = (dst); \
566 strncpy(_dst, (src), _len); \
567 _dst[_len-1] = '\0'; \
572 /* Get a bit mask of the bits set in non-int32 aligned addresses */
573 #define INT_ALIGN_MASK (sizeof(int32) - 1)
577 * Exactly the same as standard library function memset(), but considerably
578 * faster for zeroing small word-aligned structures (such as parsetree nodes).
579 * This has to be a macro because the main point is to avoid function-call
580 * overhead. However, we have also found that the loop is faster than
581 * native libc memset() on some platforms, even those with assembler
582 * memset() functions. More research needs to be done, perhaps with
583 * platform-specific MEMSET_LOOP_LIMIT values or tests in configure.
587 #define MemSet(start, val, len) \
590 int32 * _start = (int32 *) (start); \
594 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
595 (_len & INT_ALIGN_MASK) == 0 && \
597 _len <= MEMSET_LOOP_LIMIT) \
599 int32 * _stop = (int32 *) ((char *) _start + _len); \
600 while (_start < _stop) \
604 memset((char *) _start, _val, _len); \
607 #define MEMSET_LOOP_LIMIT 1024
610 * MemSetAligned is the same as MemSet except it omits the test to see if
611 * "start" is word-aligned. This is okay to use if the caller knows a-priori
612 * that the pointer is suitably aligned (typically, because he just got it
613 * from palloc(), which always delivers a max-aligned pointer).
615 #define MemSetAligned(start, val, len) \
618 int32 * _start = (int32 *) (start); \
622 if ((_len & INT_ALIGN_MASK) == 0 && \
624 _len <= MEMSET_LOOP_LIMIT) \
626 int32 * _stop = (int32 *) ((char *) _start + _len); \
627 while (_start < _stop) \
631 memset((char *) _start, _val, _len); \
636 * MemSetTest/MemSetLoop are a variant version that allow all the tests in
637 * MemSet to be done at compile time in cases where "val" and "len" are
638 * constants *and* we know the "start" pointer must be word-aligned.
639 * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
640 * MemSetAligned. Beware of multiple evaluations of the arguments when using
643 #define MemSetTest(val, len) \
644 ( ((len) & INT_ALIGN_MASK) == 0 && \
645 (len) <= MEMSET_LOOP_LIMIT && \
648 #define MemSetLoop(start, val, len) \
651 int32 * _start = (int32 *) (start); \
652 int32 * _stop = (int32 *) ((char *) _start + (Size) (len)); \
654 while (_start < _stop) \
659 /* ----------------------------------------------------------------
660 * Section 7: random stuff
661 * ----------------------------------------------------------------
665 #define CSIGNBIT (0x80)
667 #define STATUS_OK (0)
668 #define STATUS_ERROR (-1)
669 #define STATUS_EOF (-2)
670 #define STATUS_FOUND (1)
673 /* ----------------------------------------------------------------
674 * Section 8: system-specific hacks
676 * This should be limited to things that absolutely have to be
677 * included in every source file. The port-specific header file
678 * is usually a better place for this sort of thing.
679 * ----------------------------------------------------------------
682 #if defined(__CYGWIN__) || defined(WIN32)
683 #define PG_BINARY O_BINARY
684 #define PG_BINARY_R "rb"
685 #define PG_BINARY_W "wb"
688 #define PG_BINARY_R "r"
689 #define PG_BINARY_W "w"
692 #if !defined(WIN32) && !defined(__BEOS__)
693 #define FCNTL_NONBLOCK(sock) fcntl(sock, F_SETFL, O_NONBLOCK)
695 extern long ioctlsocket_ret;
697 /* Returns non-0 on failure, while fcntl() returns -1 on failure */
699 #define FCNTL_NONBLOCK(sock) ((ioctlsocket(sock, FIONBIO, &ioctlsocket_ret) == 0) ? 0 : -1)
702 #define FCNTL_NONBLOCK(sock) ((ioctl(sock, FIONBIO, &ioctlsocket_ret) == 0) ? 0 : -1)
706 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
710 /* These are for things that are one way on Unix and another on NT */
711 #define NULL_DEV "/dev/null"
714 * Provide prototypes for routines not present in a particular machine's
715 * standard C library.
718 #if !HAVE_DECL_SNPRINTF
720 snprintf(char *str, size_t count, const char *fmt,...)
721 /* This extension allows gcc to check the format string */
722 __attribute__((format(printf, 3, 4)));
725 #if !HAVE_DECL_VSNPRINTF
726 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
729 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
730 #define memmove(d, s, c) bcopy(s, d, c)
734 #define DLLIMPORT /* no special DLL markers on most ports */
738 * The following is used as the arg list for signal handlers. Any ports
739 * that take something other than an int argument should override this in
740 * their pg_config_os.h file. Note that variable names are required
741 * because it is used in both the prototypes as well as the definitions.
742 * Note also the long name. We expect that this won't collide with
743 * other names causing compiler warnings.
747 #define SIGNAL_ARGS int postgres_signal_arg
751 * When there is no sigsetjmp, its functionality is provided by plain
752 * setjmp. Incidentally, nothing provides setjmp's functionality in
755 #ifndef HAVE_SIGSETJMP
756 #define sigjmp_buf jmp_buf
757 #define sigsetjmp(x,y) setjmp(x)
758 #define siglongjmp longjmp
761 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
762 extern int fdatasync(int fildes);
765 /* If strtoq() exists, rename it to the more standard strtoll() */
766 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
767 #define strtoll strtoq
768 #define HAVE_STRTOLL 1
771 /* If strtouq() exists, rename it to the more standard strtoull() */
772 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
773 #define strtoull strtouq
774 #define HAVE_STRTOULL 1
777 /* EXEC_BACKEND defines */
779 #define NON_EXEC_STATIC
781 #define NON_EXEC_STATIC static
784 /* /port compatibility functions */