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-2002, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
15 * $Id: c.h,v 1.129 2002/10/23 23:37:47 momjian 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 "postgres_ext.h"
64 #include <sys/types.h>
68 #include <sys/fcntl.h> /* ensure O_BINARY is available */
70 #ifdef HAVE_SUPPORTDEFS_H
71 #include <SupportDefs.h>
74 /* Must be before gettext() games below */
80 #define gettext(x) (x)
82 #define gettext_noop(x) (x)
85 /* ----------------------------------------------------------------
86 * Section 1: hacks to cope with non-ANSI C compilers
88 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
89 * ----------------------------------------------------------------
94 * Convert the argument to a string, using the C preprocessor.
96 * Concatenate two arguments together, using the C preprocessor.
98 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
99 * whether #identifier works, but if we have that we likely have ## too.
101 #if defined(HAVE_STRINGIZE)
103 #define CppAsString(identifier) #identifier
104 #define CppConcat(x, y) x##y
106 #else /* !HAVE_STRINGIZE */
108 #define CppAsString(identifier) "identifier"
111 * CppIdentity -- On Reiser based cpp's this is used to concatenate
112 * two tokens. That is
113 * CppIdentity(A)B ==> AB
114 * We renamed it to _private_CppIdentity because it should not
115 * be referenced outside this file. On other cpp's it
118 #define _priv_CppIdentity(x)x
119 #define CppConcat(x, y) _priv_CppIdentity(x)y
120 #endif /* !HAVE_STRINGIZE */
123 * dummyret is used to set return values in macros that use ?: to make
124 * assignments. gcc wants these to be void, other compilers like char
126 #ifdef __GNUC__ /* GNU cc */
127 #define dummyret void
129 #define dummyret char
133 #define __attribute__(_arg_)
136 /* ----------------------------------------------------------------
137 * Section 2: bool, true, false, TRUE, FALSE, NULL
138 * ----------------------------------------------------------------
143 * Boolean value, either true or false.
145 * XXX for C++ compilers, we assume the compiler has a compatible
146 * built-in definition of bool.
149 /* BeOS defines bool already, but the compiler chokes on the
150 * #ifndef unless we wrap it in this check.
161 #define true ((bool) 1)
165 #define false ((bool) 0)
168 #endif /* __BEOS__ */
170 typedef bool *BoolPtr;
185 #define NULL ((void *) 0)
189 /* ----------------------------------------------------------------
190 * Section 3: standard system types
191 * ----------------------------------------------------------------
196 * Variable holding address of any memory resident object.
198 * XXX Pointer arithmetic is done with this, so it can't be void *
199 * under "true" ANSI compilers.
201 typedef char *Pointer;
205 * Signed integer, EXACTLY N BITS IN SIZE,
206 * used for numerical computations and the
207 * frontend/backend protocol.
210 typedef signed char int8; /* == 8 bits */
211 typedef signed short int16; /* == 16 bits */
212 typedef signed int int32; /* == 32 bits */
213 #endif /* not HAVE_INT8 */
217 * Unsigned integer, EXACTLY N BITS IN SIZE,
218 * used for numerical computations and the
219 * frontend/backend protocol.
221 /* Also defined in interfaces/odbc/md5.h */
223 typedef unsigned char uint8; /* == 8 bits */
224 typedef unsigned short uint16; /* == 16 bits */
225 typedef unsigned int uint32; /* == 32 bits */
226 #endif /* not HAVE_UINT8 */
230 * Boolean value, AT LEAST N BITS IN SIZE.
232 typedef uint8 bool8; /* >= 8 bits */
233 typedef uint16 bool16; /* >= 16 bits */
234 typedef uint32 bool32; /* >= 32 bits */
238 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
240 typedef uint8 bits8; /* >= 8 bits */
241 typedef uint16 bits16; /* >= 16 bits */
242 typedef uint32 bits32; /* >= 32 bits */
246 * Unit of storage, AT LEAST N BITS IN SIZE,
247 * used to fetch/store data.
249 typedef uint8 word8; /* >= 8 bits */
250 typedef uint16 word16; /* >= 16 bits */
251 typedef uint32 word32; /* >= 32 bits */
255 * Floating point number, AT LEAST N BITS IN SIZE,
256 * used for numerical computations.
258 * Since sizeof(floatN) may be > sizeof(char *), always pass
259 * floatN by reference.
261 * XXX: these typedefs are now deprecated in favor of float4 and float8.
262 * They will eventually go away.
264 typedef float float32data;
265 typedef double float64data;
266 typedef float *float32;
267 typedef double *float64;
272 #ifdef HAVE_LONG_INT_64
273 /* Plain "long int" fits, use it */
276 typedef long int int64;
279 typedef unsigned long int uint64;
282 #elif defined(HAVE_LONG_LONG_INT_64)
283 /* We have working support for "long long int", use that */
286 typedef long long int int64;
289 typedef unsigned long long int uint64;
292 #else /* not HAVE_LONG_INT_64 and not
293 * HAVE_LONG_LONG_INT_64 */
295 /* Won't actually work, but fall back to long int so that code compiles */
297 typedef long int int64;
300 typedef unsigned long int uint64;
303 #define INT64_IS_BUSTED
304 #endif /* not HAVE_LONG_INT_64 and not
305 * HAVE_LONG_LONG_INT_64 */
307 /* Decide if we need to decorate 64-bit constants */
308 #ifdef HAVE_LL_CONSTANTS
309 #define INT64CONST(x) ((int64) x##LL)
310 #define UINT64CONST(x) ((uint64) x##LL)
312 #define INT64CONST(x) ((int64) x)
313 #define UINT64CONST(x) ((uint64) x)
317 /* Select timestamp representation (float8 or int64) */
318 #if defined(USE_INTEGER_DATETIMES) && !defined(INT64_IS_BUSTED)
319 #define HAVE_INT64_TIMESTAMP
322 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
323 #ifndef HAVE_SIG_ATOMIC_T
324 typedef int sig_atomic_t;
329 * Size of any memory resident object, as returned by sizeof.
335 * Index into any memory resident array.
338 * Indices are non negative.
340 typedef unsigned int Index;
344 * Offset into any memory resident array.
347 * This differs from an Index in that an Index is always
348 * non negative, whereas Offset may be negative.
350 typedef signed int Offset;
353 * Common Postgres datatype names (as used in the catalogs)
357 typedef float float4;
358 typedef double float8;
361 * Oid, RegProcedure, TransactionId, CommandId
364 /* typedef Oid is in postgres_ext.h */
367 * regproc is the type name used in the include/catalog headers, but
368 * RegProcedure is the preferred name in C code.
371 typedef regproc RegProcedure;
373 typedef uint32 TransactionId;
375 typedef uint32 CommandId;
377 #define FirstCommandId ((CommandId) 0)
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 RegProcedureIsValid(p) OidIsValid(p)
468 /* ----------------------------------------------------------------
469 * Section 5: offsetof, lengthof, endof, alignment
470 * ----------------------------------------------------------------
474 * Offset of a structure/union field within that structure/union.
476 * XXX This is supposed to be part of stddef.h, but isn't on
477 * some systems (like SunOS 4).
480 #define offsetof(type, field) ((long) &((type *)0)->field)
481 #endif /* offsetof */
485 * Number of elements in an array.
487 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
491 * Address of the element one past the last in an array.
493 #define endof(array) (&array[lengthof(array)])
496 * Alignment macros: align a length or address appropriately for a given type.
498 * There used to be some incredibly crufty platform-dependent hackery here,
499 * but now we rely on the configure script to get the info for us. Much nicer.
501 * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
502 * That case seems extremely unlikely to occur in practice, however.
506 #define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
508 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
509 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
510 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
511 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
512 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
515 /* ----------------------------------------------------------------
516 * Section 6: widely useful macros
517 * ----------------------------------------------------------------
521 * Return the maximum of two numbers.
523 #define Max(x, y) ((x) > (y) ? (x) : (y))
527 * Return the minimum of two numbers.
529 #define Min(x, y) ((x) < (y) ? (x) : (y))
533 * Return the absolute value of the argument.
535 #define Abs(x) ((x) >= 0 ? (x) : -(x))
539 * Like standard library function strncpy(), except that result string
540 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
541 * of the source string will be kept.
542 * Also, the macro returns no result (too hard to do that without
543 * evaluating the arguments multiple times, which seems worse).
545 * BTW: when you need to copy a non-null-terminated string (like a text
546 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
547 * might seem to work, but it fetches one byte more than there is in the
548 * text object. One fine day you'll have a SIGSEGV because there isn't
549 * another byte before the end of memory. Don't laugh, we've had real
550 * live bug reports from real live users over exactly this mistake.
551 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
553 #define StrNCpy(dst,src,len) \
556 char * _dst = (dst); \
561 strncpy(_dst, (src), _len); \
562 _dst[_len-1] = '\0'; \
567 /* Get a bit mask of the bits set in non-int32 aligned addresses */
568 #define INT_ALIGN_MASK (sizeof(int32) - 1)
572 * Exactly the same as standard library function memset(), but considerably
573 * faster for zeroing small word-aligned structures (such as parsetree nodes).
574 * This has to be a macro because the main point is to avoid function-call
575 * overhead. However, we have also found that the loop is faster than
576 * native libc memset() on some platforms, even those with assembler
577 * memset() functions. More research needs to be done, perhaps with
578 * platform-specific MEMSET_LOOP_LIMIT values or tests in configure.
582 #define MemSet(start, val, len) \
585 int32 * _start = (int32 *) (start); \
589 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
590 (_len & INT_ALIGN_MASK) == 0 && \
592 _len <= MEMSET_LOOP_LIMIT) \
594 int32 * _stop = (int32 *) ((char *) _start + _len); \
595 while (_start < _stop) \
599 memset((char *) _start, _val, _len); \
602 #define MEMSET_LOOP_LIMIT 1024
605 /* ----------------------------------------------------------------
606 * Section 7: random stuff
607 * ----------------------------------------------------------------
611 #define CSIGNBIT (0x80)
613 #define STATUS_OK (0)
614 #define STATUS_ERROR (-1)
615 #define STATUS_EOF (-2)
616 #define STATUS_FOUND (1)
619 /* ----------------------------------------------------------------
620 * Section 8: system-specific hacks
622 * This should be limited to things that absolutely have to be
623 * included in every source file. The port-specific header file
624 * is usually a better place for this sort of thing.
625 * ----------------------------------------------------------------
629 #define PG_BINARY O_BINARY
630 #define PG_BINARY_R "rb"
631 #define PG_BINARY_W "wb"
634 #define PG_BINARY_R "r"
635 #define PG_BINARY_W "w"
638 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
643 int fseeko(FILE *stream, off_t offset, int whence);
644 off_t ftello(FILE *stream);
647 /* These are for things that are one way on Unix and another on NT */
648 #define NULL_DEV "/dev/null"
651 * Provide prototypes for routines not present in a particular machine's
652 * standard C library. It'd be better to put these in pg_config.h, but
653 * in pg_config.h we haven't yet included anything that defines size_t...
656 #if !HAVE_DECL_SNPRINTF
658 snprintf(char *str, size_t count, const char *fmt,...)
659 /* This extension allows gcc to check the format string */
660 __attribute__((format(printf, 3, 4)));
663 #if !HAVE_DECL_VSNPRINTF
664 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
667 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
668 #define memmove(d, s, c) bcopy(s, d, c)