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