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