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