]> granicus.if.org Git - postgresql/blob - src/include/c.h
Massive commit to run PGINDENT on all *.c and *.h files.
[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  * Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: c.h,v 1.17 1997/09/07 04:55:35 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 /*
15  *       TABLE OF CONTENTS
16  *
17  *              When adding stuff to this file, please try and put stuff
18  *              into the relevant section, or add new sections as appropriate.
19  *
20  *        section       description
21  *        -------       ------------------------------------------------
22  *              1)              bool, true, false, TRUE, FALSE
23  *              2)              __STDC__, non-ansi C definitions:
24  *                              Pointer typedef, NULL
25  *                              cpp magic macros
26  *                              type prefixes: const, signed, volatile, inline
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  *       NOTES
38  *
39  *              This file is MACHINE AND COMPILER dependent!!!  (For now.)
40  *
41  * ----------------------------------------------------------------
42  */
43 #ifndef C_H
44 #define C_H
45
46 /* We have to include stdlib.h here because it defines many of these macros
47    on some platforms, and we only want our definitions used if stdlib.h doesn't
48    have its own.
49 */
50 #include <stdlib.h>
51
52 /* ----------------------------------------------------------------
53  *                              Section 1:      bool, true, false, TRUE, FALSE
54  * ----------------------------------------------------------------
55  */
56 /*
57  * bool --
58  *              Boolean value, either true or false.
59  *
60  */
61 #define false   ((char) 0)
62 #define true    ((char) 1)
63 #ifndef __cplusplus
64 typedef char    bool;
65
66 #endif                                                  /* not C++ */
67 typedef bool   *BoolPtr;
68
69 #ifndef TRUE
70 #define TRUE    1
71 #endif                                                  /* TRUE */
72
73 #ifndef FALSE
74 #define FALSE   0
75 #endif                                                  /* FALSE */
76
77 /* ----------------------------------------------------------------
78  *                              Section 2: __STDC__, non-ansi C definitions:
79  *
80  *                              cpp magic macros
81  *                              Pointer typedef, NULL
82  *                              type prefixes: const, signed, volatile, inline
83  * ----------------------------------------------------------------
84  */
85
86 #ifdef  __STDC__                                /* ANSI C */
87
88 /*
89  * Pointer --
90  *              Variable holding address of any memory resident object.
91  */
92
93 /*
94  *              XXX Pointer arithmetic is done with this, so it can't be void *
95  *              under "true" ANSI compilers.
96  */
97 typedef char   *Pointer;
98
99 #ifndef NULL
100 /*
101  * NULL --
102  *              Null pointer.
103  */
104 #define NULL    ((void *) 0)
105 #endif                                                  /* !defined(NULL) */
106
107 #define HAVE_ANSI_CPP                   /* all ANSI C compilers must have this! */
108 #if defined(NEED_STD_HDRS)
109 #undef NEED_STD_HDRS                    /* all ANSI systems must have
110                                                                  * stddef/stdlib */
111 #endif                                                  /* NEED_STD_HDRS */
112
113 #else   /* !defined(__STDC__) *//* NOT ANSI C */
114
115 /*
116  * Pointer --
117  *              Variable containing address of any memory resident object.
118  */
119 typedef char   *Pointer;
120
121 #ifndef NULL
122 /*
123  * NULL --
124  *              Null pointer.
125  */
126 #define NULL    0
127 #endif                                                  /* !defined(NULL) */
128
129 /*
130  * const --
131  *              Type modifier.  Identifies read only variables.
132  *
133  * Example:
134  *              extern const Version    RomVersion;
135  */
136 #define const                                   /* const */
137
138 /*
139  * signed --
140  *              Type modifier.  Identifies signed integral types.
141  */
142 #define signed                                  /* signed */
143
144 /*
145  * volatile --
146  *              Type modifier.  Identifies variables which may change in ways not
147  *              noticeable by the compiler, e.g. via asynchronous interrupts.
148  *
149  * Example:
150  *              extern volatile unsigned int    NumberOfInterrupts;
151  */
152 #define volatile                                /* volatile */
153
154 #endif  /* !defined(__STDC__) *//* NOT ANSI C */
155
156 /*
157  * CppAsString --
158  *              Convert the argument to a string, using the C preprocessor.
159  * CppConcat --
160  *              Concatenate two arguments together, using the C preprocessor.
161  */
162 #if defined(HAVE_ANSI_CPP)
163
164 #define CppAsString(identifier) #identifier
165 #define CppConcat(x, y)                 x##y
166
167 #else                                                   /* !HAVE_ANSI_CPP */
168
169 #define CppAsString(identifier) "identifier"
170
171 /*
172  * CppIdentity -- On Reiser based cpp's this is used to concatenate
173  *              two tokens.  That is
174  *                              CppIdentity(A)B ==> AB
175  *              We renamed it to _private_CppIdentity because it should not
176  *              be referenced outside this file.  On other cpp's it
177  *              produces  A  B.
178  */
179 #define _priv_CppIdentity(x)x
180 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
181
182 #endif                                                  /* !HAVE_ANSI_CPP */
183
184 #ifndef __GNUC__                                /* GNU cc */
185 #define inline
186 #endif
187
188 #if defined(NEED_STD_HDRS)
189 /*
190  * You're doomed.  We've removed almost all of our own C library
191  * extern declarations because they conflict on the different
192  * systems.  You'll have to write your own stdlib.h.
193  */
194 #include "stdlib.h"
195 #else                                                   /* NEED_STD_HDRS */
196 #include <stddef.h>
197 #include <stdlib.h>
198 #endif                                                  /* NEED_STD_HDRS */
199
200 /* ----------------------------------------------------------------
201  *                              Section 3:      standard system types
202  * ----------------------------------------------------------------
203  */
204
205 /*
206  * intN --
207  *              Signed integer, AT LEAST N BITS IN SIZE,
208  *              used for numerical computations.
209  */
210 typedef signed char int8;               /* >= 8 bits */
211 typedef signed short int16;             /* >= 16 bits */
212 typedef signed int int32;               /* >= 32 bits */
213
214 /*
215  * uintN --
216  *              Unsigned integer, AT LEAST N BITS IN SIZE,
217  *              used for numerical computations.
218  */
219 typedef unsigned char uint8;    /* >= 8 bits */
220 typedef unsigned short uint16;  /* >= 16 bits */
221 typedef unsigned int uint32;    /* >= 32 bits */
222
223 /*
224  * floatN --
225  *              Floating point number, AT LEAST N BITS IN SIZE,
226  *              used for numerical computations.
227  *
228  *              Since sizeof(floatN) may be > sizeof(char *), always pass
229  *              floatN by reference.
230  */
231 typedef float   float32data;
232 typedef double  float64data;
233 typedef float  *float32;
234 typedef double *float64;
235
236 /*
237  * boolN --
238  *              Boolean value, AT LEAST N BITS IN SIZE.
239  */
240 typedef uint8   bool8;                  /* >= 8 bits */
241 typedef uint16  bool16;                 /* >= 16 bits */
242 typedef uint32  bool32;                 /* >= 32 bits */
243
244 /*
245  * bitsN --
246  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
247  */
248 typedef uint8   bits8;                  /* >= 8 bits */
249 typedef uint16  bits16;                 /* >= 16 bits */
250 typedef uint32  bits32;                 /* >= 32 bits */
251
252 /*
253  * wordN --
254  *              Unit of storage, AT LEAST N BITS IN SIZE,
255  *              used to fetch/store data.
256  */
257 typedef uint8   word8;                  /* >= 8 bits */
258 typedef uint16  word16;                 /* >= 16 bits */
259 typedef uint32  word32;                 /* >= 32 bits */
260
261 /*
262  * Size --
263  *              Size of any memory resident object, as returned by sizeof.
264  */
265 typedef unsigned int Size;
266
267 /*
268  * Index --
269  *              Index into any memory resident array.
270  *
271  * Note:
272  *              Indices are non negative.
273  */
274 typedef unsigned int Index;
275
276 #define MAXDIM 6
277 typedef struct
278 {
279         int                             indx[MAXDIM];
280 }                               IntArray;
281
282 /*
283  * Offset --
284  *              Offset into any memory resident array.
285  *
286  * Note:
287  *              This differs from an Index in that an Index is always
288  *              non negative, whereas Offset may be negative.
289  */
290 typedef signed int Offset;
291
292 /* ----------------------------------------------------------------
293  *                              Section 4:      datum type + support macros
294  * ----------------------------------------------------------------
295  */
296 /*
297  * datum.h --
298  *              POSTGRES abstract data type datum representation definitions.
299  *
300  * Note:
301  *
302  * Port Notes:
303  *      Postgres makes the following assumption about machines:
304  *
305  *      sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
306  *
307  *      Postgres also assumes that
308  *
309  *      sizeof(char) == 1
310  *
311  *      and that
312  *
313  *      sizeof(short) == 2
314  *
315  *      If your machine meets these requirements, Datums should also be checked
316  *      to see if the positioning is correct.
317  *
318  *              This file is MACHINE AND COMPILER dependent!!!
319  */
320
321 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
322 typedef Datum  *DatumPtr;
323
324 #define GET_1_BYTE(datum)       (((Datum) (datum)) & 0x000000ff)
325 #define GET_2_BYTES(datum)      (((Datum) (datum)) & 0x0000ffff)
326 #define GET_4_BYTES(datum)      (((Datum) (datum)) & 0xffffffff)
327 #define SET_1_BYTE(value)       (((Datum) (value)) & 0x000000ff)
328 #define SET_2_BYTES(value)      (((Datum) (value)) & 0x0000ffff)
329 #define SET_4_BYTES(value)      (((Datum) (value)) & 0xffffffff)
330
331 /*
332  * DatumGetChar --
333  *              Returns character value of a datum.
334  */
335
336 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
337
338 /*
339  * CharGetDatum --
340  *              Returns datum representation for a character.
341  */
342
343 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
344
345 /*
346  * Int8GetDatum --
347  *              Returns datum representation for an 8-bit integer.
348  */
349
350 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
351
352 /*
353  * DatumGetUInt8 --
354  *              Returns 8-bit unsigned integer value of a datum.
355  */
356
357 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
358
359 /*
360  * UInt8GetDatum --
361  *              Returns datum representation for an 8-bit unsigned integer.
362  */
363
364 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
365
366 /*
367  * DatumGetInt16 --
368  *              Returns 16-bit integer value of a datum.
369  */
370
371 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
372
373 /*
374  * Int16GetDatum --
375  *              Returns datum representation for a 16-bit integer.
376  */
377
378 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
379
380 /*
381  * DatumGetUInt16 --
382  *              Returns 16-bit unsigned integer value of a datum.
383  */
384
385 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
386
387 /*
388  * UInt16GetDatum --
389  *              Returns datum representation for a 16-bit unsigned integer.
390  */
391
392 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
393
394 /*
395  * DatumGetInt32 --
396  *              Returns 32-bit integer value of a datum.
397  */
398
399 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
400
401 /*
402  * Int32GetDatum --
403  *              Returns datum representation for a 32-bit integer.
404  */
405
406 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
407
408 /*
409  * DatumGetUInt32 --
410  *              Returns 32-bit unsigned integer value of a datum.
411  */
412
413 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
414
415 /*
416  * UInt32GetDatum --
417  *              Returns datum representation for a 32-bit unsigned integer.
418  */
419
420 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
421
422 /*
423  * DatumGetObjectId --
424  *              Returns object identifier value of a datum.
425  */
426
427 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
428
429 /*
430  * ObjectIdGetDatum --
431  *              Returns datum representation for an object identifier.
432  */
433
434 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
435
436 /*
437  * DatumGetPointer --
438  *              Returns pointer value of a datum.
439  */
440
441 #define DatumGetPointer(X) ((Pointer) X)
442
443 /*
444  * PointerGetDatum --
445  *              Returns datum representation for a pointer.
446  */
447
448 #define PointerGetDatum(X) ((Datum) X)
449
450 /*
451  * DatumGetName --
452  *              Returns name value of a datum.
453  */
454
455 #define DatumGetName(X) ((Name) DatumGetPointer((Datum) X))
456
457 /*
458  * NameGetDatum --
459  *              Returns datum representation for a name.
460  */
461
462 #define NameGetDatum(X) PointerGetDatum((Pointer) X)
463
464
465 /*
466  * DatumGetFloat32 --
467  *              Returns 32-bit floating point value of a datum.
468  *              This is really a pointer, of course.
469  */
470
471 #define DatumGetFloat32(X) ((float32) DatumGetPointer((Datum) X))
472
473 /*
474  * Float32GetDatum --
475  *              Returns datum representation for a 32-bit floating point number.
476  *              This is really a pointer, of course.
477  */
478
479 #define Float32GetDatum(X) PointerGetDatum((Pointer) X)
480
481 /*
482  * DatumGetFloat64 --
483  *              Returns 64-bit floating point value of a datum.
484  *              This is really a pointer, of course.
485  */
486
487 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
488
489 /*
490  * Float64GetDatum --
491  *              Returns datum representation for a 64-bit floating point number.
492  *              This is really a pointer, of course.
493  */
494
495 #define Float64GetDatum(X) PointerGetDatum((Pointer) X)
496
497 /* ----------------------------------------------------------------
498  *                              Section 5:      IsValid macros for system types
499  * ----------------------------------------------------------------
500  */
501 /*
502  * BoolIsValid --
503  *              True iff bool is valid.
504  */
505 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
506
507 /*
508  * PointerIsValid --
509  *              True iff pointer is valid.
510  */
511 #define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
512
513 /*
514  * PointerIsInBounds --
515  *              True iff pointer is within given bounds.
516  *
517  * Note:
518  *              Assumes the bounded interval to be [min,max),
519  *              i.e. closed on the left and open on the right.
520  */
521 #define PointerIsInBounds(pointer, min, max) \
522                 ((min) <= (pointer) && (pointer) < (max))
523
524 /*
525  * PointerIsAligned --
526  *              True iff pointer is properly aligned to point to the given type.
527  */
528 #define PointerIsAligned(pointer, type) \
529                 (((long)(pointer) % (sizeof (type))) == 0)
530
531 /* ----------------------------------------------------------------
532  *                              Section 6:      offsetof, lengthof, endof
533  * ----------------------------------------------------------------
534  */
535 /*
536  * offsetof --
537  *              Offset of a structure/union field within that structure/union.
538  *
539  *              XXX This is supposed to be part of stddef.h, but isn't on
540  *              some systems (like SunOS 4).
541  */
542 #ifndef offsetof
543 #define offsetof(type, field)   ((long) &((type *)0)->field)
544 #endif                                                  /* offsetof */
545
546 /*
547  * lengthof --
548  *              Number of elements in an array.
549  */
550 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
551
552 /*
553  * endof --
554  *              Address of the element one past the last in an array.
555  */
556 #define endof(array)    (&array[lengthof(array)])
557
558 /* ----------------------------------------------------------------
559  *                              Section 7:      exception handling definitions
560  *                                                      Assert, Trap, etc macros
561  * ----------------------------------------------------------------
562  */
563 /*
564  * Exception Handling definitions
565  */
566
567 typedef char   *ExcMessage;
568 typedef struct Exception
569 {
570         ExcMessage              message;
571 }                               Exception;
572
573 /*
574  * NO_ASSERT_CHECKING, if defined, turns off all the assertions.
575  * - plai  9/5/90
576  *
577  * It should _NOT_ be undef'ed in releases or in benchmark copies
578  *
579  * #undef NO_ASSERT_CHECKING
580  */
581
582 /*
583  * Trap --
584  *              Generates an exception if the given condition is true.
585  *
586  */
587 #define Trap(condition, exception) \
588                 { if (condition) \
589                                 ExceptionalCondition(CppAsString(condition), &(exception), \
590                                                 (char*)NULL, __FILE__, __LINE__); }
591
592 /*
593  *      TrapMacro is the same as Trap but it's intended for use in macros:
594  *
595  *              #define foo(x) (AssertM(x != 0) && bar(x))
596  *
597  *      Isn't CPP fun?
598  */
599 #define TrapMacro(condition, exception) \
600         ((bool) ((! condition) || \
601                          (ExceptionalCondition(CppAsString(condition), \
602                                                                   &(exception), \
603                                                                   (char*) NULL, __FILE__, __LINE__))))
604
605 #ifdef NO_ASSERT_CHECKING
606 #define Assert(condition)
607 #define AssertMacro(condition)  true
608 #define AssertArg(condition)
609 #define AssertState(condition)
610 #else
611 #define Assert(condition) \
612                 Trap(!(condition), FailedAssertion)
613
614 #define AssertMacro(condition) \
615                 TrapMacro(!(condition), FailedAssertion)
616
617 #define AssertArg(condition) \
618                 Trap(!(condition), BadArg)
619
620 #define AssertState(condition) \
621                 Trap(!(condition), BadState)
622
623 #endif                                                  /* NO_ASSERT_CHECKING */
624
625 /*
626  * LogTrap --
627  *              Generates an exception with a message if the given condition is true.
628  *
629  */
630 #define LogTrap(condition, exception, printArgs) \
631                 { if (condition) \
632                                 ExceptionalCondition(CppAsString(condition), &(exception), \
633                                                 form printArgs, __FILE__, __LINE__); }
634
635 /*
636  *      LogTrapMacro is the same as LogTrap but it's intended for use in macros:
637  *
638  *              #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
639  */
640 #define LogTrapMacro(condition, exception, printArgs) \
641         ((bool) ((! condition) || \
642                          (ExceptionalCondition(CppAsString(condition), \
643                                                                    &(exception), \
644                                                                    form printArgs, __FILE__, __LINE__))))
645
646 #ifdef NO_ASSERT_CHECKING
647 #define LogAssert(condition, printArgs)
648 #define LogAssertMacro(condition, printArgs) true
649 #define LogAssertArg(condition, printArgs)
650 #define LogAssertState(condition, printArgs)
651 #else
652 #define LogAssert(condition, printArgs) \
653                 LogTrap(!(condition), FailedAssertion, printArgs)
654
655 #define LogAssertMacro(condition, printArgs) \
656                 LogTrapMacro(!(condition), FailedAssertion, printArgs)
657
658 #define LogAssertArg(condition, printArgs) \
659                 LogTrap(!(condition), BadArg, printArgs)
660
661 #define LogAssertState(condition, printArgs) \
662                 LogTrap(!(condition), BadState, printArgs)
663
664 #endif                                                  /* NO_ASSERT_CHECKING */
665
666 /* ----------------------------------------------------------------
667  *                              Section 8:      Min, Max, Abs macros
668  * ----------------------------------------------------------------
669  */
670 /*
671  * Max --
672  *              Return the maximum of two numbers.
673  */
674 #define Max(x, y)               ((x) > (y) ? (x) : (y))
675
676 /*
677  * Min --
678  *              Return the minimum of two numbers.
679  */
680 #define Min(x, y)               ((x) < (y) ? (x) : (y))
681
682 /*
683  * Abs --
684  *              Return the absolute value of the argument.
685  */
686 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
687
688 /*
689  * strNcpy --
690  *              Does string copy, and forces terminating NULL
691  */
692 /* we do this so if the macro is used in an if action, it will work */
693 #define strNcpy(dst,src,len)    (strncpy((dst),(src),(len)),*((dst)+(len))='\0')
694
695 /* ----------------------------------------------------------------
696  *                              Section 9: externs
697  * ----------------------------------------------------------------
698  */
699
700 extern Exception FailedAssertion;
701 extern Exception BadArg;
702 extern Exception BadState;
703
704 /* in utils/error/assert.c */
705 extern int
706 ExceptionalCondition(char *conditionName,
707                                          Exception * exceptionP, char *details,
708                                          char *fileName, int lineNumber);
709
710
711 /* ----------------
712  *              form is used by assert and the exception handling stuff
713  * ----------------
714  */
715 extern char    *form(const char *fmt,...);
716
717
718
719 /* ----------------------------------------------------------------
720  *                              Section 10: berkeley-specific configuration
721  *
722  * this section contains settings which are only relevant to the UC Berkeley
723  * sites.  Other sites can ignore this
724  * ----------------------------------------------------------------
725  */
726
727 /* ----------------
728  *              storage managers
729  *
730  *              These are experimental and are not supported in the code that
731  *              we distribute to other sites.
732  * ----------------
733  */
734 #ifdef SEQUOIA
735 #define MAIN_MEMORY
736 #endif
737
738
739
740 /* ----------------------------------------------------------------
741  *                              Section 11: system-specific hacks
742  *
743  *              This should be limited to things that absolutely have to be
744  *              included in every source file.  The changes should be factored
745  *              into a separate file so that changes to one port don't require
746  *              changes to c.h (and everyone recompiling their whole system).
747  * ----------------------------------------------------------------
748  */
749
750 #ifdef FIXADE
751 #if defined(hpux)
752 #include "port/hpux/fixade.h"   /* for unaligned access fixup */
753 #endif                                                  /* hpux */
754 #endif
755
756 #if defined(sunos4)
757 #define memmove(d, s, l)                bcopy(s, d, l)
758 #include <unistd.h>
759 #endif
760
761 /* These are for things that are one way on Unix and another on NT */
762 #define NULL_DEV                "/dev/null"
763 #define COPY_CMD                "cp"
764 #define SEP_CHAR                '/'
765
766 /* ----------------
767  *              end of c.h
768  * ----------------
769  */
770 #endif                                                  /* C_H */