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 of
8 * the frontend interface libraries --- so we don't worry much about polluting
9 * the namespace with lots of stuff...
12 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
15 * $Id: c.h,v 1.96 2001/07/03 20:21:50 petere 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) 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 macros
40 * are the kind of thing that might go here.
42 *----------------------------------------------------------------
47 /* We have to include stdlib.h here because it defines many of these macros
48 on some platforms, and we only want our definitions used if stdlib.h doesn't
49 have its own. The same goes for stddef and stdarg if present.
59 #ifdef STRING_H_WITH_STRINGS_H
65 #include <sys/fcntl.h> /* ensure O_BINARY is available */
67 #ifdef HAVE_SUPPORTDEFS_H
68 #include <SupportDefs.h>
74 #define gettext(x) (x)
76 #define gettext_noop(x) (x)
79 /* ----------------------------------------------------------------
80 * Section 1: hacks to cope with non-ANSI C compilers
82 * type prefixes (const, signed, volatile, inline) are now handled in config.h.
83 * ----------------------------------------------------------------
88 * Convert the argument to a string, using the C preprocessor.
90 * Concatenate two arguments together, using the C preprocessor.
92 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
93 * whether #identifier works, but if we have that we likely have ## too.
95 #if defined(HAVE_STRINGIZE)
97 #define CppAsString(identifier) #identifier
98 #define CppConcat(x, y) x##y
100 #else /* !HAVE_STRINGIZE */
102 #define CppAsString(identifier) "identifier"
105 * CppIdentity -- On Reiser based cpp's this is used to concatenate
106 * two tokens. That is
107 * CppIdentity(A)B ==> AB
108 * We renamed it to _private_CppIdentity because it should not
109 * be referenced outside this file. On other cpp's it
112 #define _priv_CppIdentity(x)x
113 #define CppConcat(x, y) _priv_CppIdentity(x)y
115 #endif /* !HAVE_STRINGIZE */
118 * dummyret is used to set return values in macros that use ?: to make
119 * assignments. gcc wants these to be void, other compilers like char
121 #ifdef __GNUC__ /* GNU cc */
122 #define dummyret void
124 #define dummyret char
128 #define __attribute__(x)
131 /* ----------------------------------------------------------------
132 * Section 2: bool, true, false, TRUE, FALSE, NULL
133 * ----------------------------------------------------------------
137 * Boolean value, either true or false.
141 /* BeOS defines bool already, but the compiler chokes on the
142 * #ifndef unless we wrap it in this check.
150 #endif /* ndef bool */
154 #define true ((bool) 1)
158 #define false ((bool) 0)
161 #endif /* __BEOS__ */
163 typedef bool *BoolPtr;
178 #define NULL ((void *) 0)
182 /* ----------------------------------------------------------------
183 * Section 3: standard system types
184 * ----------------------------------------------------------------
189 * Variable holding address of any memory resident object.
191 * XXX Pointer arithmetic is done with this, so it can't be void *
192 * under "true" ANSI compilers.
194 typedef char *Pointer;
198 * Signed integer, EXACTLY N BITS IN SIZE,
199 * used for numerical computations and the
200 * frontend/backend protocol.
202 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
203 typedef signed char int8; /* == 8 bits */
204 typedef signed short int16; /* == 16 bits */
205 typedef signed int int32; /* == 32 bits */
207 #endif /* __BEOS__ */
211 * Unsigned integer, EXACTLY N BITS IN SIZE,
212 * used for numerical computations and the
213 * frontend/backend protocol.
215 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
216 typedef unsigned char uint8; /* == 8 bits */
217 typedef unsigned short uint16; /* == 16 bits */
218 typedef unsigned int uint32; /* == 32 bits */
220 #endif /* __BEOS__ */
224 * Boolean value, AT LEAST N BITS IN SIZE.
226 typedef uint8 bool8; /* >= 8 bits */
227 typedef uint16 bool16; /* >= 16 bits */
228 typedef uint32 bool32; /* >= 32 bits */
232 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
234 typedef uint8 bits8; /* >= 8 bits */
235 typedef uint16 bits16; /* >= 16 bits */
236 typedef uint32 bits32; /* >= 32 bits */
240 * Unit of storage, AT LEAST N BITS IN SIZE,
241 * used to fetch/store data.
243 typedef uint8 word8; /* >= 8 bits */
244 typedef uint16 word16; /* >= 16 bits */
245 typedef uint32 word32; /* >= 32 bits */
249 * Floating point number, AT LEAST N BITS IN SIZE,
250 * used for numerical computations.
252 * Since sizeof(floatN) may be > sizeof(char *), always pass
253 * floatN by reference.
255 * XXX: these typedefs are now deprecated in favor of float4 and float8.
256 * They will eventually go away.
258 typedef float float32data;
259 typedef double float64data;
260 typedef float *float32;
261 typedef double *float64;
266 #ifndef __BEOS__ /* this is already defined on BeOS */
267 #ifdef HAVE_LONG_INT_64
268 /* Plain "long int" fits, use it */
269 typedef long int int64;
270 typedef unsigned long int uint64;
273 #ifdef HAVE_LONG_LONG_INT_64
274 /* We have working support for "long long int", use that */
275 typedef long long int int64;
276 typedef unsigned long long int uint64;
279 /* Won't actually work, but fall back to long int so that code compiles */
280 typedef long int int64;
281 typedef unsigned long int uint64;
283 #define INT64_IS_BUSTED
286 #endif /* __BEOS__ */
290 * Size of any memory resident object, as returned by sizeof.
296 * Index into any memory resident array.
299 * Indices are non negative.
301 typedef unsigned int Index;
305 * Offset into any memory resident array.
308 * This differs from an Index in that an Index is always
309 * non negative, whereas Offset may be negative.
311 typedef signed int Offset;
314 * Common Postgres datatype names (as used in the catalogs)
318 typedef float float4;
319 typedef double float8;
322 * Oid, RegProcedure, TransactionId, CommandId
325 /* typedef Oid is in postgres_ext.h */
327 /* unfortunately, both regproc and RegProcedure are used */
329 typedef Oid RegProcedure;
331 typedef uint32 TransactionId;
333 #define InvalidTransactionId 0
335 typedef uint32 CommandId;
337 #define FirstCommandId 0
340 * Array indexing support
349 * Variable-length datatypes all share the 'struct varlena' header.
351 * NOTE: for TOASTable types, this is an oversimplification, since the value may be
352 * compressed or moved out-of-line. However datatype-specific routines are mostly
353 * content to deal with de-TOASTed values only, and of course client-side routines
354 * should never see a TOASTed value. See postgres.h for details of the TOASTed form.
363 #define VARHDRSZ ((int32) sizeof(int32))
366 * These widely-used datatypes are just a varlena header and the data bytes.
367 * There is no terminating null or anything like that --- the data length is
368 * always VARSIZE(ptr) - VARHDRSZ.
370 typedef struct varlena bytea;
371 typedef struct varlena text;
372 typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */
373 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
376 * Fixed-length array types (these are not varlena's!)
379 typedef int2 int2vector[INDEX_MAX_KEYS];
380 typedef Oid oidvector[INDEX_MAX_KEYS];
383 * We want NameData to have length NAMEDATALEN and int alignment,
384 * because that's how the data type 'name' is defined in pg_type.
385 * Use a union to make sure the compiler agrees.
387 typedef union nameData
389 char data[NAMEDATALEN];
392 typedef NameData *Name;
394 #define NameStr(name) ((name).data)
397 /* ----------------------------------------------------------------
398 * Section 4: IsValid macros for system types
399 * ----------------------------------------------------------------
403 * True iff bool is valid.
405 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
409 * True iff pointer is valid.
411 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
415 * True iff pointer is properly aligned to point to the given type.
417 #define PointerIsAligned(pointer, type) \
418 (((long)(pointer) % (sizeof (type))) == 0)
420 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
422 #define RegProcedureIsValid(p) OidIsValid(p)
425 /* ----------------------------------------------------------------
426 * Section 5: offsetof, lengthof, endof, alignment
427 * ----------------------------------------------------------------
431 * Offset of a structure/union field within that structure/union.
433 * XXX This is supposed to be part of stddef.h, but isn't on
434 * some systems (like SunOS 4).
437 #define offsetof(type, field) ((long) &((type *)0)->field)
438 #endif /* offsetof */
442 * Number of elements in an array.
444 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
448 * Address of the element one past the last in an array.
450 #define endof(array) (&array[lengthof(array)])
453 * Alignment macros: align a length or address appropriately for a given type.
455 * There used to be some incredibly crufty platform-dependent hackery here,
456 * but now we rely on the configure script to get the info for us. Much nicer.
458 * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
459 * That case seems extremely unlikely to occur in practice, however.
463 #define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
465 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
466 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
467 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
468 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
469 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
472 /* ----------------------------------------------------------------
473 * Section 6: widely useful macros
474 * ----------------------------------------------------------------
478 * Return the maximum of two numbers.
480 #define Max(x, y) ((x) > (y) ? (x) : (y))
484 * Return the minimum of two numbers.
486 #define Min(x, y) ((x) < (y) ? (x) : (y))
490 * Return the absolute value of the argument.
492 #define Abs(x) ((x) >= 0 ? (x) : -(x))
496 * Like standard library function strncpy(), except that result string
497 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
498 * of the source string will be kept.
499 * Also, the macro returns no result (too hard to do that without
500 * evaluating the arguments multiple times, which seems worse).
502 * BTW: when you need to copy a non-null-terminated string (like a text
503 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
504 * might seem to work, but it fetches one byte more than there is in the
505 * text object. One fine day you'll have a SIGSEGV because there isn't
506 * another byte before the end of memory. Don't laugh, we've had real
507 * live bug reports from real live users over exactly this mistake.
508 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
510 #define StrNCpy(dst,src,len) \
513 char * _dst = (dst); \
518 strncpy(_dst, (src), _len); \
519 _dst[_len-1] = '\0'; \
524 /* Get a bit mask of the bits set in non-int32 aligned addresses */
525 #define INT_ALIGN_MASK (sizeof(int32) - 1)
529 * Exactly the same as standard library function memset(), but considerably
530 * faster for zeroing small word-aligned structures (such as parsetree nodes).
531 * This has to be a macro because the main point is to avoid function-call
534 * We got the 64 number by testing this against the stock memset() on
535 * BSD/OS 3.0. Larger values were slower. bjm 1997/09/11
537 * I think the crossover point could be a good deal higher for
538 * most platforms, actually. tgl 2000-03-19
540 #define MemSet(start, val, len) \
543 int32 * _start = (int32 *) (start); \
547 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
548 (_len & INT_ALIGN_MASK) == 0 && \
550 _len <= MEMSET_LOOP_LIMIT) \
552 int32 * _stop = (int32 *) ((char *) _start + _len); \
553 while (_start < _stop) \
557 memset((char *) _start, _val, _len); \
560 #define MEMSET_LOOP_LIMIT 64
563 /* ----------------------------------------------------------------
564 * Section 7: random stuff
565 * ----------------------------------------------------------------
569 #define CSIGNBIT (0x80)
571 #define STATUS_OK (0)
572 #define STATUS_ERROR (-1)
573 #define STATUS_NOT_FOUND (-2)
574 #define STATUS_INVALID (-3)
575 #define STATUS_UNCATALOGUED (-4)
576 #define STATUS_REPLACED (-5)
577 #define STATUS_NOT_DONE (-6)
578 #define STATUS_BAD_PACKET (-7)
579 #define STATUS_FOUND (1)
582 /* ----------------------------------------------------------------
583 * Section 8: system-specific hacks
585 * This should be limited to things that absolutely have to be
586 * included in every source file. The port-specific header file
587 * is usually a better place for this sort of thing.
588 * ----------------------------------------------------------------
592 #define PG_BINARY O_BINARY
593 #define PG_BINARY_R "rb"
594 #define PG_BINARY_W "wb"
597 #define PG_BINARY_R "r"
598 #define PG_BINARY_W "w"
601 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
605 /* These are for things that are one way on Unix and another on NT */
606 #define NULL_DEV "/dev/null"
608 /* defines for dynamic linking on Win32 platform */
610 #if __GNUC__ && ! defined (__declspec)
611 #error You need egcs 1.1 or newer for compiling!
614 #define DLLIMPORT __declspec (dllexport)
615 #else /* not BUILDING_DLL */
616 #define DLLIMPORT __declspec (dllimport)
618 #else /* not CYGWIN */
622 /* Provide prototypes for routines not present in a particular machine's
623 * standard C library. It'd be better to put these in config.h, but
624 * in config.h we haven't yet included anything that defines size_t...
627 #ifndef HAVE_SNPRINTF_DECL
628 extern int snprintf(char *str, size_t count, const char *fmt,...);
632 #ifndef HAVE_VSNPRINTF_DECL
633 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
637 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
638 #define memmove(d, s, c) bcopy(s, d, c)