OSDN Git Service

Fix bug introduced by recent SSI patch to merge ROLLED_BACK and
[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
8  *        of the frontend interface libraries --- so we don't worry much about
9  *        polluting the namespace with lots of stuff...
10  *
11  *
12  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * src/include/c.h
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
40  * macros are the kind of thing that might go here.
41  *
42  *----------------------------------------------------------------
43  */
44 #ifndef C_H
45 #define C_H
46
47 /*
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.
51  */
52
53 #include "pg_config.h"
54 #include "pg_config_manual.h"   /* must be after pg_config.h */
55 #if !defined(WIN32) && !defined(__CYGWIN__)             /* win32 will include further
56                                                                                                  * down */
57 #include "pg_config_os.h"               /* must be before any system header files */
58 #endif
59 #include "postgres_ext.h"
60
61 #if _MSC_VER >= 1400 || defined(WIN64)
62 #define errcode __msvc_errcode
63 #include <crtdefs.h>
64 #undef errcode
65 #endif
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <stddef.h>
71 #include <stdarg.h>
72 #ifdef HAVE_STRINGS_H
73 #include <strings.h>
74 #endif
75 #ifdef HAVE_STDINT_H
76 #include <stdint.h>
77 #endif
78 #include <sys/types.h>
79
80 #include <errno.h>
81 #if defined(WIN32) || defined(__CYGWIN__)
82 #include <fcntl.h>                              /* ensure O_BINARY is available */
83 #endif
84 #ifdef HAVE_SUPPORTDEFS_H
85 #include <SupportDefs.h>
86 #endif
87
88 #if defined(WIN32) || defined(__CYGWIN__)
89 /* We have to redefine some system functions after they are included above. */
90 #include "pg_config_os.h"
91 #endif
92
93 /* Must be before gettext() games below */
94 #include <locale.h>
95
96 #define _(x) gettext(x)
97
98 #ifdef ENABLE_NLS
99 #include <libintl.h>
100 #else
101 #define gettext(x) (x)
102 #define dgettext(d,x) (x)
103 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
104 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
105 #endif
106
107 /*
108  *      Use this to mark string constants as needing translation at some later
109  *      time, rather than immediately.  This is useful for cases where you need
110  *      access to the original string and translated string, and for cases where
111  *      immediate translation is not possible, like when initializing global
112  *      variables.
113  *              http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
114  */
115 #define gettext_noop(x) (x)
116
117
118 /* ----------------------------------------------------------------
119  *                              Section 1: hacks to cope with non-ANSI C compilers
120  *
121  * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
122  * ----------------------------------------------------------------
123  */
124
125 /*
126  * CppAsString
127  *              Convert the argument to a string, using the C preprocessor.
128  * CppConcat
129  *              Concatenate two arguments together, using the C preprocessor.
130  *
131  * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
132  * whether #identifier works, but if we have that we likely have ## too.
133  */
134 #if defined(HAVE_STRINGIZE)
135
136 #define CppAsString(identifier) #identifier
137 #define CppConcat(x, y)                 x##y
138 #else                                                   /* !HAVE_STRINGIZE */
139
140 #define CppAsString(identifier) "identifier"
141
142 /*
143  * CppIdentity -- On Reiser based cpp's this is used to concatenate
144  *              two tokens.  That is
145  *                              CppIdentity(A)B ==> AB
146  *              We renamed it to _private_CppIdentity because it should not
147  *              be referenced outside this file.  On other cpp's it
148  *              produces  A  B.
149  */
150 #define _priv_CppIdentity(x)x
151 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
152 #endif   /* !HAVE_STRINGIZE */
153
154 /*
155  * dummyret is used to set return values in macros that use ?: to make
156  * assignments.  gcc wants these to be void, other compilers like char
157  */
158 #ifdef __GNUC__                                 /* GNU cc */
159 #define dummyret        void
160 #else
161 #define dummyret        char
162 #endif
163
164 #ifndef __GNUC__
165 #define __attribute__(_arg_)
166 #endif
167
168 /* ----------------------------------------------------------------
169  *                              Section 2:      bool, true, false, TRUE, FALSE, NULL
170  * ----------------------------------------------------------------
171  */
172
173 /*
174  * bool
175  *              Boolean value, either true or false.
176  *
177  * XXX for C++ compilers, we assume the compiler has a compatible
178  * built-in definition of bool.
179  */
180
181 #ifndef __cplusplus
182
183 #ifndef bool
184 typedef char bool;
185 #endif
186
187 #ifndef true
188 #define true    ((bool) 1)
189 #endif
190
191 #ifndef false
192 #define false   ((bool) 0)
193 #endif
194 #endif   /* not C++ */
195
196 typedef bool *BoolPtr;
197
198 #ifndef TRUE
199 #define TRUE    1
200 #endif
201
202 #ifndef FALSE
203 #define FALSE   0
204 #endif
205
206 /*
207  * NULL
208  *              Null pointer.
209  */
210 #ifndef NULL
211 #define NULL    ((void *) 0)
212 #endif
213
214
215 /* ----------------------------------------------------------------
216  *                              Section 3:      standard system types
217  * ----------------------------------------------------------------
218  */
219
220 /*
221  * Pointer
222  *              Variable holding address of any memory resident object.
223  *
224  *              XXX Pointer arithmetic is done with this, so it can't be void *
225  *              under "true" ANSI compilers.
226  */
227 typedef char *Pointer;
228
229 /*
230  * intN
231  *              Signed integer, EXACTLY N BITS IN SIZE,
232  *              used for numerical computations and the
233  *              frontend/backend protocol.
234  */
235 #ifndef HAVE_INT8
236 typedef signed char int8;               /* == 8 bits */
237 typedef signed short int16;             /* == 16 bits */
238 typedef signed int int32;               /* == 32 bits */
239 #endif   /* not HAVE_INT8 */
240
241 /*
242  * uintN
243  *              Unsigned integer, EXACTLY N BITS IN SIZE,
244  *              used for numerical computations and the
245  *              frontend/backend protocol.
246  */
247 #ifndef HAVE_UINT8
248 typedef unsigned char uint8;    /* == 8 bits */
249 typedef unsigned short uint16;  /* == 16 bits */
250 typedef unsigned int uint32;    /* == 32 bits */
251 #endif   /* not HAVE_UINT8 */
252
253 /*
254  * bitsN
255  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
256  */
257 typedef uint8 bits8;                    /* >= 8 bits */
258 typedef uint16 bits16;                  /* >= 16 bits */
259 typedef uint32 bits32;                  /* >= 32 bits */
260
261 /*
262  * 64-bit integers
263  */
264 #ifdef HAVE_LONG_INT_64
265 /* Plain "long int" fits, use it */
266
267 #ifndef HAVE_INT64
268 typedef long int int64;
269 #endif
270 #ifndef HAVE_UINT64
271 typedef unsigned long int uint64;
272 #endif
273 #elif defined(HAVE_LONG_LONG_INT_64)
274 /* We have working support for "long long int", use that */
275
276 #ifndef HAVE_INT64
277 typedef long long int int64;
278 #endif
279 #ifndef HAVE_UINT64
280 typedef unsigned long long int uint64;
281 #endif
282 #else
283 /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
284 #error must have a working 64-bit integer datatype
285 #endif
286
287 /* Decide if we need to decorate 64-bit constants */
288 #ifdef HAVE_LL_CONSTANTS
289 #define INT64CONST(x)  ((int64) x##LL)
290 #define UINT64CONST(x) ((uint64) x##ULL)
291 #else
292 #define INT64CONST(x)  ((int64) x)
293 #define UINT64CONST(x) ((uint64) x)
294 #endif
295
296
297 /* Select timestamp representation (float8 or int64) */
298 #ifdef USE_INTEGER_DATETIMES
299 #define HAVE_INT64_TIMESTAMP
300 #endif
301
302 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
303 #ifndef HAVE_SIG_ATOMIC_T
304 typedef int sig_atomic_t;
305 #endif
306
307 /*
308  * Size
309  *              Size of any memory resident object, as returned by sizeof.
310  */
311 typedef size_t Size;
312
313 /*
314  * Index
315  *              Index into any memory resident array.
316  *
317  * Note:
318  *              Indices are non negative.
319  */
320 typedef unsigned int Index;
321
322 /*
323  * Offset
324  *              Offset into any memory resident array.
325  *
326  * Note:
327  *              This differs from an Index in that an Index is always
328  *              non negative, whereas Offset may be negative.
329  */
330 typedef signed int Offset;
331
332 /*
333  * Common Postgres datatype names (as used in the catalogs)
334  */
335 typedef int16 int2;
336 typedef int32 int4;
337 typedef float float4;
338 typedef double float8;
339
340 /*
341  * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
342  * CommandId
343  */
344
345 /* typedef Oid is in postgres_ext.h */
346
347 /*
348  * regproc is the type name used in the include/catalog headers, but
349  * RegProcedure is the preferred name in C code.
350  */
351 typedef Oid regproc;
352 typedef regproc RegProcedure;
353
354 typedef uint32 TransactionId;
355
356 typedef uint32 LocalTransactionId;
357
358 typedef uint32 SubTransactionId;
359
360 #define InvalidSubTransactionId         ((SubTransactionId) 0)
361 #define TopSubTransactionId                     ((SubTransactionId) 1)
362
363 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
364 typedef TransactionId MultiXactId;
365
366 typedef uint32 MultiXactOffset;
367
368 typedef uint32 CommandId;
369
370 #define FirstCommandId  ((CommandId) 0)
371
372 /*
373  * Array indexing support
374  */
375 #define MAXDIM 6
376 typedef struct
377 {
378         int                     indx[MAXDIM];
379 } IntArray;
380
381 /* ----------------
382  *              Variable-length datatypes all share the 'struct varlena' header.
383  *
384  * NOTE: for TOASTable types, this is an oversimplification, since the value
385  * may be compressed or moved out-of-line.      However datatype-specific routines
386  * are mostly content to deal with de-TOASTed values only, and of course
387  * client-side routines should never see a TOASTed value.  But even in a
388  * de-TOASTed value, beware of touching vl_len_ directly, as its representation
389  * is no longer convenient.  It's recommended that code always use the VARDATA,
390  * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of
391  * the struct fields.  See postgres.h for details of the TOASTed form.
392  * ----------------
393  */
394 struct varlena
395 {
396         char            vl_len_[4];             /* Do not touch this field directly! */
397         char            vl_dat[1];
398 };
399
400 #define VARHDRSZ                ((int32) sizeof(int32))
401
402 /*
403  * These widely-used datatypes are just a varlena header and the data bytes.
404  * There is no terminating null or anything like that --- the data length is
405  * always VARSIZE(ptr) - VARHDRSZ.
406  */
407 typedef struct varlena bytea;
408 typedef struct varlena text;
409 typedef struct varlena BpChar;  /* blank-padded char, ie SQL char(n) */
410 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
411
412 /*
413  * Specialized array types.  These are physically laid out just the same
414  * as regular arrays (so that the regular array subscripting code works
415  * with them).  They exist as distinct types mostly for historical reasons:
416  * they have nonstandard I/O behavior which we don't want to change for fear
417  * of breaking applications that look at the system catalogs.  There is also
418  * an implementation issue for oidvector: it's part of the primary key for
419  * pg_proc, and we can't use the normal btree array support routines for that
420  * without circularity.
421  */
422 typedef struct
423 {
424         int32           vl_len_;                /* these fields must match ArrayType! */
425         int                     ndim;                   /* always 1 for int2vector */
426         int32           dataoffset;             /* always 0 for int2vector */
427         Oid                     elemtype;
428         int                     dim1;
429         int                     lbound1;
430         int2            values[1];              /* VARIABLE LENGTH ARRAY */
431 } int2vector;                                   /* VARIABLE LENGTH STRUCT */
432
433 typedef struct
434 {
435         int32           vl_len_;                /* these fields must match ArrayType! */
436         int                     ndim;                   /* always 1 for oidvector */
437         int32           dataoffset;             /* always 0 for oidvector */
438         Oid                     elemtype;
439         int                     dim1;
440         int                     lbound1;
441         Oid                     values[1];              /* VARIABLE LENGTH ARRAY */
442 } oidvector;                                    /* VARIABLE LENGTH STRUCT */
443
444 /*
445  * Representation of a Name: effectively just a C string, but null-padded to
446  * exactly NAMEDATALEN bytes.  The use of a struct is historical.
447  */
448 typedef struct nameData
449 {
450         char            data[NAMEDATALEN];
451 } NameData;
452 typedef NameData *Name;
453
454 #define NameStr(name)   ((name).data)
455
456 /*
457  * Support macros for escaping strings.  escape_backslash should be TRUE
458  * if generating a non-standard-conforming string.      Prefixing a string
459  * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
460  * Beware of multiple evaluation of the "ch" argument!
461  */
462 #define SQL_STR_DOUBLE(ch, escape_backslash)    \
463         ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
464
465 #define ESCAPE_STRING_SYNTAX    'E'
466
467 /* ----------------------------------------------------------------
468  *                              Section 4:      IsValid macros for system types
469  * ----------------------------------------------------------------
470  */
471 /*
472  * BoolIsValid
473  *              True iff bool is valid.
474  */
475 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
476
477 /*
478  * PointerIsValid
479  *              True iff pointer is valid.
480  */
481 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
482
483 /*
484  * PointerIsAligned
485  *              True iff pointer is properly aligned to point to the given type.
486  */
487 #define PointerIsAligned(pointer, type) \
488                 (((intptr_t)(pointer) % (sizeof (type))) == 0)
489
490 #define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
491
492 #define RegProcedureIsValid(p)  OidIsValid(p)
493
494
495 /* ----------------------------------------------------------------
496  *                              Section 5:      offsetof, lengthof, endof, alignment
497  * ----------------------------------------------------------------
498  */
499 /*
500  * offsetof
501  *              Offset of a structure/union field within that structure/union.
502  *
503  *              XXX This is supposed to be part of stddef.h, but isn't on
504  *              some systems (like SunOS 4).
505  */
506 #ifndef offsetof
507 #define offsetof(type, field)   ((long) &((type *)0)->field)
508 #endif   /* offsetof */
509
510 /*
511  * lengthof
512  *              Number of elements in an array.
513  */
514 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
515
516 /*
517  * endof
518  *              Address of the element one past the last in an array.
519  */
520 #define endof(array)    (&(array)[lengthof(array)])
521
522 /* ----------------
523  * Alignment macros: align a length or address appropriately for a given type.
524  * The fooALIGN() macros round up to a multiple of the required alignment,
525  * while the fooALIGN_DOWN() macros round down.  The latter are more useful
526  * for problems like "how many X-sized structures will fit in a page?".
527  *
528  * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
529  * That case seems extremely unlikely to be needed in practice, however.
530  * ----------------
531  */
532
533 #define TYPEALIGN(ALIGNVAL,LEN)  \
534         (((intptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((intptr_t) ((ALIGNVAL) - 1)))
535
536 #define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
537 #define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
538 #define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
539 #define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
540 #define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
541 /* MAXALIGN covers only built-in types, not buffers */
542 #define BUFFERALIGN(LEN)                TYPEALIGN(ALIGNOF_BUFFER, (LEN))
543
544 #define TYPEALIGN_DOWN(ALIGNVAL,LEN)  \
545         (((intptr_t) (LEN)) & ~((intptr_t) ((ALIGNVAL) - 1)))
546
547 #define SHORTALIGN_DOWN(LEN)    TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
548 #define INTALIGN_DOWN(LEN)              TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
549 #define LONGALIGN_DOWN(LEN)             TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
550 #define DOUBLEALIGN_DOWN(LEN)   TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
551 #define MAXALIGN_DOWN(LEN)              TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
552
553 /* ----------------------------------------------------------------
554  *                              Section 6:      widely useful macros
555  * ----------------------------------------------------------------
556  */
557 /*
558  * Max
559  *              Return the maximum of two numbers.
560  */
561 #define Max(x, y)               ((x) > (y) ? (x) : (y))
562
563 /*
564  * Min
565  *              Return the minimum of two numbers.
566  */
567 #define Min(x, y)               ((x) < (y) ? (x) : (y))
568
569 /*
570  * Abs
571  *              Return the absolute value of the argument.
572  */
573 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
574
575 /*
576  * StrNCpy
577  *      Like standard library function strncpy(), except that result string
578  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
579  *      of the source string will be kept.
580  *      Also, the macro returns no result (too hard to do that without
581  *      evaluating the arguments multiple times, which seems worse).
582  *
583  *      BTW: when you need to copy a non-null-terminated string (like a text
584  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
585  *      might seem to work, but it fetches one byte more than there is in the
586  *      text object.  One fine day you'll have a SIGSEGV because there isn't
587  *      another byte before the end of memory.  Don't laugh, we've had real
588  *      live bug reports from real live users over exactly this mistake.
589  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
590  */
591 #define StrNCpy(dst,src,len) \
592         do \
593         { \
594                 char * _dst = (dst); \
595                 Size _len = (len); \
596 \
597                 if (_len > 0) \
598                 { \
599                         strncpy(_dst, (src), _len); \
600                         _dst[_len-1] = '\0'; \
601                 } \
602         } while (0)
603
604
605 /* Get a bit mask of the bits set in non-long aligned addresses */
606 #define LONG_ALIGN_MASK (sizeof(long) - 1)
607
608 /*
609  * MemSet
610  *      Exactly the same as standard library function memset(), but considerably
611  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
612  *      This has to be a macro because the main point is to avoid function-call
613  *      overhead.       However, we have also found that the loop is faster than
614  *      native libc memset() on some platforms, even those with assembler
615  *      memset() functions.  More research needs to be done, perhaps with
616  *      MEMSET_LOOP_LIMIT tests in configure.
617  */
618 #define MemSet(start, val, len) \
619         do \
620         { \
621                 /* must be void* because we don't know if it is integer aligned yet */ \
622                 void   *_vstart = (void *) (start); \
623                 int             _val = (val); \
624                 Size    _len = (len); \
625 \
626                 if ((((intptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
627                         (_len & LONG_ALIGN_MASK) == 0 && \
628                         _val == 0 && \
629                         _len <= MEMSET_LOOP_LIMIT && \
630                         /* \
631                          *      If MEMSET_LOOP_LIMIT == 0, optimizer should find \
632                          *      the whole "if" false at compile time. \
633                          */ \
634                         MEMSET_LOOP_LIMIT != 0) \
635                 { \
636                         long *_start = (long *) _vstart; \
637                         long *_stop = (long *) ((char *) _start + _len); \
638                         while (_start < _stop) \
639                                 *_start++ = 0; \
640                 } \
641                 else \
642                         memset(_vstart, _val, _len); \
643         } while (0)
644
645 /*
646  * MemSetAligned is the same as MemSet except it omits the test to see if
647  * "start" is word-aligned.  This is okay to use if the caller knows a-priori
648  * that the pointer is suitably aligned (typically, because he just got it
649  * from palloc(), which always delivers a max-aligned pointer).
650  */
651 #define MemSetAligned(start, val, len) \
652         do \
653         { \
654                 long   *_start = (long *) (start); \
655                 int             _val = (val); \
656                 Size    _len = (len); \
657 \
658                 if ((_len & LONG_ALIGN_MASK) == 0 && \
659                         _val == 0 && \
660                         _len <= MEMSET_LOOP_LIMIT && \
661                         MEMSET_LOOP_LIMIT != 0) \
662                 { \
663                         long *_stop = (long *) ((char *) _start + _len); \
664                         while (_start < _stop) \
665                                 *_start++ = 0; \
666                 } \
667                 else \
668                         memset(_start, _val, _len); \
669         } while (0)
670
671
672 /*
673  * MemSetTest/MemSetLoop are a variant version that allow all the tests in
674  * MemSet to be done at compile time in cases where "val" and "len" are
675  * constants *and* we know the "start" pointer must be word-aligned.
676  * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
677  * MemSetAligned.  Beware of multiple evaluations of the arguments when using
678  * this approach.
679  */
680 #define MemSetTest(val, len) \
681         ( ((len) & LONG_ALIGN_MASK) == 0 && \
682         (len) <= MEMSET_LOOP_LIMIT && \
683         MEMSET_LOOP_LIMIT != 0 && \
684         (val) == 0 )
685
686 #define MemSetLoop(start, val, len) \
687         do \
688         { \
689                 long * _start = (long *) (start); \
690                 long * _stop = (long *) ((char *) _start + (Size) (len)); \
691         \
692                 while (_start < _stop) \
693                         *_start++ = 0; \
694         } while (0)
695
696
697 /* ----------------------------------------------------------------
698  *                              Section 7:      random stuff
699  * ----------------------------------------------------------------
700  */
701
702 /* msb for char */
703 #define HIGHBIT                                 (0x80)
704 #define IS_HIGHBIT_SET(ch)              ((unsigned char)(ch) & HIGHBIT)
705
706 #define STATUS_OK                               (0)
707 #define STATUS_ERROR                    (-1)
708 #define STATUS_EOF                              (-2)
709 #define STATUS_FOUND                    (1)
710 #define STATUS_WAITING                  (2)
711
712
713 /* gettext domain name mangling */
714
715 /*
716  * To better support parallel installations of major PostgeSQL
717  * versions as well as parallel installations of major library soname
718  * versions, we mangle the gettext domain name by appending those
719  * version numbers.  The coding rule ought to be that whereever the
720  * domain name is mentioned as a literal, it must be wrapped into
721  * PG_TEXTDOMAIN().  The macros below do not work on non-literals; but
722  * that is somewhat intentional because it avoids having to worry
723  * about multiple states of premangling and postmangling as the values
724  * are being passed around.
725  *
726  * Make sure this matches the installation rules in nls-global.mk.
727  */
728
729 /* need a second indirection because we want to stringize the macro value, not the name */
730 #define CppAsString2(x) CppAsString(x)
731
732 #ifdef SO_MAJOR_VERSION
733 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
734 #else
735 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
736 #endif
737
738
739 /* ----------------------------------------------------------------
740  *                              Section 8: system-specific hacks
741  *
742  *              This should be limited to things that absolutely have to be
743  *              included in every source file.  The port-specific header file
744  *              is usually a better place for this sort of thing.
745  * ----------------------------------------------------------------
746  */
747
748 /*
749  *      NOTE:  this is also used for opening text files.
750  *      WIN32 treats Control-Z as EOF in files opened in text mode.
751  *      Therefore, we open files in binary mode on Win32 so we can read
752  *      literal control-Z.      The other affect is that we see CRLF, but
753  *      that is OK because we can already handle those cleanly.
754  */
755 #if defined(WIN32) || defined(__CYGWIN__)
756 #define PG_BINARY       O_BINARY
757 #define PG_BINARY_A "ab"
758 #define PG_BINARY_R "rb"
759 #define PG_BINARY_W "wb"
760 #else
761 #define PG_BINARY       0
762 #define PG_BINARY_A "a"
763 #define PG_BINARY_R "r"
764 #define PG_BINARY_W "w"
765 #endif
766
767 /*
768  * Provide prototypes for routines not present in a particular machine's
769  * standard C library.
770  */
771
772 #if !HAVE_DECL_SNPRINTF
773 extern int
774 snprintf(char *str, size_t count, const char *fmt,...)
775 /* This extension allows gcc to check the format string */
776 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
777 #endif
778
779 #if !HAVE_DECL_VSNPRINTF
780 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
781 #endif
782
783 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
784 #define memmove(d, s, c)                bcopy(s, d, c)
785 #endif
786
787 /* no special DLL markers on most ports */
788 #ifndef PGDLLIMPORT
789 #define PGDLLIMPORT
790 #endif
791 #ifndef PGDLLEXPORT
792 #define PGDLLEXPORT
793 #endif
794
795 /*
796  * The following is used as the arg list for signal handlers.  Any ports
797  * that take something other than an int argument should override this in
798  * their pg_config_os.h file.  Note that variable names are required
799  * because it is used in both the prototypes as well as the definitions.
800  * Note also the long name.  We expect that this won't collide with
801  * other names causing compiler warnings.
802  */
803
804 #ifndef SIGNAL_ARGS
805 #define SIGNAL_ARGS  int postgres_signal_arg
806 #endif
807
808 /*
809  * When there is no sigsetjmp, its functionality is provided by plain
810  * setjmp. Incidentally, nothing provides setjmp's functionality in
811  * that case.
812  */
813 #ifndef HAVE_SIGSETJMP
814 #define sigjmp_buf jmp_buf
815 #define sigsetjmp(x,y) setjmp(x)
816 #define siglongjmp longjmp
817 #endif
818
819 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
820 extern int      fdatasync(int fildes);
821 #endif
822
823 /* If strtoq() exists, rename it to the more standard strtoll() */
824 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
825 #define strtoll strtoq
826 #define HAVE_STRTOLL 1
827 #endif
828
829 /* If strtouq() exists, rename it to the more standard strtoull() */
830 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
831 #define strtoull strtouq
832 #define HAVE_STRTOULL 1
833 #endif
834
835 /*
836  * We assume if we have these two functions, we have their friends too, and
837  * can use the wide-character functions.
838  */
839 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
840 #define USE_WIDE_UPPER_LOWER
841 #endif
842
843 /* EXEC_BACKEND defines */
844 #ifdef EXEC_BACKEND
845 #define NON_EXEC_STATIC
846 #else
847 #define NON_EXEC_STATIC static
848 #endif
849
850 /* /port compatibility functions */
851 #include "port.h"
852
853 #endif   /* C_H */