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