]> granicus.if.org Git - postgresql/blob - src/include/c.h
Add configure check to see whether <string.h> and <strings.h> may both be
[postgresql] / src / include / c.h
1 /*-------------------------------------------------------------------------
2  *
3  * c.h
4  *        Fundamental C definitions.  This is included by every .c file in
5  *        postgres.
6  *
7  *
8  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * $Id: c.h,v 1.85 2000/11/03 18:43:52 petere Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  *       TABLE OF CONTENTS
17  *
18  *              When adding stuff to this file, please try to put stuff
19  *              into the relevant section, or add new sections as appropriate.
20  *
21  *        section       description
22  *        -------       ------------------------------------------------
23  *              1)              bool, true, false, TRUE, FALSE, NULL
24  *              2)              non-ansi C definitions:
25  *                              type prefixes: const, signed, volatile, inline
26  *                              cpp magic macros
27  *              3)              standard system types
28  *              4)              datum type
29  *              5)              IsValid macros for system types
30  *              6)              offsetof, lengthof, endof
31  *              7)              exception handling definitions, Assert, Trap, etc macros
32  *              8)              Min, Max, Abs, StrNCpy macros
33  *              9)              externs
34  *              10)             Berkeley-specific defs
35  *              11)             system-specific hacks
36  *
37  * ----------------------------------------------------------------
38  */
39 #ifndef C_H
40 #define C_H
41
42 /* We have to include stdlib.h here because it defines many of these macros
43    on some platforms, and we only want our definitions used if stdlib.h doesn't
44    have its own.  The same goes for stddef and stdarg if present.
45 */
46
47 #include "config.h"
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <stddef.h>
53 #include <stdarg.h>
54 #ifdef STRING_H_WITH_STRINGS_H
55 #include <strings.h>
56 #endif
57
58 #ifdef __CYGWIN__
59 #include <errno.h>
60 #include <sys/fcntl.h>          /* ensure O_BINARY is available */
61 #endif
62 #ifdef HAVE_SUPPORTDEFS_H
63 #include <SupportDefs.h>
64 #endif
65
66 /* ----------------------------------------------------------------
67  *                              Section 1:      bool, true, false, TRUE, FALSE, NULL
68  * ----------------------------------------------------------------
69  */
70 /*
71  * bool
72  *              Boolean value, either true or false.
73  *
74  */
75
76 /* BeOS defines bool already, but the compiler chokes on the
77  * #ifndef unless we wrap it in this check.
78  */
79 #ifndef __BEOS__ 
80 #ifndef __cplusplus
81 #ifndef bool
82 typedef char bool;
83
84 #endif   /* ndef bool */
85 #endif   /* not C++ */
86 #ifndef true
87 #define true    ((bool) 1)
88 #endif
89 #ifndef false
90 #define false   ((bool) 0)
91 #endif
92 #endif /* __BEOS__ */
93 typedef bool *BoolPtr;
94
95 #ifndef TRUE
96 #define TRUE    1
97 #endif   /* TRUE */
98
99 #ifndef FALSE
100 #define FALSE   0
101 #endif   /* FALSE */
102
103 /*
104  * NULL
105  *              Null pointer.
106  */
107 #ifndef NULL
108 #define NULL    ((void *) 0)
109 #endif   /* !defined(NULL) */
110
111 /* ----------------------------------------------------------------
112  *                              Section 2: non-ansi C definitions:
113  *
114  *                              type prefixes: const, signed, volatile, inline
115  *                              cpp magic macros
116  * ----------------------------------------------------------------
117  */
118
119 /*
120  * CppAsString
121  *              Convert the argument to a string, using the C preprocessor.
122  * CppConcat
123  *              Concatenate two arguments together, using the C preprocessor.
124  *
125  * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
126  * whether #identifier works, but if we have that we likely have ## too.
127  */
128 #if defined(HAVE_STRINGIZE)
129
130 #define CppAsString(identifier) #identifier
131 #define CppConcat(x, y)                 x##y
132
133 #else                                                   /* !HAVE_STRINGIZE */
134
135 #define CppAsString(identifier) "identifier"
136
137 /*
138  * CppIdentity -- On Reiser based cpp's this is used to concatenate
139  *              two tokens.  That is
140  *                              CppIdentity(A)B ==> AB
141  *              We renamed it to _private_CppIdentity because it should not
142  *              be referenced outside this file.  On other cpp's it
143  *              produces  A  B.
144  */
145 #define _priv_CppIdentity(x)x
146 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
147
148 #endif   /* !HAVE_STRINGIZE */
149
150 /*
151  * dummyret is used to set return values in macros that use ?: to make
152  * assignments.  gcc wants these to be void, other compilers like char
153  */
154 #ifdef __GNUC__                                 /* GNU cc */
155 #define dummyret        void
156 #else
157 #define dummyret        char
158 #endif
159
160 /* ----------------------------------------------------------------
161  *                              Section 3:      standard system types
162  * ----------------------------------------------------------------
163  */
164
165 /*
166  * Pointer
167  *              Variable holding address of any memory resident object.
168  *
169  *              XXX Pointer arithmetic is done with this, so it can't be void *
170  *              under "true" ANSI compilers.
171  */
172 typedef char *Pointer;
173
174 /*
175  * intN
176  *              Signed integer, EXACTLY N BITS IN SIZE,
177  *              used for numerical computations and the
178  *              frontend/backend protocol.
179  */
180 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
181 typedef signed char int8;               /* == 8 bits */
182 typedef signed short int16;             /* == 16 bits */
183 typedef signed int int32;               /* == 32 bits */
184 #endif /* __BEOS__ */
185 /*
186  * uintN
187  *              Unsigned integer, EXACTLY N BITS IN SIZE,
188  *              used for numerical computations and the
189  *              frontend/backend protocol.
190  */
191 #ifndef __BEOS__ /* this shouldn't be required, but is is! */
192 typedef unsigned char uint8;    /* == 8 bits */
193 typedef unsigned short uint16;  /* == 16 bits */
194 typedef unsigned int uint32;    /* == 32 bits */
195 #endif /* __BEOS__ */
196 /*
197  * floatN
198  *              Floating point number, AT LEAST N BITS IN SIZE,
199  *              used for numerical computations.
200  *
201  *              Since sizeof(floatN) may be > sizeof(char *), always pass
202  *              floatN by reference.
203  *
204  * XXX: these typedefs are now deprecated in favor of float4 and float8.
205  * They will eventually go away.
206  */
207 typedef float float32data;
208 typedef double float64data;
209 typedef float *float32;
210 typedef double *float64;
211
212 /*
213  * boolN
214  *              Boolean value, AT LEAST N BITS IN SIZE.
215  */
216 typedef uint8 bool8;                    /* >= 8 bits */
217 typedef uint16 bool16;                  /* >= 16 bits */
218 typedef uint32 bool32;                  /* >= 32 bits */
219
220 /*
221  * bitsN
222  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
223  */
224 typedef uint8 bits8;                    /* >= 8 bits */
225 typedef uint16 bits16;                  /* >= 16 bits */
226 typedef uint32 bits32;                  /* >= 32 bits */
227
228 /*
229  * wordN
230  *              Unit of storage, AT LEAST N BITS IN SIZE,
231  *              used to fetch/store data.
232  */
233 typedef uint8 word8;                    /* >= 8 bits */
234 typedef uint16 word16;                  /* >= 16 bits */
235 typedef uint32 word32;                  /* >= 32 bits */
236
237 /*
238  * Size
239  *              Size of any memory resident object, as returned by sizeof.
240  */
241 typedef size_t Size;
242
243 /*
244  * Index
245  *              Index into any memory resident array.
246  *
247  * Note:
248  *              Indices are non negative.
249  */
250 typedef unsigned int Index;
251
252 #define MAXDIM 6
253 typedef struct
254 {
255         int                     indx[MAXDIM];
256 } IntArray;
257
258 /*
259  * Offset
260  *              Offset into any memory resident array.
261  *
262  * Note:
263  *              This differs from an Index in that an Index is always
264  *              non negative, whereas Offset may be negative.
265  */
266 typedef signed int Offset;
267
268 /*
269  * Common Postgres datatypes.
270  */
271 typedef int16 int2;
272 typedef int32 int4;
273 typedef float float4;
274 typedef double float8;
275
276 #ifndef __BEOS__ /* this is already defined on BeOS */
277 #ifdef HAVE_LONG_INT_64
278 /* Plain "long int" fits, use it */
279 typedef long int int64;
280 #else
281 #ifdef HAVE_LONG_LONG_INT_64
282 /* We have working support for "long long int", use that */
283 typedef long long int int64;
284 #else
285 /* Won't actually work, but fall back to long int so that code compiles */
286 typedef long int int64;
287 #define INT64_IS_BUSTED
288 #endif
289 #endif
290 #endif /* __BEOS__ */
291
292 /* ----------------------------------------------------------------
293  *                              Section 4:      datum type + support macros
294  * ----------------------------------------------------------------
295  */
296 /*
297  * datum.h
298  *              POSTGRES abstract data type datum representation definitions.
299  *
300  * Note:
301  *
302  * Port Notes:
303  *      Postgres makes the following assumption about machines:
304  *
305  *      sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
306  *
307  *      Postgres also assumes that
308  *
309  *      sizeof(char) == 1
310  *
311  *      and that
312  *
313  *      sizeof(short) == 2
314  *
315  *      If your machine meets these requirements, Datums should also be checked
316  *      to see if the positioning is correct.
317  */
318
319 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
320 typedef Datum *DatumPtr;
321
322 #define GET_1_BYTE(datum)       (((Datum) (datum)) & 0x000000ff)
323 #define GET_2_BYTES(datum)      (((Datum) (datum)) & 0x0000ffff)
324 #define GET_4_BYTES(datum)      (((Datum) (datum)) & 0xffffffff)
325 #define SET_1_BYTE(value)       (((Datum) (value)) & 0x000000ff)
326 #define SET_2_BYTES(value)      (((Datum) (value)) & 0x0000ffff)
327 #define SET_4_BYTES(value)      (((Datum) (value)) & 0xffffffff)
328
329 /*
330  * DatumGetBool
331  *              Returns boolean value of a datum.
332  *
333  * Note: any nonzero value will be considered TRUE.
334  */
335
336 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
337
338 /*
339  * BoolGetDatum
340  *              Returns datum representation for a boolean.
341  *
342  * Note: any nonzero value will be considered TRUE.
343  */
344
345 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
346
347 /*
348  * DatumGetChar
349  *              Returns character value of a datum.
350  */
351
352 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
353
354 /*
355  * CharGetDatum
356  *              Returns datum representation for a character.
357  */
358
359 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
360
361 /*
362  * Int8GetDatum
363  *              Returns datum representation for an 8-bit integer.
364  */
365
366 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
367
368 /*
369  * DatumGetUInt8
370  *              Returns 8-bit unsigned integer value of a datum.
371  */
372
373 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
374
375 /*
376  * UInt8GetDatum
377  *              Returns datum representation for an 8-bit unsigned integer.
378  */
379
380 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
381
382 /*
383  * DatumGetInt16
384  *              Returns 16-bit integer value of a datum.
385  */
386
387 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
388
389 /*
390  * Int16GetDatum
391  *              Returns datum representation for a 16-bit integer.
392  */
393
394 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
395
396 /*
397  * DatumGetUInt16
398  *              Returns 16-bit unsigned integer value of a datum.
399  */
400
401 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
402
403 /*
404  * UInt16GetDatum
405  *              Returns datum representation for a 16-bit unsigned integer.
406  */
407
408 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
409
410 /*
411  * DatumGetInt32
412  *              Returns 32-bit integer value of a datum.
413  */
414
415 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
416
417 /*
418  * Int32GetDatum
419  *              Returns datum representation for a 32-bit integer.
420  */
421
422 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
423
424 /*
425  * DatumGetUInt32
426  *              Returns 32-bit unsigned integer value of a datum.
427  */
428
429 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
430
431 /*
432  * UInt32GetDatum
433  *              Returns datum representation for a 32-bit unsigned integer.
434  */
435
436 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
437
438 /*
439  * DatumGetObjectId
440  *              Returns object identifier value of a datum.
441  */
442
443 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
444
445 /*
446  * ObjectIdGetDatum
447  *              Returns datum representation for an object identifier.
448  */
449
450 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
451
452 /*
453  * DatumGetPointer
454  *              Returns pointer value of a datum.
455  */
456
457 #define DatumGetPointer(X) ((Pointer) (X))
458
459 /*
460  * PointerGetDatum
461  *              Returns datum representation for a pointer.
462  */
463
464 #define PointerGetDatum(X) ((Datum) (X))
465
466 /*
467  * DatumGetCString
468  *              Returns C string (null-terminated string) value of a datum.
469  *
470  * Note: C string is not a full-fledged Postgres type at present,
471  * but type input functions use this conversion for their inputs.
472  */
473
474 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
475
476 /*
477  * CStringGetDatum
478  *              Returns datum representation for a C string (null-terminated string).
479  *
480  * Note: C string is not a full-fledged Postgres type at present,
481  * but type output functions use this conversion for their outputs.
482  * Note: CString is pass-by-reference; caller must ensure the pointed-to
483  * value has adequate lifetime.
484  */
485
486 #define CStringGetDatum(X) PointerGetDatum(X)
487
488 /*
489  * DatumGetName
490  *              Returns name value of a datum.
491  */
492
493 #define DatumGetName(X) ((Name) DatumGetPointer(X))
494
495 /*
496  * NameGetDatum
497  *              Returns datum representation for a name.
498  *
499  * Note: Name is pass-by-reference; caller must ensure the pointed-to
500  * value has adequate lifetime.
501  */
502
503 #define NameGetDatum(X) PointerGetDatum(X)
504
505 /*
506  * DatumGetInt64
507  *              Returns 64-bit integer value of a datum.
508  *
509  * Note: this macro hides the fact that int64 is currently a
510  * pass-by-reference type.  Someday it may be pass-by-value,
511  * at least on some platforms.
512  */
513
514 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
515
516 /*
517  * Int64GetDatum
518  *              Returns datum representation for a 64-bit integer.
519  *
520  * Note: this routine returns a reference to palloc'd space.
521  */
522
523 extern Datum Int64GetDatum(int64 X);
524
525 /*
526  * DatumGetFloat4
527  *              Returns 4-byte floating point value of a datum.
528  *
529  * Note: this macro hides the fact that float4 is currently a
530  * pass-by-reference type.  Someday it may be pass-by-value.
531  */
532
533 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
534
535 /*
536  * Float4GetDatum
537  *              Returns datum representation for a 4-byte floating point number.
538  *
539  * Note: this routine returns a reference to palloc'd space.
540  */
541
542 extern Datum Float4GetDatum(float4 X);
543
544 /*
545  * DatumGetFloat8
546  *              Returns 8-byte floating point value of a datum.
547  *
548  * Note: this macro hides the fact that float8 is currently a
549  * pass-by-reference type.  Someday it may be pass-by-value,
550  * at least on some platforms.
551  */
552
553 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
554
555 /*
556  * Float8GetDatum
557  *              Returns datum representation for an 8-byte floating point number.
558  *
559  * Note: this routine returns a reference to palloc'd space.
560  */
561
562 extern Datum Float8GetDatum(float8 X);
563
564
565 /*
566  * DatumGetFloat32
567  *              Returns 32-bit floating point value of a datum.
568  *              This is really a pointer, of course.
569  *
570  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
571  * It will eventually go away.
572  */
573
574 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
575
576 /*
577  * Float32GetDatum
578  *              Returns datum representation for a 32-bit floating point number.
579  *              This is really a pointer, of course.
580  *
581  * XXX: this macro is now deprecated in favor of Float4GetDatum.
582  * It will eventually go away.
583  */
584
585 #define Float32GetDatum(X) PointerGetDatum(X)
586
587 /*
588  * DatumGetFloat64
589  *              Returns 64-bit floating point value of a datum.
590  *              This is really a pointer, of course.
591  *
592  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
593  * It will eventually go away.
594  */
595
596 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
597
598 /*
599  * Float64GetDatum
600  *              Returns datum representation for a 64-bit floating point number.
601  *              This is really a pointer, of course.
602  *
603  * XXX: this macro is now deprecated in favor of Float8GetDatum.
604  * It will eventually go away.
605  */
606
607 #define Float64GetDatum(X) PointerGetDatum(X)
608
609 /*
610  * Int64GetDatumFast
611  * Float4GetDatumFast
612  * Float8GetDatumFast
613  *
614  * These macros are intended to allow writing code that does not depend on
615  * whether int64, float4, float8 are pass-by-reference types, while not
616  * sacrificing performance when they are.  The argument must be a variable
617  * that will exist and have the same value for as long as the Datum is needed.
618  * In the pass-by-ref case, the address of the variable is taken to use as
619  * the Datum.  In the pass-by-val case, these will be the same as the non-Fast
620  * macros.
621  */
622
623 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
624 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
625 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
626
627
628 /* ----------------------------------------------------------------
629  *                              Section 5:      IsValid macros for system types
630  * ----------------------------------------------------------------
631  */
632 /*
633  * BoolIsValid
634  *              True iff bool is valid.
635  */
636 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
637
638 /*
639  * PointerIsValid
640  *              True iff pointer is valid.
641  */
642 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
643
644 /*
645  * PointerIsAligned
646  *              True iff pointer is properly aligned to point to the given type.
647  */
648 #define PointerIsAligned(pointer, type) \
649                 (((long)(pointer) % (sizeof (type))) == 0)
650
651 /* ----------------------------------------------------------------
652  *                              Section 6:      offsetof, lengthof, endof
653  * ----------------------------------------------------------------
654  */
655 /*
656  * offsetof
657  *              Offset of a structure/union field within that structure/union.
658  *
659  *              XXX This is supposed to be part of stddef.h, but isn't on
660  *              some systems (like SunOS 4).
661  */
662 #ifndef offsetof
663 #define offsetof(type, field)   ((long) &((type *)0)->field)
664 #endif   /* offsetof */
665
666 /*
667  * lengthof
668  *              Number of elements in an array.
669  */
670 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
671
672 /*
673  * endof
674  *              Address of the element one past the last in an array.
675  */
676 #define endof(array)    (&array[lengthof(array)])
677
678 /* ----------------------------------------------------------------
679  *                              Section 7:      exception handling definitions
680  *                                                      Assert, Trap, etc macros
681  * ----------------------------------------------------------------
682  */
683 /*
684  * Exception Handling definitions
685  */
686
687 typedef char *ExcMessage;
688 typedef struct Exception
689 {
690         ExcMessage      message;
691 } Exception;
692
693 /*
694  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
695  * - plai  9/5/90
696  *
697  * It should _NOT_ be defined in releases or in benchmark copies
698  */
699
700 /*
701  * Trap
702  *              Generates an exception if the given condition is true.
703  *
704  */
705 #define Trap(condition, exception) \
706                 do { \
707                         if ((assert_enabled) && (condition)) \
708                                 ExceptionalCondition(CppAsString(condition), &(exception), \
709                                                 (char*)NULL, __FILE__, __LINE__); \
710                 } while (0)
711
712 /*
713  *      TrapMacro is the same as Trap but it's intended for use in macros:
714  *
715  *              #define foo(x) (AssertM(x != 0) && bar(x))
716  *
717  *      Isn't CPP fun?
718  */
719 #define TrapMacro(condition, exception) \
720         ((bool) ((! assert_enabled) || ! (condition) || \
721                          (ExceptionalCondition(CppAsString(condition), \
722                                                                   &(exception), \
723                                                                   (char*) NULL, __FILE__, __LINE__))))
724
725 #ifndef USE_ASSERT_CHECKING
726 #define Assert(condition)
727 #define AssertMacro(condition)  ((void)true)
728 #define AssertArg(condition)
729 #define AssertState(condition)
730 #define assert_enabled 0
731 #else
732 #define Assert(condition) \
733                 Trap(!(condition), FailedAssertion)
734
735 #define AssertMacro(condition) \
736                 ((void) TrapMacro(!(condition), FailedAssertion))
737
738 #define AssertArg(condition) \
739                 Trap(!(condition), BadArg)
740
741 #define AssertState(condition) \
742                 Trap(!(condition), BadState)
743
744 extern bool     assert_enabled;
745
746 #endif   /* USE_ASSERT_CHECKING */
747
748 /*
749  * LogTrap
750  *              Generates an exception with a message if the given condition is true.
751  *
752  */
753 #define LogTrap(condition, exception, printArgs) \
754                 do { \
755                         if ((assert_enabled) && (condition)) \
756                                 ExceptionalCondition(CppAsString(condition), &(exception), \
757                                                 vararg_format printArgs, __FILE__, __LINE__); \
758                 } while (0)
759
760 /*
761  *      LogTrapMacro is the same as LogTrap but it's intended for use in macros:
762  *
763  *              #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
764  */
765 #define LogTrapMacro(condition, exception, printArgs) \
766         ((bool) ((! assert_enabled) || ! (condition) || \
767                          (ExceptionalCondition(CppAsString(condition), \
768                                                                    &(exception), \
769                                                                    vararg_format printArgs, __FILE__, __LINE__))))
770
771 #ifndef USE_ASSERT_CHECKING
772 #define LogAssert(condition, printArgs)
773 #define LogAssertMacro(condition, printArgs) true
774 #define LogAssertArg(condition, printArgs)
775 #define LogAssertState(condition, printArgs)
776 #else
777 #define LogAssert(condition, printArgs) \
778                 LogTrap(!(condition), FailedAssertion, printArgs)
779
780 #define LogAssertMacro(condition, printArgs) \
781                 LogTrapMacro(!(condition), FailedAssertion, printArgs)
782
783 #define LogAssertArg(condition, printArgs) \
784                 LogTrap(!(condition), BadArg, printArgs)
785
786 #define LogAssertState(condition, printArgs) \
787                 LogTrap(!(condition), BadState, printArgs)
788
789 #ifdef ASSERT_CHECKING_TEST
790 extern int      assertTest(int val);
791
792 #endif
793 #endif   /* USE_ASSERT_CHECKING */
794
795 /* ----------------------------------------------------------------
796  *                              Section 8:      Min, Max, Abs macros
797  * ----------------------------------------------------------------
798  */
799 /*
800  * Max
801  *              Return the maximum of two numbers.
802  */
803 #define Max(x, y)               ((x) > (y) ? (x) : (y))
804
805 /*
806  * Min
807  *              Return the minimum of two numbers.
808  */
809 #define Min(x, y)               ((x) < (y) ? (x) : (y))
810
811 /*
812  * Abs
813  *              Return the absolute value of the argument.
814  */
815 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
816
817 /*
818  * StrNCpy
819  *      Like standard library function strncpy(), except that result string
820  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
821  *      of the source string will be kept.
822  *      Also, the macro returns no result (too hard to do that without
823  *      evaluating the arguments multiple times, which seems worse).
824  *
825  *      BTW: when you need to copy a non-null-terminated string (like a text
826  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
827  *      might seem to work, but it fetches one byte more than there is in the
828  *      text object.  One fine day you'll have a SIGSEGV because there isn't
829  *      another byte before the end of memory.  Don't laugh, we've had real
830  *      live bug reports from real live users over exactly this mistake.
831  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
832  */
833 #define StrNCpy(dst,src,len) \
834         do \
835         { \
836                 char * _dst = (dst); \
837                 Size _len = (len); \
838 \
839                 if (_len > 0) \
840                 { \
841                         strncpy(_dst, (src), _len); \
842                         _dst[_len-1] = '\0'; \
843                 } \
844         } while (0)
845
846
847 /* Get a bit mask of the bits set in non-int32 aligned addresses */
848 #define INT_ALIGN_MASK (sizeof(int32) - 1)
849
850 /*
851  * MemSet
852  *      Exactly the same as standard library function memset(), but considerably
853  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
854  *      This has to be a macro because the main point is to avoid function-call
855  *      overhead.
856  *
857  *      We got the 64 number by testing this against the stock memset() on
858  *      BSD/OS 3.0. Larger values were slower.  bjm 1997/09/11
859  *
860  *      I think the crossover point could be a good deal higher for
861  *      most platforms, actually.  tgl 2000-03-19
862  */
863 #define MemSet(start, val, len) \
864         do \
865         { \
866                 int32 * _start = (int32 *) (start); \
867                 int             _val = (val); \
868                 Size    _len = (len); \
869 \
870                 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
871                         (_len & INT_ALIGN_MASK) == 0 && \
872                         _val == 0 && \
873                         _len <= MEMSET_LOOP_LIMIT) \
874                 { \
875                         int32 * _stop = (int32 *) ((char *) _start + _len); \
876                         while (_start < _stop) \
877                                 *_start++ = 0; \
878                 } \
879                 else \
880                         memset((char *) _start, _val, _len); \
881         } while (0)
882
883 #define MEMSET_LOOP_LIMIT  64
884
885
886 /* ----------------------------------------------------------------
887  *                              Section 9: externs
888  * ----------------------------------------------------------------
889  */
890
891 extern Exception FailedAssertion;
892 extern Exception BadArg;
893 extern Exception BadState;
894
895 /* in utils/error/assert.c */
896 extern int ExceptionalCondition(char *conditionName,
897                                          Exception *exceptionP, char *details,
898                                          char *fileName, int lineNumber);
899
900
901 /* ----------------
902  *              vararg_format is used by assert and the exception handling stuff
903  * ----------------
904  */
905 extern char *vararg_format(const char *fmt,...);
906
907
908
909 /* ----------------------------------------------------------------
910  *                              Section 10: berkeley-specific configuration
911  *
912  * this section contains settings which are only relevant to the UC Berkeley
913  * sites.  Other sites can ignore this
914  * ----------------------------------------------------------------
915  */
916
917 /* ----------------
918  *              storage managers
919  *
920  *              These are experimental and are not supported in the code that
921  *              we distribute to other sites.
922  * ----------------
923  */
924 #ifdef NOT_USED
925 #define STABLE_MEMORY_STORAGE
926 #endif
927
928
929
930 /* ----------------------------------------------------------------
931  *                              Section 11: system-specific hacks
932  *
933  *              This should be limited to things that absolutely have to be
934  *              included in every source file.  The changes should be factored
935  *              into a separate file so that changes to one port don't require
936  *              changes to c.h (and everyone recompiling their whole system).
937  * ----------------------------------------------------------------
938  */
939
940 #ifdef __CYGWIN__
941 #define PG_BINARY       O_BINARY
942 #define PG_BINARY_R     "rb"
943 #define PG_BINARY_W     "wb"
944 #else
945 #define PG_BINARY       0
946 #define PG_BINARY_R     "r"
947 #define PG_BINARY_W     "w"
948 #endif
949
950 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
951 #define memmove(d, s, l)                bcopy(s, d, l)
952 #include <unistd.h>
953 #include <varargs.h>
954 #endif
955
956 /* These are for things that are one way on Unix and another on NT */
957 #define NULL_DEV                "/dev/null"
958 #define SEP_CHAR                '/'
959
960 /* defines for dynamic linking on Win32 platform */
961 #ifdef __CYGWIN__
962 #if __GNUC__ && ! defined (__declspec)
963 #error You need egcs 1.1 or newer for compiling!
964 #endif
965 #ifdef BUILDING_DLL
966 #define DLLIMPORT __declspec (dllexport)
967 #else                                                   /* not BUILDING_DLL */
968 #define DLLIMPORT __declspec (dllimport)
969 #endif
970 #else                                                   /* not CYGWIN */
971 #define DLLIMPORT
972 #endif
973
974 /* Provide prototypes for routines not present in a particular machine's
975  * standard C library.  It'd be better to put these in config.h, but
976  * in config.h we haven't yet included anything that defines size_t...
977  */
978
979 #ifndef HAVE_SNPRINTF_DECL
980 extern int      snprintf(char *str, size_t count, const char *fmt,...);
981
982 #endif
983
984 #ifndef HAVE_VSNPRINTF_DECL
985 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
986
987 #endif
988
989 #ifndef HAVE_MEMMOVE
990 #include <regex/utils.h>
991 #endif
992
993 /* ----------------
994  *              end of c.h
995  * ----------------
996  */
997 #endif   /* C_H */