]> granicus.if.org Git - postgresql/blob - src/include/c.h
Include locale.h before undefining gettext() to avoid compilation errors
[postgresql] / src / include / c.h
1 /*-------------------------------------------------------------------------
2  *
3  * c.h
4  *        Fundamental C definitions.  This is included by every .c file in
5  *        PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate).
6  *
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...
10  *
11  *
12  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * $Id: c.h,v 1.105 2001/10/24 21:49:14 petere Exp $
16  *
17  *-------------------------------------------------------------------------
18  */
19 /*
20  *----------------------------------------------------------------
21  *       TABLE OF CONTENTS
22  *
23  *              When adding stuff to this file, please try to put stuff
24  *              into the relevant section, or add new sections as appropriate.
25  *
26  *        section       description
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
35  *              7)              random stuff
36  *              8)              system-specific hacks
37  *
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.
41  *
42  *----------------------------------------------------------------
43  */
44 #ifndef C_H
45 #define C_H
46
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.
50 */
51
52 #include "pg_config.h"
53 #include "postgres_ext.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <stddef.h>
59 #include <stdarg.h>
60 #ifdef STRING_H_WITH_STRINGS_H
61 #include <strings.h>
62 #endif
63
64 #ifdef __CYGWIN__
65 #include <errno.h>
66 #include <sys/fcntl.h>                  /* ensure O_BINARY is available */
67 #endif
68 #ifdef HAVE_SUPPORTDEFS_H
69 #include <SupportDefs.h>
70 #endif
71
72 /* Must be before gettext() games below */
73 #include <locale.h>
74
75 #ifdef ENABLE_NLS
76 #include <libintl.h>
77 #else
78 #define gettext(x) (x)
79 #endif
80 #define gettext_noop(x) (x)
81
82
83 /* ----------------------------------------------------------------
84  *                              Section 1: hacks to cope with non-ANSI C compilers
85  *
86  * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
87  * ----------------------------------------------------------------
88  */
89
90 /*
91  * CppAsString
92  *              Convert the argument to a string, using the C preprocessor.
93  * CppConcat
94  *              Concatenate two arguments together, using the C preprocessor.
95  *
96  * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
97  * whether #identifier works, but if we have that we likely have ## too.
98  */
99 #if defined(HAVE_STRINGIZE)
100
101 #define CppAsString(identifier) #identifier
102 #define CppConcat(x, y)                 x##y
103
104 #else                                                   /* !HAVE_STRINGIZE */
105
106 #define CppAsString(identifier) "identifier"
107
108 /*
109  * CppIdentity -- On Reiser based cpp's this is used to concatenate
110  *              two tokens.  That is
111  *                              CppIdentity(A)B ==> AB
112  *              We renamed it to _private_CppIdentity because it should not
113  *              be referenced outside this file.  On other cpp's it
114  *              produces  A  B.
115  */
116 #define _priv_CppIdentity(x)x
117 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
118
119 #endif   /* !HAVE_STRINGIZE */
120
121 /*
122  * dummyret is used to set return values in macros that use ?: to make
123  * assignments.  gcc wants these to be void, other compilers like char
124  */
125 #ifdef __GNUC__                                 /* GNU cc */
126 #define dummyret        void
127 #else
128 #define dummyret        char
129 #endif
130
131 #ifndef __GNUC__
132 #define __attribute__(_arg_)
133 #endif
134
135 /* ----------------------------------------------------------------
136  *                              Section 2:      bool, true, false, TRUE, FALSE, NULL
137  * ----------------------------------------------------------------
138  */
139
140 /*
141  * bool
142  *              Boolean value, either true or false.
143  *
144  * XXX for C++ compilers, we assume the compiler has a compatible
145  * built-in definition of bool.
146  */
147
148 /* BeOS defines bool already, but the compiler chokes on the
149  * #ifndef unless we wrap it in this check.
150  */
151 #ifndef __BEOS__
152
153 #ifndef __cplusplus
154
155 #ifndef bool
156 typedef char bool;
157 #endif
158
159 #ifndef true
160 #define true    ((bool) 1)
161 #endif
162
163 #ifndef false
164 #define false   ((bool) 0)
165 #endif
166
167 #endif   /* not C++ */
168
169 #endif   /* __BEOS__ */
170
171 typedef bool *BoolPtr;
172
173 #ifndef TRUE
174 #define TRUE    1
175 #endif
176
177 #ifndef FALSE
178 #define FALSE   0
179 #endif
180
181 /*
182  * NULL
183  *              Null pointer.
184  */
185 #ifndef NULL
186 #define NULL    ((void *) 0)
187 #endif
188
189
190 /* ----------------------------------------------------------------
191  *                              Section 3:      standard system types
192  * ----------------------------------------------------------------
193  */
194
195 /*
196  * Pointer
197  *              Variable holding address of any memory resident object.
198  *
199  *              XXX Pointer arithmetic is done with this, so it can't be void *
200  *              under "true" ANSI compilers.
201  */
202 typedef char *Pointer;
203
204 /*
205  * intN
206  *              Signed integer, EXACTLY N BITS IN SIZE,
207  *              used for numerical computations and the
208  *              frontend/backend protocol.
209  */
210 #ifndef __BEOS__                                /* this shouldn't be required, but is is! */
211 typedef signed char int8;               /* == 8 bits */
212 typedef signed short int16;             /* == 16 bits */
213 typedef signed int int32;               /* == 32 bits */
214
215 #endif   /* __BEOS__ */
216
217 /*
218  * uintN
219  *              Unsigned integer, EXACTLY N BITS IN SIZE,
220  *              used for numerical computations and the
221  *              frontend/backend protocol.
222  */
223 #ifndef __BEOS__                                /* this shouldn't be required, but is is! */
224 typedef unsigned char uint8;    /* == 8 bits */
225 typedef unsigned short uint16;  /* == 16 bits */
226 typedef unsigned int uint32;    /* == 32 bits */
227
228 #endif   /* __BEOS__ */
229
230 /*
231  * boolN
232  *              Boolean value, AT LEAST N BITS IN SIZE.
233  */
234 typedef uint8 bool8;                    /* >= 8 bits */
235 typedef uint16 bool16;                  /* >= 16 bits */
236 typedef uint32 bool32;                  /* >= 32 bits */
237
238 /*
239  * bitsN
240  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
241  */
242 typedef uint8 bits8;                    /* >= 8 bits */
243 typedef uint16 bits16;                  /* >= 16 bits */
244 typedef uint32 bits32;                  /* >= 32 bits */
245
246 /*
247  * wordN
248  *              Unit of storage, AT LEAST N BITS IN SIZE,
249  *              used to fetch/store data.
250  */
251 typedef uint8 word8;                    /* >= 8 bits */
252 typedef uint16 word16;                  /* >= 16 bits */
253 typedef uint32 word32;                  /* >= 32 bits */
254
255 /*
256  * floatN
257  *              Floating point number, AT LEAST N BITS IN SIZE,
258  *              used for numerical computations.
259  *
260  *              Since sizeof(floatN) may be > sizeof(char *), always pass
261  *              floatN by reference.
262  *
263  * XXX: these typedefs are now deprecated in favor of float4 and float8.
264  * They will eventually go away.
265  */
266 typedef float float32data;
267 typedef double float64data;
268 typedef float *float32;
269 typedef double *float64;
270
271 /*
272  * 64-bit integers
273  */
274 #ifndef __BEOS__                                /* this is already defined on BeOS */
275 #ifdef HAVE_LONG_INT_64
276 /* Plain "long int" fits, use it */
277 typedef long int int64;
278 typedef unsigned long int uint64;
279
280 #else
281 #ifdef HAVE_LONG_LONG_INT_64
282 /* We have working support for "long long int", use that */
283 typedef long long int int64;
284 typedef unsigned long long int uint64;
285
286 #else
287 /* Won't actually work, but fall back to long int so that code compiles */
288 typedef long int int64;
289 typedef unsigned long int uint64;
290
291 #define INT64_IS_BUSTED
292 #endif
293 #endif
294 #endif   /* __BEOS__ */
295
296 /*
297  * Size
298  *              Size of any memory resident object, as returned by sizeof.
299  */
300 typedef size_t Size;
301
302 /*
303  * Index
304  *              Index into any memory resident array.
305  *
306  * Note:
307  *              Indices are non negative.
308  */
309 typedef unsigned int Index;
310
311 /*
312  * Offset
313  *              Offset into any memory resident array.
314  *
315  * Note:
316  *              This differs from an Index in that an Index is always
317  *              non negative, whereas Offset may be negative.
318  */
319 typedef signed int Offset;
320
321 /*
322  * Common Postgres datatype names (as used in the catalogs)
323  */
324 typedef int16 int2;
325 typedef int32 int4;
326 typedef float float4;
327 typedef double float8;
328
329 /*
330  * Oid, RegProcedure, TransactionId, CommandId
331  */
332
333 /* typedef Oid is in postgres_ext.h */
334
335 /* unfortunately, both regproc and RegProcedure are used */
336 typedef Oid regproc;
337 typedef Oid RegProcedure;
338
339 typedef uint32 TransactionId;
340
341 typedef uint32 CommandId;
342
343 #define FirstCommandId  ((CommandId) 0)
344
345 /*
346  * Array indexing support
347  */
348 #define MAXDIM 6
349 typedef struct
350 {
351         int                     indx[MAXDIM];
352 } IntArray;
353
354 /* ----------------
355  *              Variable-length datatypes all share the 'struct varlena' header.
356  *
357  * NOTE: for TOASTable types, this is an oversimplification, since the value may be
358  * compressed or moved out-of-line.  However datatype-specific routines are mostly
359  * content to deal with de-TOASTed values only, and of course client-side routines
360  * should never see a TOASTed value.  See postgres.h for details of the TOASTed form.
361  * ----------------
362  */
363 struct varlena
364 {
365         int32           vl_len;
366         char            vl_dat[1];
367 };
368
369 #define VARHDRSZ                ((int32) sizeof(int32))
370
371 /*
372  * These widely-used datatypes are just a varlena header and the data bytes.
373  * There is no terminating null or anything like that --- the data length is
374  * always VARSIZE(ptr) - VARHDRSZ.
375  */
376 typedef struct varlena bytea;
377 typedef struct varlena text;
378 typedef struct varlena BpChar;  /* blank-padded char, ie SQL char(n) */
379 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
380
381 /*
382  * Fixed-length array types (these are not varlena's!)
383  */
384
385 typedef int2 int2vector[INDEX_MAX_KEYS];
386 typedef Oid oidvector[INDEX_MAX_KEYS];
387
388 /*
389  * We want NameData to have length NAMEDATALEN and int alignment,
390  * because that's how the data type 'name' is defined in pg_type.
391  * Use a union to make sure the compiler agrees.
392  */
393 typedef union nameData
394 {
395         char            data[NAMEDATALEN];
396         int                     alignmentDummy;
397 } NameData;
398 typedef NameData *Name;
399
400 #define NameStr(name)   ((name).data)
401
402
403 /* ----------------------------------------------------------------
404  *                              Section 4:      IsValid macros for system types
405  * ----------------------------------------------------------------
406  */
407 /*
408  * BoolIsValid
409  *              True iff bool is valid.
410  */
411 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
412
413 /*
414  * PointerIsValid
415  *              True iff pointer is valid.
416  */
417 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
418
419 /*
420  * PointerIsAligned
421  *              True iff pointer is properly aligned to point to the given type.
422  */
423 #define PointerIsAligned(pointer, type) \
424                 (((long)(pointer) % (sizeof (type))) == 0)
425
426 #define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
427
428 #define RegProcedureIsValid(p)  OidIsValid(p)
429
430
431 /* ----------------------------------------------------------------
432  *                              Section 5:      offsetof, lengthof, endof, alignment
433  * ----------------------------------------------------------------
434  */
435 /*
436  * offsetof
437  *              Offset of a structure/union field within that structure/union.
438  *
439  *              XXX This is supposed to be part of stddef.h, but isn't on
440  *              some systems (like SunOS 4).
441  */
442 #ifndef offsetof
443 #define offsetof(type, field)   ((long) &((type *)0)->field)
444 #endif   /* offsetof */
445
446 /*
447  * lengthof
448  *              Number of elements in an array.
449  */
450 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
451
452 /*
453  * endof
454  *              Address of the element one past the last in an array.
455  */
456 #define endof(array)    (&array[lengthof(array)])
457
458 /* ----------------
459  * Alignment macros: align a length or address appropriately for a given type.
460  *
461  * There used to be some incredibly crufty platform-dependent hackery here,
462  * but now we rely on the configure script to get the info for us. Much nicer.
463  *
464  * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
465  * That case seems extremely unlikely to occur in practice, however.
466  * ----------------
467  */
468
469 #define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
470
471 #define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
472 #define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
473 #define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
474 #define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
475 #define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
476
477
478 /* ----------------------------------------------------------------
479  *                              Section 6:      widely useful macros
480  * ----------------------------------------------------------------
481  */
482 /*
483  * Max
484  *              Return the maximum of two numbers.
485  */
486 #define Max(x, y)               ((x) > (y) ? (x) : (y))
487
488 /*
489  * Min
490  *              Return the minimum of two numbers.
491  */
492 #define Min(x, y)               ((x) < (y) ? (x) : (y))
493
494 /*
495  * Abs
496  *              Return the absolute value of the argument.
497  */
498 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
499
500 /*
501  * StrNCpy
502  *      Like standard library function strncpy(), except that result string
503  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
504  *      of the source string will be kept.
505  *      Also, the macro returns no result (too hard to do that without
506  *      evaluating the arguments multiple times, which seems worse).
507  *
508  *      BTW: when you need to copy a non-null-terminated string (like a text
509  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
510  *      might seem to work, but it fetches one byte more than there is in the
511  *      text object.  One fine day you'll have a SIGSEGV because there isn't
512  *      another byte before the end of memory.  Don't laugh, we've had real
513  *      live bug reports from real live users over exactly this mistake.
514  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
515  */
516 #define StrNCpy(dst,src,len) \
517         do \
518         { \
519                 char * _dst = (dst); \
520                 Size _len = (len); \
521 \
522                 if (_len > 0) \
523                 { \
524                         strncpy(_dst, (src), _len); \
525                         _dst[_len-1] = '\0'; \
526                 } \
527         } while (0)
528
529
530 /* Get a bit mask of the bits set in non-int32 aligned addresses */
531 #define INT_ALIGN_MASK (sizeof(int32) - 1)
532
533 /*
534  * MemSet
535  *      Exactly the same as standard library function memset(), but considerably
536  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
537  *      This has to be a macro because the main point is to avoid function-call
538  *      overhead.
539  *
540  *      We got the 64 number by testing this against the stock memset() on
541  *      BSD/OS 3.0. Larger values were slower.  bjm 1997/09/11
542  *
543  *      I think the crossover point could be a good deal higher for
544  *      most platforms, actually.  tgl 2000-03-19
545  */
546 #define MemSet(start, val, len) \
547         do \
548         { \
549                 int32 * _start = (int32 *) (start); \
550                 int             _val = (val); \
551                 Size    _len = (len); \
552 \
553                 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
554                         (_len & INT_ALIGN_MASK) == 0 && \
555                         _val == 0 && \
556                         _len <= MEMSET_LOOP_LIMIT) \
557                 { \
558                         int32 * _stop = (int32 *) ((char *) _start + _len); \
559                         while (_start < _stop) \
560                                 *_start++ = 0; \
561                 } \
562                 else \
563                         memset((char *) _start, _val, _len); \
564         } while (0)
565
566 #define MEMSET_LOOP_LIMIT  64
567
568
569 /* ----------------------------------------------------------------
570  *                              Section 7:      random stuff
571  * ----------------------------------------------------------------
572  */
573
574 /* msb for char */
575 #define CSIGNBIT (0x80)
576
577 #define STATUS_OK                               (0)
578 #define STATUS_ERROR                    (-1)
579 #define STATUS_EOF                              (-2)
580 #define STATUS_FOUND                    (1)
581
582
583 /* ----------------------------------------------------------------
584  *                              Section 8: system-specific hacks
585  *
586  *              This should be limited to things that absolutely have to be
587  *              included in every source file.  The port-specific header file
588  *              is usually a better place for this sort of thing.
589  * ----------------------------------------------------------------
590  */
591
592 #ifdef __CYGWIN__
593 #define PG_BINARY       O_BINARY
594 #define PG_BINARY_R "rb"
595 #define PG_BINARY_W "wb"
596 #else
597 #define PG_BINARY       0
598 #define PG_BINARY_R "r"
599 #define PG_BINARY_W "w"
600 #endif
601
602 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
603 #include <unistd.h>
604 #endif
605
606 /* These are for things that are one way on Unix and another on NT */
607 #define NULL_DEV                "/dev/null"
608
609 /* defines for dynamic linking on Win32 platform */
610 #ifdef __CYGWIN__
611 #if __GNUC__ && ! defined (__declspec)
612 #error You need egcs 1.1 or newer for compiling!
613 #endif
614 #ifdef BUILDING_DLL
615 #define DLLIMPORT __declspec (dllexport)
616 #else                                                   /* not BUILDING_DLL */
617 #define DLLIMPORT __declspec (dllimport)
618 #endif
619 #elif defined(WIN32) && defined(_MSC_VER)       /* not CYGWIN */
620 #if defined(_DLL)
621 #define DLLIMPORT __declspec (dllexport)
622 #else                                                   /* not _DLL */
623 #define DLLIMPORT __declspec (dllimport)
624 #endif
625 #else                                                   /* not CYGWIN, not MSVC */
626 #define DLLIMPORT
627 #endif
628
629 /* Provide prototypes for routines not present in a particular machine's
630  * standard C library.  It'd be better to put these in pg_config.h, but
631  * in pg_config.h we haven't yet included anything that defines size_t...
632  */
633
634 #ifndef HAVE_SNPRINTF_DECL
635 extern int      snprintf(char *str, size_t count, const char *fmt, ...)
636 /* This extension allows gcc to check the format string */
637 __attribute__((format(printf, 3, 4)));
638 #endif
639
640 #ifndef HAVE_VSNPRINTF_DECL
641 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
642 #endif
643
644 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
645 #define memmove(d, s, c)                bcopy(s, d, c)
646 #endif
647
648 /* ----------------
649  *              end of c.h
650  * ----------------
651  */
652 #endif   /* C_H */