]> granicus.if.org Git - postgresql/blob - src/include/c.h
Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[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-2001, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * $Id: c.h,v 1.89 2001/01/24 19:43:19 momjian 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 typedef unsigned long int uint64;
281 #else
282 #ifdef HAVE_LONG_LONG_INT_64
283 /* We have working support for "long long int", use that */
284 typedef long long int int64;
285 typedef unsigned long long int uint64;
286 #else
287 /* Won't actually work, but fall back to long int so that code compiles */
288 typedef long int int64;
289 typedef unsigned long int uint64;
290 #define INT64_IS_BUSTED
291 #endif
292 #endif
293 #endif /* __BEOS__ */
294
295 /* ----------------------------------------------------------------
296  *                              Section 4:      datum type + support macros
297  * ----------------------------------------------------------------
298  */
299 /*
300  * datum.h
301  *              POSTGRES abstract data type datum representation definitions.
302  *
303  * Note:
304  *
305  * Port Notes:
306  *      Postgres makes the following assumption about machines:
307  *
308  *      sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
309  *
310  *      Postgres also assumes that
311  *
312  *      sizeof(char) == 1
313  *
314  *      and that
315  *
316  *      sizeof(short) == 2
317  *
318  *      If your machine meets these requirements, Datums should also be checked
319  *      to see if the positioning is correct.
320  */
321
322 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
323 typedef Datum *DatumPtr;
324
325 #define GET_1_BYTE(datum)       (((Datum) (datum)) & 0x000000ff)
326 #define GET_2_BYTES(datum)      (((Datum) (datum)) & 0x0000ffff)
327 #define GET_4_BYTES(datum)      (((Datum) (datum)) & 0xffffffff)
328 #define SET_1_BYTE(value)       (((Datum) (value)) & 0x000000ff)
329 #define SET_2_BYTES(value)      (((Datum) (value)) & 0x0000ffff)
330 #define SET_4_BYTES(value)      (((Datum) (value)) & 0xffffffff)
331
332 /*
333  * DatumGetBool
334  *              Returns boolean value of a datum.
335  *
336  * Note: any nonzero value will be considered TRUE.
337  */
338
339 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
340
341 /*
342  * BoolGetDatum
343  *              Returns datum representation for a boolean.
344  *
345  * Note: any nonzero value will be considered TRUE.
346  */
347
348 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
349
350 /*
351  * DatumGetChar
352  *              Returns character value of a datum.
353  */
354
355 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
356
357 /*
358  * CharGetDatum
359  *              Returns datum representation for a character.
360  */
361
362 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
363
364 /*
365  * Int8GetDatum
366  *              Returns datum representation for an 8-bit integer.
367  */
368
369 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
370
371 /*
372  * DatumGetUInt8
373  *              Returns 8-bit unsigned integer value of a datum.
374  */
375
376 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
377
378 /*
379  * UInt8GetDatum
380  *              Returns datum representation for an 8-bit unsigned integer.
381  */
382
383 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
384
385 /*
386  * DatumGetInt16
387  *              Returns 16-bit integer value of a datum.
388  */
389
390 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
391
392 /*
393  * Int16GetDatum
394  *              Returns datum representation for a 16-bit integer.
395  */
396
397 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
398
399 /*
400  * DatumGetUInt16
401  *              Returns 16-bit unsigned integer value of a datum.
402  */
403
404 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
405
406 /*
407  * UInt16GetDatum
408  *              Returns datum representation for a 16-bit unsigned integer.
409  */
410
411 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
412
413 /*
414  * DatumGetInt32
415  *              Returns 32-bit integer value of a datum.
416  */
417
418 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
419
420 /*
421  * Int32GetDatum
422  *              Returns datum representation for a 32-bit integer.
423  */
424
425 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
426
427 /*
428  * DatumGetUInt32
429  *              Returns 32-bit unsigned integer value of a datum.
430  */
431
432 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
433
434 /*
435  * UInt32GetDatum
436  *              Returns datum representation for a 32-bit unsigned integer.
437  */
438
439 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
440
441 /*
442  * DatumGetObjectId
443  *              Returns object identifier value of a datum.
444  */
445
446 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
447
448 /*
449  * ObjectIdGetDatum
450  *              Returns datum representation for an object identifier.
451  */
452
453 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
454
455 /*
456  * DatumGetPointer
457  *              Returns pointer value of a datum.
458  */
459
460 #define DatumGetPointer(X) ((Pointer) (X))
461
462 /*
463  * PointerGetDatum
464  *              Returns datum representation for a pointer.
465  */
466
467 #define PointerGetDatum(X) ((Datum) (X))
468
469 /*
470  * DatumGetCString
471  *              Returns C string (null-terminated string) value of a datum.
472  *
473  * Note: C string is not a full-fledged Postgres type at present,
474  * but type input functions use this conversion for their inputs.
475  */
476
477 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
478
479 /*
480  * CStringGetDatum
481  *              Returns datum representation for a C string (null-terminated string).
482  *
483  * Note: C string is not a full-fledged Postgres type at present,
484  * but type output functions use this conversion for their outputs.
485  * Note: CString is pass-by-reference; caller must ensure the pointed-to
486  * value has adequate lifetime.
487  */
488
489 #define CStringGetDatum(X) PointerGetDatum(X)
490
491 /*
492  * DatumGetName
493  *              Returns name value of a datum.
494  */
495
496 #define DatumGetName(X) ((Name) DatumGetPointer(X))
497
498 /*
499  * NameGetDatum
500  *              Returns datum representation for a name.
501  *
502  * Note: Name is pass-by-reference; caller must ensure the pointed-to
503  * value has adequate lifetime.
504  */
505
506 #define NameGetDatum(X) PointerGetDatum(X)
507
508 /*
509  * DatumGetInt64
510  *              Returns 64-bit integer value of a datum.
511  *
512  * Note: this macro hides the fact that int64 is currently a
513  * pass-by-reference type.  Someday it may be pass-by-value,
514  * at least on some platforms.
515  */
516
517 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
518
519 /*
520  * Int64GetDatum
521  *              Returns datum representation for a 64-bit integer.
522  *
523  * Note: this routine returns a reference to palloc'd space.
524  */
525
526 extern Datum Int64GetDatum(int64 X);
527
528 /*
529  * DatumGetFloat4
530  *              Returns 4-byte floating point value of a datum.
531  *
532  * Note: this macro hides the fact that float4 is currently a
533  * pass-by-reference type.  Someday it may be pass-by-value.
534  */
535
536 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
537
538 /*
539  * Float4GetDatum
540  *              Returns datum representation for a 4-byte floating point number.
541  *
542  * Note: this routine returns a reference to palloc'd space.
543  */
544
545 extern Datum Float4GetDatum(float4 X);
546
547 /*
548  * DatumGetFloat8
549  *              Returns 8-byte floating point value of a datum.
550  *
551  * Note: this macro hides the fact that float8 is currently a
552  * pass-by-reference type.  Someday it may be pass-by-value,
553  * at least on some platforms.
554  */
555
556 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
557
558 /*
559  * Float8GetDatum
560  *              Returns datum representation for an 8-byte floating point number.
561  *
562  * Note: this routine returns a reference to palloc'd space.
563  */
564
565 extern Datum Float8GetDatum(float8 X);
566
567
568 /*
569  * DatumGetFloat32
570  *              Returns 32-bit floating point value of a datum.
571  *              This is really a pointer, of course.
572  *
573  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
574  * It will eventually go away.
575  */
576
577 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
578
579 /*
580  * Float32GetDatum
581  *              Returns datum representation for a 32-bit floating point number.
582  *              This is really a pointer, of course.
583  *
584  * XXX: this macro is now deprecated in favor of Float4GetDatum.
585  * It will eventually go away.
586  */
587
588 #define Float32GetDatum(X) PointerGetDatum(X)
589
590 /*
591  * DatumGetFloat64
592  *              Returns 64-bit floating point value of a datum.
593  *              This is really a pointer, of course.
594  *
595  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
596  * It will eventually go away.
597  */
598
599 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
600
601 /*
602  * Float64GetDatum
603  *              Returns datum representation for a 64-bit floating point number.
604  *              This is really a pointer, of course.
605  *
606  * XXX: this macro is now deprecated in favor of Float8GetDatum.
607  * It will eventually go away.
608  */
609
610 #define Float64GetDatum(X) PointerGetDatum(X)
611
612 /*
613  * Int64GetDatumFast
614  * Float4GetDatumFast
615  * Float8GetDatumFast
616  *
617  * These macros are intended to allow writing code that does not depend on
618  * whether int64, float4, float8 are pass-by-reference types, while not
619  * sacrificing performance when they are.  The argument must be a variable
620  * that will exist and have the same value for as long as the Datum is needed.
621  * In the pass-by-ref case, the address of the variable is taken to use as
622  * the Datum.  In the pass-by-val case, these will be the same as the non-Fast
623  * macros.
624  */
625
626 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
627 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
628 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
629
630
631 /* ----------------------------------------------------------------
632  *                              Section 5:      IsValid macros for system types
633  * ----------------------------------------------------------------
634  */
635 /*
636  * BoolIsValid
637  *              True iff bool is valid.
638  */
639 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
640
641 /*
642  * PointerIsValid
643  *              True iff pointer is valid.
644  */
645 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
646
647 /*
648  * PointerIsAligned
649  *              True iff pointer is properly aligned to point to the given type.
650  */
651 #define PointerIsAligned(pointer, type) \
652                 (((long)(pointer) % (sizeof (type))) == 0)
653
654 /* ----------------------------------------------------------------
655  *                              Section 6:      offsetof, lengthof, endof
656  * ----------------------------------------------------------------
657  */
658 /*
659  * offsetof
660  *              Offset of a structure/union field within that structure/union.
661  *
662  *              XXX This is supposed to be part of stddef.h, but isn't on
663  *              some systems (like SunOS 4).
664  */
665 #ifndef offsetof
666 #define offsetof(type, field)   ((long) &((type *)0)->field)
667 #endif   /* offsetof */
668
669 /*
670  * lengthof
671  *              Number of elements in an array.
672  */
673 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
674
675 /*
676  * endof
677  *              Address of the element one past the last in an array.
678  */
679 #define endof(array)    (&array[lengthof(array)])
680
681 /* ----------------------------------------------------------------
682  *                              Section 7:      exception handling definitions
683  *                                                      Assert, Trap, etc macros
684  * ----------------------------------------------------------------
685  */
686 /*
687  * Exception Handling definitions
688  */
689
690 typedef char *ExcMessage;
691 typedef struct Exception
692 {
693         ExcMessage      message;
694 } Exception;
695
696 /*
697  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
698  * - plai  9/5/90
699  *
700  * It should _NOT_ be defined in releases or in benchmark copies
701  */
702
703 /*
704  * Trap
705  *              Generates an exception if the given condition is true.
706  *
707  */
708 #define Trap(condition, exception) \
709                 do { \
710                         if ((assert_enabled) && (condition)) \
711                                 ExceptionalCondition(CppAsString(condition), &(exception), \
712                                                 (char*)NULL, __FILE__, __LINE__); \
713                 } while (0)
714
715 /*
716  *      TrapMacro is the same as Trap but it's intended for use in macros:
717  *
718  *              #define foo(x) (AssertM(x != 0) && bar(x))
719  *
720  *      Isn't CPP fun?
721  */
722 #define TrapMacro(condition, exception) \
723         ((bool) ((! assert_enabled) || ! (condition) || \
724                          (ExceptionalCondition(CppAsString(condition), \
725                                                                   &(exception), \
726                                                                   (char*) NULL, __FILE__, __LINE__))))
727
728 #ifndef USE_ASSERT_CHECKING
729 #define Assert(condition)
730 #define AssertMacro(condition)  ((void)true)
731 #define AssertArg(condition)
732 #define AssertState(condition)
733 #define assert_enabled 0
734 #else
735 #define Assert(condition) \
736                 Trap(!(condition), FailedAssertion)
737
738 #define AssertMacro(condition) \
739                 ((void) TrapMacro(!(condition), FailedAssertion))
740
741 #define AssertArg(condition) \
742                 Trap(!(condition), BadArg)
743
744 #define AssertState(condition) \
745                 Trap(!(condition), BadState)
746
747 extern bool     assert_enabled;
748
749 #endif   /* USE_ASSERT_CHECKING */
750
751 /*
752  * LogTrap
753  *              Generates an exception with a message if the given condition is true.
754  *
755  */
756 #define LogTrap(condition, exception, printArgs) \
757                 do { \
758                         if ((assert_enabled) && (condition)) \
759                                 ExceptionalCondition(CppAsString(condition), &(exception), \
760                                                 vararg_format printArgs, __FILE__, __LINE__); \
761                 } while (0)
762
763 /*
764  *      LogTrapMacro is the same as LogTrap but it's intended for use in macros:
765  *
766  *              #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
767  */
768 #define LogTrapMacro(condition, exception, printArgs) \
769         ((bool) ((! assert_enabled) || ! (condition) || \
770                          (ExceptionalCondition(CppAsString(condition), \
771                                                                    &(exception), \
772                                                                    vararg_format printArgs, __FILE__, __LINE__))))
773
774 #ifndef USE_ASSERT_CHECKING
775 #define LogAssert(condition, printArgs)
776 #define LogAssertMacro(condition, printArgs) true
777 #define LogAssertArg(condition, printArgs)
778 #define LogAssertState(condition, printArgs)
779 #else
780 #define LogAssert(condition, printArgs) \
781                 LogTrap(!(condition), FailedAssertion, printArgs)
782
783 #define LogAssertMacro(condition, printArgs) \
784                 LogTrapMacro(!(condition), FailedAssertion, printArgs)
785
786 #define LogAssertArg(condition, printArgs) \
787                 LogTrap(!(condition), BadArg, printArgs)
788
789 #define LogAssertState(condition, printArgs) \
790                 LogTrap(!(condition), BadState, printArgs)
791
792 #ifdef ASSERT_CHECKING_TEST
793 extern int      assertTest(int val);
794
795 #endif
796 #endif   /* USE_ASSERT_CHECKING */
797
798 /* ----------------------------------------------------------------
799  *                              Section 8:      Min, Max, Abs macros
800  * ----------------------------------------------------------------
801  */
802 /*
803  * Max
804  *              Return the maximum of two numbers.
805  */
806 #define Max(x, y)               ((x) > (y) ? (x) : (y))
807
808 /*
809  * Min
810  *              Return the minimum of two numbers.
811  */
812 #define Min(x, y)               ((x) < (y) ? (x) : (y))
813
814 /*
815  * Abs
816  *              Return the absolute value of the argument.
817  */
818 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
819
820 /*
821  * StrNCpy
822  *      Like standard library function strncpy(), except that result string
823  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
824  *      of the source string will be kept.
825  *      Also, the macro returns no result (too hard to do that without
826  *      evaluating the arguments multiple times, which seems worse).
827  *
828  *      BTW: when you need to copy a non-null-terminated string (like a text
829  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
830  *      might seem to work, but it fetches one byte more than there is in the
831  *      text object.  One fine day you'll have a SIGSEGV because there isn't
832  *      another byte before the end of memory.  Don't laugh, we've had real
833  *      live bug reports from real live users over exactly this mistake.
834  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
835  */
836 #define StrNCpy(dst,src,len) \
837         do \
838         { \
839                 char * _dst = (dst); \
840                 Size _len = (len); \
841 \
842                 if (_len > 0) \
843                 { \
844                         strncpy(_dst, (src), _len); \
845                         _dst[_len-1] = '\0'; \
846                 } \
847         } while (0)
848
849
850 /* Get a bit mask of the bits set in non-int32 aligned addresses */
851 #define INT_ALIGN_MASK (sizeof(int32) - 1)
852
853 /*
854  * MemSet
855  *      Exactly the same as standard library function memset(), but considerably
856  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
857  *      This has to be a macro because the main point is to avoid function-call
858  *      overhead.
859  *
860  *      We got the 64 number by testing this against the stock memset() on
861  *      BSD/OS 3.0. Larger values were slower.  bjm 1997/09/11
862  *
863  *      I think the crossover point could be a good deal higher for
864  *      most platforms, actually.  tgl 2000-03-19
865  */
866 #define MemSet(start, val, len) \
867         do \
868         { \
869                 int32 * _start = (int32 *) (start); \
870                 int             _val = (val); \
871                 Size    _len = (len); \
872 \
873                 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
874                         (_len & INT_ALIGN_MASK) == 0 && \
875                         _val == 0 && \
876                         _len <= MEMSET_LOOP_LIMIT) \
877                 { \
878                         int32 * _stop = (int32 *) ((char *) _start + _len); \
879                         while (_start < _stop) \
880                                 *_start++ = 0; \
881                 } \
882                 else \
883                         memset((char *) _start, _val, _len); \
884         } while (0)
885
886 #define MEMSET_LOOP_LIMIT  64
887
888
889 /* ----------------------------------------------------------------
890  *                              Section 9: externs
891  * ----------------------------------------------------------------
892  */
893
894 extern Exception FailedAssertion;
895 extern Exception BadArg;
896 extern Exception BadState;
897
898 /* in utils/error/assert.c */
899 extern int ExceptionalCondition(char *conditionName,
900                                          Exception *exceptionP, char *details,
901                                          char *fileName, int lineNumber);
902
903
904 /* ----------------
905  *              vararg_format is used by assert and the exception handling stuff
906  * ----------------
907  */
908 extern char *vararg_format(const char *fmt,...);
909
910
911
912 /* ----------------------------------------------------------------
913  *                              Section 10: berkeley-specific configuration
914  *
915  * this section contains settings which are only relevant to the UC Berkeley
916  * sites.  Other sites can ignore this
917  * ----------------------------------------------------------------
918  */
919
920 /* ----------------
921  *              storage managers
922  *
923  *              These are experimental and are not supported in the code that
924  *              we distribute to other sites.
925  * ----------------
926  */
927 #ifdef NOT_USED
928 #define STABLE_MEMORY_STORAGE
929 #endif
930
931
932
933 /* ----------------------------------------------------------------
934  *                              Section 11: system-specific hacks
935  *
936  *              This should be limited to things that absolutely have to be
937  *              included in every source file.  The changes should be factored
938  *              into a separate file so that changes to one port don't require
939  *              changes to c.h (and everyone recompiling their whole system).
940  * ----------------------------------------------------------------
941  */
942
943 #ifdef __CYGWIN__
944 #define PG_BINARY       O_BINARY
945 #define PG_BINARY_R     "rb"
946 #define PG_BINARY_W     "wb"
947 #else
948 #define PG_BINARY       0
949 #define PG_BINARY_R     "r"
950 #define PG_BINARY_W     "w"
951 #endif
952
953 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
954 #define memmove(d, s, l)                bcopy(s, d, l)
955 #include <unistd.h>
956 #include <varargs.h>
957 #endif
958
959 /* These are for things that are one way on Unix and another on NT */
960 #define NULL_DEV                "/dev/null"
961 #define SEP_CHAR                '/'
962
963 /* defines for dynamic linking on Win32 platform */
964 #ifdef __CYGWIN__
965 #if __GNUC__ && ! defined (__declspec)
966 #error You need egcs 1.1 or newer for compiling!
967 #endif
968 #ifdef BUILDING_DLL
969 #define DLLIMPORT __declspec (dllexport)
970 #else                                                   /* not BUILDING_DLL */
971 #define DLLIMPORT __declspec (dllimport)
972 #endif
973 #else                                                   /* not CYGWIN */
974 #define DLLIMPORT
975 #endif
976
977 /* Provide prototypes for routines not present in a particular machine's
978  * standard C library.  It'd be better to put these in config.h, but
979  * in config.h we haven't yet included anything that defines size_t...
980  */
981
982 #ifndef HAVE_SNPRINTF_DECL
983 extern int      snprintf(char *str, size_t count, const char *fmt,...);
984
985 #endif
986
987 #ifndef HAVE_VSNPRINTF_DECL
988 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
989
990 #endif
991
992 #ifndef HAVE_MEMMOVE
993 #include <regex/utils.h>
994 #endif
995
996 /* ----------------
997  *              end of c.h
998  * ----------------
999  */
1000 #endif   /* C_H */