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