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