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