OSDN Git Service

More message munging and localization for pg_dump, especially the
[pg-rex/syncrep.git] / 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.96 2001/07/03 20:21:50 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)              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 "config.h"
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stddef.h>
58 #include <stdarg.h>
59 #ifdef STRING_H_WITH_STRINGS_H
60 #include <strings.h>
61 #endif
62
63 #ifdef __CYGWIN__
64 #include <errno.h>
65 #include <sys/fcntl.h>                  /* ensure O_BINARY is available */
66 #endif
67 #ifdef HAVE_SUPPORTDEFS_H
68 #include <SupportDefs.h>
69 #endif
70
71 #ifdef ENABLE_NLS
72 #include <libintl.h>
73 #else
74 #define gettext(x) (x)
75 #endif
76 #define gettext_noop(x) (x)
77
78
79 /* ----------------------------------------------------------------
80  *                              Section 1: hacks to cope with non-ANSI C compilers
81  *
82  * type prefixes (const, signed, volatile, inline) are now handled in config.h.
83  * ----------------------------------------------------------------
84  */
85
86 /*
87  * CppAsString
88  *              Convert the argument to a string, using the C preprocessor.
89  * CppConcat
90  *              Concatenate two arguments together, using the C preprocessor.
91  *
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.
94  */
95 #if defined(HAVE_STRINGIZE)
96
97 #define CppAsString(identifier) #identifier
98 #define CppConcat(x, y)                 x##y
99
100 #else                                                   /* !HAVE_STRINGIZE */
101
102 #define CppAsString(identifier) "identifier"
103
104 /*
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
110  *              produces  A  B.
111  */
112 #define _priv_CppIdentity(x)x
113 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
114
115 #endif   /* !HAVE_STRINGIZE */
116
117 /*
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
120  */
121 #ifdef __GNUC__                                 /* GNU cc */
122 #define dummyret        void
123 #else
124 #define dummyret        char
125 #endif
126
127 #ifndef __GNUC__
128 #define __attribute__(x)
129 #endif
130
131 /* ----------------------------------------------------------------
132  *                              Section 2:      bool, true, false, TRUE, FALSE, NULL
133  * ----------------------------------------------------------------
134  */
135 /*
136  * bool
137  *              Boolean value, either true or false.
138  *
139  */
140
141 /* BeOS defines bool already, but the compiler chokes on the
142  * #ifndef unless we wrap it in this check.
143  */
144 #ifndef __BEOS__
145
146 #ifndef __cplusplus
147 #ifndef bool
148 typedef char bool;
149
150 #endif   /* ndef bool */
151 #endif   /* not C++ */
152
153 #ifndef true
154 #define true    ((bool) 1)
155 #endif
156
157 #ifndef false
158 #define false   ((bool) 0)
159 #endif
160
161 #endif   /* __BEOS__ */
162
163 typedef bool *BoolPtr;
164
165 #ifndef TRUE
166 #define TRUE    1
167 #endif
168
169 #ifndef FALSE
170 #define FALSE   0
171 #endif
172
173 /*
174  * NULL
175  *              Null pointer.
176  */
177 #ifndef NULL
178 #define NULL    ((void *) 0)
179 #endif
180
181
182 /* ----------------------------------------------------------------
183  *                              Section 3:      standard system types
184  * ----------------------------------------------------------------
185  */
186
187 /*
188  * Pointer
189  *              Variable holding address of any memory resident object.
190  *
191  *              XXX Pointer arithmetic is done with this, so it can't be void *
192  *              under "true" ANSI compilers.
193  */
194 typedef char *Pointer;
195
196 /*
197  * intN
198  *              Signed integer, EXACTLY N BITS IN SIZE,
199  *              used for numerical computations and the
200  *              frontend/backend protocol.
201  */
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 */
206
207 #endif   /* __BEOS__ */
208
209 /*
210  * uintN
211  *              Unsigned integer, EXACTLY N BITS IN SIZE,
212  *              used for numerical computations and the
213  *              frontend/backend protocol.
214  */
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 */
219
220 #endif   /* __BEOS__ */
221
222 /*
223  * boolN
224  *              Boolean value, AT LEAST N BITS IN SIZE.
225  */
226 typedef uint8 bool8;                    /* >= 8 bits */
227 typedef uint16 bool16;                  /* >= 16 bits */
228 typedef uint32 bool32;                  /* >= 32 bits */
229
230 /*
231  * bitsN
232  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
233  */
234 typedef uint8 bits8;                    /* >= 8 bits */
235 typedef uint16 bits16;                  /* >= 16 bits */
236 typedef uint32 bits32;                  /* >= 32 bits */
237
238 /*
239  * wordN
240  *              Unit of storage, AT LEAST N BITS IN SIZE,
241  *              used to fetch/store data.
242  */
243 typedef uint8 word8;                    /* >= 8 bits */
244 typedef uint16 word16;                  /* >= 16 bits */
245 typedef uint32 word32;                  /* >= 32 bits */
246
247 /*
248  * floatN
249  *              Floating point number, AT LEAST N BITS IN SIZE,
250  *              used for numerical computations.
251  *
252  *              Since sizeof(floatN) may be > sizeof(char *), always pass
253  *              floatN by reference.
254  *
255  * XXX: these typedefs are now deprecated in favor of float4 and float8.
256  * They will eventually go away.
257  */
258 typedef float float32data;
259 typedef double float64data;
260 typedef float *float32;
261 typedef double *float64;
262
263 /*
264  * 64-bit integers
265  */
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;
271
272 #else
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;
277
278 #else
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;
282
283 #define INT64_IS_BUSTED
284 #endif
285 #endif
286 #endif   /* __BEOS__ */
287
288 /*
289  * Size
290  *              Size of any memory resident object, as returned by sizeof.
291  */
292 typedef size_t Size;
293
294 /*
295  * Index
296  *              Index into any memory resident array.
297  *
298  * Note:
299  *              Indices are non negative.
300  */
301 typedef unsigned int Index;
302
303 /*
304  * Offset
305  *              Offset into any memory resident array.
306  *
307  * Note:
308  *              This differs from an Index in that an Index is always
309  *              non negative, whereas Offset may be negative.
310  */
311 typedef signed int Offset;
312
313 /*
314  * Common Postgres datatype names (as used in the catalogs)
315  */
316 typedef int16 int2;
317 typedef int32 int4;
318 typedef float float4;
319 typedef double float8;
320
321 /*
322  * Oid, RegProcedure, TransactionId, CommandId
323  */
324
325 /* typedef Oid is in postgres_ext.h */
326
327 /* unfortunately, both regproc and RegProcedure are used */
328 typedef Oid regproc;
329 typedef Oid RegProcedure;
330
331 typedef uint32 TransactionId;
332
333 #define InvalidTransactionId    0
334
335 typedef uint32 CommandId;
336
337 #define FirstCommandId  0
338
339 /*
340  * Array indexing support
341  */
342 #define MAXDIM 6
343 typedef struct
344 {
345         int                     indx[MAXDIM];
346 } IntArray;
347
348 /* ----------------
349  *              Variable-length datatypes all share the 'struct varlena' header.
350  *
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.
355  * ----------------
356  */
357 struct varlena
358 {
359         int32           vl_len;
360         char            vl_dat[1];
361 };
362
363 #define VARHDRSZ                ((int32) sizeof(int32))
364
365 /*
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.
369  */
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) */
374
375 /*
376  * Fixed-length array types (these are not varlena's!)
377  */
378
379 typedef int2 int2vector[INDEX_MAX_KEYS];
380 typedef Oid oidvector[INDEX_MAX_KEYS];
381
382 /*
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.
386  */
387 typedef union nameData
388 {
389         char            data[NAMEDATALEN];
390         int                     alignmentDummy;
391 } NameData;
392 typedef NameData *Name;
393
394 #define NameStr(name)   ((name).data)
395
396
397 /* ----------------------------------------------------------------
398  *                              Section 4:      IsValid macros for system types
399  * ----------------------------------------------------------------
400  */
401 /*
402  * BoolIsValid
403  *              True iff bool is valid.
404  */
405 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
406
407 /*
408  * PointerIsValid
409  *              True iff pointer is valid.
410  */
411 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
412
413 /*
414  * PointerIsAligned
415  *              True iff pointer is properly aligned to point to the given type.
416  */
417 #define PointerIsAligned(pointer, type) \
418                 (((long)(pointer) % (sizeof (type))) == 0)
419
420 #define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
421
422 #define RegProcedureIsValid(p)  OidIsValid(p)
423
424
425 /* ----------------------------------------------------------------
426  *                              Section 5:      offsetof, lengthof, endof, alignment
427  * ----------------------------------------------------------------
428  */
429 /*
430  * offsetof
431  *              Offset of a structure/union field within that structure/union.
432  *
433  *              XXX This is supposed to be part of stddef.h, but isn't on
434  *              some systems (like SunOS 4).
435  */
436 #ifndef offsetof
437 #define offsetof(type, field)   ((long) &((type *)0)->field)
438 #endif   /* offsetof */
439
440 /*
441  * lengthof
442  *              Number of elements in an array.
443  */
444 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
445
446 /*
447  * endof
448  *              Address of the element one past the last in an array.
449  */
450 #define endof(array)    (&array[lengthof(array)])
451
452 /* ----------------
453  * Alignment macros: align a length or address appropriately for a given type.
454  *
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.
457  *
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.
460  * ----------------
461  */
462
463 #define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
464
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))
470
471
472 /* ----------------------------------------------------------------
473  *                              Section 6:      widely useful macros
474  * ----------------------------------------------------------------
475  */
476 /*
477  * Max
478  *              Return the maximum of two numbers.
479  */
480 #define Max(x, y)               ((x) > (y) ? (x) : (y))
481
482 /*
483  * Min
484  *              Return the minimum of two numbers.
485  */
486 #define Min(x, y)               ((x) < (y) ? (x) : (y))
487
488 /*
489  * Abs
490  *              Return the absolute value of the argument.
491  */
492 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
493
494 /*
495  * StrNCpy
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).
501  *
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.
509  */
510 #define StrNCpy(dst,src,len) \
511         do \
512         { \
513                 char * _dst = (dst); \
514                 Size _len = (len); \
515 \
516                 if (_len > 0) \
517                 { \
518                         strncpy(_dst, (src), _len); \
519                         _dst[_len-1] = '\0'; \
520                 } \
521         } while (0)
522
523
524 /* Get a bit mask of the bits set in non-int32 aligned addresses */
525 #define INT_ALIGN_MASK (sizeof(int32) - 1)
526
527 /*
528  * MemSet
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
532  *      overhead.
533  *
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
536  *
537  *      I think the crossover point could be a good deal higher for
538  *      most platforms, actually.  tgl 2000-03-19
539  */
540 #define MemSet(start, val, len) \
541         do \
542         { \
543                 int32 * _start = (int32 *) (start); \
544                 int             _val = (val); \
545                 Size    _len = (len); \
546 \
547                 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
548                         (_len & INT_ALIGN_MASK) == 0 && \
549                         _val == 0 && \
550                         _len <= MEMSET_LOOP_LIMIT) \
551                 { \
552                         int32 * _stop = (int32 *) ((char *) _start + _len); \
553                         while (_start < _stop) \
554                                 *_start++ = 0; \
555                 } \
556                 else \
557                         memset((char *) _start, _val, _len); \
558         } while (0)
559
560 #define MEMSET_LOOP_LIMIT  64
561
562
563 /* ----------------------------------------------------------------
564  *                              Section 7:      random stuff
565  * ----------------------------------------------------------------
566  */
567
568 /* msb for char */
569 #define CSIGNBIT (0x80)
570
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)
580
581
582 /* ----------------------------------------------------------------
583  *                              Section 8: system-specific hacks
584  *
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  * ----------------------------------------------------------------
589  */
590
591 #ifdef __CYGWIN__
592 #define PG_BINARY       O_BINARY
593 #define PG_BINARY_R "rb"
594 #define PG_BINARY_W "wb"
595 #else
596 #define PG_BINARY       0
597 #define PG_BINARY_R "r"
598 #define PG_BINARY_W "w"
599 #endif
600
601 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
602 #include <unistd.h>
603 #endif
604
605 /* These are for things that are one way on Unix and another on NT */
606 #define NULL_DEV                "/dev/null"
607
608 /* defines for dynamic linking on Win32 platform */
609 #ifdef __CYGWIN__
610 #if __GNUC__ && ! defined (__declspec)
611 #error You need egcs 1.1 or newer for compiling!
612 #endif
613 #ifdef BUILDING_DLL
614 #define DLLIMPORT __declspec (dllexport)
615 #else                                                   /* not BUILDING_DLL */
616 #define DLLIMPORT __declspec (dllimport)
617 #endif
618 #else                                                   /* not CYGWIN */
619 #define DLLIMPORT
620 #endif
621
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...
625  */
626
627 #ifndef HAVE_SNPRINTF_DECL
628 extern int      snprintf(char *str, size_t count, const char *fmt,...);
629
630 #endif
631
632 #ifndef HAVE_VSNPRINTF_DECL
633 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
634
635 #endif
636
637 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
638 #define memmove(d, s, c)                bcopy(s, d, c)
639 #endif
640
641 /* ----------------
642  *              end of c.h
643  * ----------------
644  */
645 #endif   /* C_H */