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