]> granicus.if.org Git - postgresql/blob - src/include/c.h
Remove all the special-case code for INT64_IS_BUSTED, per decision that
[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-2010, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * $PostgreSQL: pgsql/src/include/c.h,v 1.239 2010/01/07 04:53:35 tgl 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
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
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
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
284 #else
285 /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
286 #error must have a working 64-bit integer datatype
287 #endif
288
289 /* Decide if we need to decorate 64-bit constants */
290 #ifdef HAVE_LL_CONSTANTS
291 #define INT64CONST(x)  ((int64) x##LL)
292 #define UINT64CONST(x) ((uint64) x##ULL)
293 #else
294 #define INT64CONST(x)  ((int64) x)
295 #define UINT64CONST(x) ((uint64) x)
296 #endif
297
298
299 /* Select timestamp representation (float8 or int64) */
300 #ifdef USE_INTEGER_DATETIMES
301 #define HAVE_INT64_TIMESTAMP
302 #endif
303
304 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
305 #ifndef HAVE_SIG_ATOMIC_T
306 typedef int sig_atomic_t;
307 #endif
308
309 /*
310  * Size
311  *              Size of any memory resident object, as returned by sizeof.
312  */
313 typedef size_t Size;
314
315 /*
316  * Index
317  *              Index into any memory resident array.
318  *
319  * Note:
320  *              Indices are non negative.
321  */
322 typedef unsigned int Index;
323
324 /*
325  * Offset
326  *              Offset into any memory resident array.
327  *
328  * Note:
329  *              This differs from an Index in that an Index is always
330  *              non negative, whereas Offset may be negative.
331  */
332 typedef signed int Offset;
333
334 /*
335  * Common Postgres datatype names (as used in the catalogs)
336  */
337 typedef int16 int2;
338 typedef int32 int4;
339 typedef float float4;
340 typedef double float8;
341
342 /*
343  * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
344  * CommandId
345  */
346
347 /* typedef Oid is in postgres_ext.h */
348
349 /*
350  * regproc is the type name used in the include/catalog headers, but
351  * RegProcedure is the preferred name in C code.
352  */
353 typedef Oid regproc;
354 typedef regproc RegProcedure;
355
356 typedef uint32 TransactionId;
357
358 typedef uint32 LocalTransactionId;
359
360 typedef uint32 SubTransactionId;
361
362 #define InvalidSubTransactionId         ((SubTransactionId) 0)
363 #define TopSubTransactionId                     ((SubTransactionId) 1)
364
365 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
366 typedef TransactionId MultiXactId;
367
368 typedef uint32 MultiXactOffset;
369
370 typedef uint32 CommandId;
371
372 #define FirstCommandId  ((CommandId) 0)
373
374 /*
375  * Array indexing support
376  */
377 #define MAXDIM 6
378 typedef struct
379 {
380         int                     indx[MAXDIM];
381 } IntArray;
382
383 /* ----------------
384  *              Variable-length datatypes all share the 'struct varlena' header.
385  *
386  * NOTE: for TOASTable types, this is an oversimplification, since the value
387  * may be compressed or moved out-of-line.      However datatype-specific routines
388  * are mostly content to deal with de-TOASTed values only, and of course
389  * client-side routines should never see a TOASTed value.  But even in a
390  * de-TOASTed value, beware of touching vl_len_ directly, as its representation
391  * is no longer convenient.  It's recommended that code always use the VARDATA,
392  * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of
393  * the struct fields.  See postgres.h for details of the TOASTed form.
394  * ----------------
395  */
396 struct varlena
397 {
398         char            vl_len_[4];             /* Do not touch this field directly! */
399         char            vl_dat[1];
400 };
401
402 #define VARHDRSZ                ((int32) sizeof(int32))
403
404 /*
405  * These widely-used datatypes are just a varlena header and the data bytes.
406  * There is no terminating null or anything like that --- the data length is
407  * always VARSIZE(ptr) - VARHDRSZ.
408  */
409 typedef struct varlena bytea;
410 typedef struct varlena text;
411 typedef struct varlena BpChar;  /* blank-padded char, ie SQL char(n) */
412 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
413
414 /*
415  * Specialized array types.  These are physically laid out just the same
416  * as regular arrays (so that the regular array subscripting code works
417  * with them).  They exist as distinct types mostly for historical reasons:
418  * they have nonstandard I/O behavior which we don't want to change for fear
419  * of breaking applications that look at the system catalogs.  There is also
420  * an implementation issue for oidvector: it's part of the primary key for
421  * pg_proc, and we can't use the normal btree array support routines for that
422  * without circularity.
423  */
424 typedef struct
425 {
426         int32           vl_len_;                /* these fields must match ArrayType! */
427         int                     ndim;                   /* always 1 for int2vector */
428         int32           dataoffset;             /* always 0 for int2vector */
429         Oid                     elemtype;
430         int                     dim1;
431         int                     lbound1;
432         int2            values[1];              /* VARIABLE LENGTH ARRAY */
433 } int2vector;                                   /* VARIABLE LENGTH STRUCT */
434
435 typedef struct
436 {
437         int32           vl_len_;                /* these fields must match ArrayType! */
438         int                     ndim;                   /* always 1 for oidvector */
439         int32           dataoffset;             /* always 0 for oidvector */
440         Oid                     elemtype;
441         int                     dim1;
442         int                     lbound1;
443         Oid                     values[1];              /* VARIABLE LENGTH ARRAY */
444 } oidvector;                                    /* VARIABLE LENGTH STRUCT */
445
446 /*
447  * Representation of a Name: effectively just a C string, but null-padded to
448  * exactly NAMEDATALEN bytes.  The use of a struct is historical.
449  */
450 typedef struct nameData
451 {
452         char            data[NAMEDATALEN];
453 } NameData;
454 typedef NameData *Name;
455
456 #define NameStr(name)   ((name).data)
457
458 /*
459  * Support macros for escaping strings.  escape_backslash should be TRUE
460  * if generating a non-standard-conforming string.      Prefixing a string
461  * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
462  * Beware of multiple evaluation of the "ch" argument!
463  */
464 #define SQL_STR_DOUBLE(ch, escape_backslash)    \
465         ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
466
467 #define ESCAPE_STRING_SYNTAX    'E'
468
469 /* ----------------------------------------------------------------
470  *                              Section 4:      IsValid macros for system types
471  * ----------------------------------------------------------------
472  */
473 /*
474  * BoolIsValid
475  *              True iff bool is valid.
476  */
477 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
478
479 /*
480  * PointerIsValid
481  *              True iff pointer is valid.
482  */
483 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
484
485 /*
486  * PointerIsAligned
487  *              True iff pointer is properly aligned to point to the given type.
488  */
489 #define PointerIsAligned(pointer, type) \
490                 (((intptr_t)(pointer) % (sizeof (type))) == 0)
491
492 #define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
493
494 #define RegProcedureIsValid(p)  OidIsValid(p)
495
496
497 /* ----------------------------------------------------------------
498  *                              Section 5:      offsetof, lengthof, endof, alignment
499  * ----------------------------------------------------------------
500  */
501 /*
502  * offsetof
503  *              Offset of a structure/union field within that structure/union.
504  *
505  *              XXX This is supposed to be part of stddef.h, but isn't on
506  *              some systems (like SunOS 4).
507  */
508 #ifndef offsetof
509 #define offsetof(type, field)   ((long) &((type *)0)->field)
510 #endif   /* offsetof */
511
512 /*
513  * lengthof
514  *              Number of elements in an array.
515  */
516 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
517
518 /*
519  * endof
520  *              Address of the element one past the last in an array.
521  */
522 #define endof(array)    (&(array)[lengthof(array)])
523
524 /* ----------------
525  * Alignment macros: align a length or address appropriately for a given type.
526  * The fooALIGN() macros round up to a multiple of the required alignment,
527  * while the fooALIGN_DOWN() macros round down.  The latter are more useful
528  * for problems like "how many X-sized structures will fit in a page?".
529  *
530  * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
531  * That case seems extremely unlikely to be needed in practice, however.
532  * ----------------
533  */
534
535 #define TYPEALIGN(ALIGNVAL,LEN)  \
536         (((intptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((intptr_t) ((ALIGNVAL) - 1)))
537
538 #define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
539 #define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
540 #define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
541 #define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
542 #define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
543 /* MAXALIGN covers only built-in types, not buffers */
544 #define BUFFERALIGN(LEN)                TYPEALIGN(ALIGNOF_BUFFER, (LEN))
545
546 #define TYPEALIGN_DOWN(ALIGNVAL,LEN)  \
547         (((intptr_t) (LEN)) & ~((intptr_t) ((ALIGNVAL) - 1)))
548
549 #define SHORTALIGN_DOWN(LEN)    TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
550 #define INTALIGN_DOWN(LEN)              TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
551 #define LONGALIGN_DOWN(LEN)             TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
552 #define DOUBLEALIGN_DOWN(LEN)   TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
553 #define MAXALIGN_DOWN(LEN)              TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
554
555 /* ----------------------------------------------------------------
556  *                              Section 6:      widely useful macros
557  * ----------------------------------------------------------------
558  */
559 /*
560  * Max
561  *              Return the maximum of two numbers.
562  */
563 #define Max(x, y)               ((x) > (y) ? (x) : (y))
564
565 /*
566  * Min
567  *              Return the minimum of two numbers.
568  */
569 #define Min(x, y)               ((x) < (y) ? (x) : (y))
570
571 /*
572  * Abs
573  *              Return the absolute value of the argument.
574  */
575 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
576
577 /*
578  * StrNCpy
579  *      Like standard library function strncpy(), except that result string
580  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
581  *      of the source string will be kept.
582  *      Also, the macro returns no result (too hard to do that without
583  *      evaluating the arguments multiple times, which seems worse).
584  *
585  *      BTW: when you need to copy a non-null-terminated string (like a text
586  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
587  *      might seem to work, but it fetches one byte more than there is in the
588  *      text object.  One fine day you'll have a SIGSEGV because there isn't
589  *      another byte before the end of memory.  Don't laugh, we've had real
590  *      live bug reports from real live users over exactly this mistake.
591  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
592  */
593 #define StrNCpy(dst,src,len) \
594         do \
595         { \
596                 char * _dst = (dst); \
597                 Size _len = (len); \
598 \
599                 if (_len > 0) \
600                 { \
601                         strncpy(_dst, (src), _len); \
602                         _dst[_len-1] = '\0'; \
603                 } \
604         } while (0)
605
606
607 /* Get a bit mask of the bits set in non-long aligned addresses */
608 #define LONG_ALIGN_MASK (sizeof(long) - 1)
609
610 /*
611  * MemSet
612  *      Exactly the same as standard library function memset(), but considerably
613  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
614  *      This has to be a macro because the main point is to avoid function-call
615  *      overhead.       However, we have also found that the loop is faster than
616  *      native libc memset() on some platforms, even those with assembler
617  *      memset() functions.  More research needs to be done, perhaps with
618  *      MEMSET_LOOP_LIMIT tests in configure.
619  */
620 #define MemSet(start, val, len) \
621         do \
622         { \
623                 /* must be void* because we don't know if it is integer aligned yet */ \
624                 void   *_vstart = (void *) (start); \
625                 int             _val = (val); \
626                 Size    _len = (len); \
627 \
628                 if ((((intptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
629                         (_len & LONG_ALIGN_MASK) == 0 && \
630                         _val == 0 && \
631                         _len <= MEMSET_LOOP_LIMIT && \
632                         /* \
633                          *      If MEMSET_LOOP_LIMIT == 0, optimizer should find \
634                          *      the whole "if" false at compile time. \
635                          */ \
636                         MEMSET_LOOP_LIMIT != 0) \
637                 { \
638                         long *_start = (long *) _vstart; \
639                         long *_stop = (long *) ((char *) _start + _len); \
640                         while (_start < _stop) \
641                                 *_start++ = 0; \
642                 } \
643                 else \
644                         memset(_vstart, _val, _len); \
645         } while (0)
646
647 /*
648  * MemSetAligned is the same as MemSet except it omits the test to see if
649  * "start" is word-aligned.  This is okay to use if the caller knows a-priori
650  * that the pointer is suitably aligned (typically, because he just got it
651  * from palloc(), which always delivers a max-aligned pointer).
652  */
653 #define MemSetAligned(start, val, len) \
654         do \
655         { \
656                 long   *_start = (long *) (start); \
657                 int             _val = (val); \
658                 Size    _len = (len); \
659 \
660                 if ((_len & LONG_ALIGN_MASK) == 0 && \
661                         _val == 0 && \
662                         _len <= MEMSET_LOOP_LIMIT && \
663                         MEMSET_LOOP_LIMIT != 0) \
664                 { \
665                         long *_stop = (long *) ((char *) _start + _len); \
666                         while (_start < _stop) \
667                                 *_start++ = 0; \
668                 } \
669                 else \
670                         memset(_start, _val, _len); \
671         } while (0)
672
673
674 /*
675  * MemSetTest/MemSetLoop are a variant version that allow all the tests in
676  * MemSet to be done at compile time in cases where "val" and "len" are
677  * constants *and* we know the "start" pointer must be word-aligned.
678  * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
679  * MemSetAligned.  Beware of multiple evaluations of the arguments when using
680  * this approach.
681  */
682 #define MemSetTest(val, len) \
683         ( ((len) & LONG_ALIGN_MASK) == 0 && \
684         (len) <= MEMSET_LOOP_LIMIT && \
685         MEMSET_LOOP_LIMIT != 0 && \
686         (val) == 0 )
687
688 #define MemSetLoop(start, val, len) \
689         do \
690         { \
691                 long * _start = (long *) (start); \
692                 long * _stop = (long *) ((char *) _start + (Size) (len)); \
693         \
694                 while (_start < _stop) \
695                         *_start++ = 0; \
696         } while (0)
697
698
699 /* ----------------------------------------------------------------
700  *                              Section 7:      random stuff
701  * ----------------------------------------------------------------
702  */
703
704 /* msb for char */
705 #define HIGHBIT                                 (0x80)
706 #define IS_HIGHBIT_SET(ch)              ((unsigned char)(ch) & HIGHBIT)
707
708 #define STATUS_OK                               (0)
709 #define STATUS_ERROR                    (-1)
710 #define STATUS_EOF                              (-2)
711 #define STATUS_FOUND                    (1)
712 #define STATUS_WAITING                  (2)
713
714
715 /* gettext domain name mangling */
716
717 /*
718  * To better support parallel installations of major PostgeSQL
719  * versions as well as parallel installations of major library soname
720  * versions, we mangle the gettext domain name by appending those
721  * version numbers.  The coding rule ought to be that whereever the
722  * domain name is mentioned as a literal, it must be wrapped into
723  * PG_TEXTDOMAIN().  The macros below do not work on non-literals; but
724  * that is somewhat intentional because it avoids having to worry
725  * about multiple states of premangling and postmangling as the values
726  * are being passed around.
727  *
728  * Make sure this matches the installation rules in nls-global.mk.
729  */
730
731 /* need a second indirection because we want to stringize the macro value, not the name */
732 #define CppAsString2(x) CppAsString(x)
733
734 #ifdef SO_MAJOR_VERSION
735 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
736 #else
737 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
738 #endif
739
740
741 /* ----------------------------------------------------------------
742  *                              Section 8: system-specific hacks
743  *
744  *              This should be limited to things that absolutely have to be
745  *              included in every source file.  The port-specific header file
746  *              is usually a better place for this sort of thing.
747  * ----------------------------------------------------------------
748  */
749
750 /*
751  *      NOTE:  this is also used for opening text files.
752  *      WIN32 treats Control-Z as EOF in files opened in text mode.
753  *      Therefore, we open files in binary mode on Win32 so we can read
754  *      literal control-Z.      The other affect is that we see CRLF, but
755  *      that is OK because we can already handle those cleanly.
756  */
757 #if defined(WIN32) || defined(__CYGWIN__)
758 #define PG_BINARY       O_BINARY
759 #define PG_BINARY_A "ab"
760 #define PG_BINARY_R "rb"
761 #define PG_BINARY_W "wb"
762 #else
763 #define PG_BINARY       0
764 #define PG_BINARY_A "a"
765 #define PG_BINARY_R "r"
766 #define PG_BINARY_W "w"
767 #endif
768
769 /*
770  * Provide prototypes for routines not present in a particular machine's
771  * standard C library.
772  */
773
774 #if !HAVE_DECL_SNPRINTF
775 extern int
776 snprintf(char *str, size_t count, const char *fmt,...)
777 /* This extension allows gcc to check the format string */
778 __attribute__((format(printf, 3, 4)));
779 #endif
780
781 #if !HAVE_DECL_VSNPRINTF
782 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
783 #endif
784
785 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
786 #define memmove(d, s, c)                bcopy(s, d, c)
787 #endif
788
789 #ifndef PGDLLIMPORT
790 #define PGDLLIMPORT                             /* no special DLL markers on most ports */
791 #endif
792
793 /*
794  * The following is used as the arg list for signal handlers.  Any ports
795  * that take something other than an int argument should override this in
796  * their pg_config_os.h file.  Note that variable names are required
797  * because it is used in both the prototypes as well as the definitions.
798  * Note also the long name.  We expect that this won't collide with
799  * other names causing compiler warnings.
800  */
801
802 #ifndef SIGNAL_ARGS
803 #define SIGNAL_ARGS  int postgres_signal_arg
804 #endif
805
806 /*
807  * When there is no sigsetjmp, its functionality is provided by plain
808  * setjmp. Incidentally, nothing provides setjmp's functionality in
809  * that case.
810  */
811 #ifndef HAVE_SIGSETJMP
812 #define sigjmp_buf jmp_buf
813 #define sigsetjmp(x,y) setjmp(x)
814 #define siglongjmp longjmp
815 #endif
816
817 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
818 extern int      fdatasync(int fildes);
819 #endif
820
821 /* If strtoq() exists, rename it to the more standard strtoll() */
822 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
823 #define strtoll strtoq
824 #define HAVE_STRTOLL 1
825 #endif
826
827 /* If strtouq() exists, rename it to the more standard strtoull() */
828 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
829 #define strtoull strtouq
830 #define HAVE_STRTOULL 1
831 #endif
832
833 /*
834  * We assume if we have these two functions, we have their friends too, and
835  * can use the wide-character functions.
836  */
837 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
838 #define USE_WIDE_UPPER_LOWER
839 #endif
840
841 /* EXEC_BACKEND defines */
842 #ifdef EXEC_BACKEND
843 #define NON_EXEC_STATIC
844 #else
845 #define NON_EXEC_STATIC static
846 #endif
847
848 /* /port compatibility functions */
849 #include "port.h"
850
851 #endif   /* C_H */