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