]> granicus.if.org Git - postgresql/blob - src/include/c.h
c.h needs to include postgres_ext.h to be self-contained.
[postgresql] / src / include / c.h
1 /*-------------------------------------------------------------------------
2  *
3  * c.h
4  *        Fundamental C definitions.  This is included by every .c file in
5  *        PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate).
6  *
7  *        Note that the definitions here are not intended to be exposed to clients of
8  *        the frontend interface libraries --- so we don't worry much about polluting
9  *        the namespace with lots of stuff...
10  *
11  *
12  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * $Id: c.h,v 1.100 2001/08/24 22:46:28 petere Exp $
16  *
17  *-------------------------------------------------------------------------
18  */
19 /*
20  *----------------------------------------------------------------
21  *       TABLE OF CONTENTS
22  *
23  *              When adding stuff to this file, please try to put stuff
24  *              into the relevant section, or add new sections as appropriate.
25  *
26  *        section       description
27  *        -------       ------------------------------------------------
28  *              0)              pg_config.h and standard system headers
29  *              1)              hacks to cope with non-ANSI C compilers
30  *              2)              bool, true, false, TRUE, FALSE, NULL
31  *              3)              standard system types
32  *              4)              IsValid macros for system types
33  *              5)              offsetof, lengthof, endof, alignment
34  *              6)              widely useful macros
35  *              7)              random stuff
36  *              8)              system-specific hacks
37  *
38  * NOTE: since this file is included by both frontend and backend modules, it's
39  * almost certainly wrong to put an "extern" declaration here.  typedefs and macros
40  * are the kind of thing that might go here.
41  *
42  *----------------------------------------------------------------
43  */
44 #ifndef C_H
45 #define C_H
46
47 /* We have to include stdlib.h here because it defines many of these macros
48    on some platforms, and we only want our definitions used if stdlib.h doesn't
49    have its own.  The same goes for stddef and stdarg if present.
50 */
51
52 #include "pg_config.h"
53 #include "postgres_ext.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <stddef.h>
59 #include <stdarg.h>
60 #ifdef STRING_H_WITH_STRINGS_H
61 #include <strings.h>
62 #endif
63
64 #ifdef __CYGWIN__
65 #include <errno.h>
66 #include <sys/fcntl.h>                  /* ensure O_BINARY is available */
67 #endif
68 #ifdef HAVE_SUPPORTDEFS_H
69 #include <SupportDefs.h>
70 #endif
71
72 #ifdef ENABLE_NLS
73 #include <libintl.h>
74 #else
75 #define gettext(x) (x)
76 #endif
77 #define gettext_noop(x) (x)
78
79
80 /* ----------------------------------------------------------------
81  *                              Section 1: hacks to cope with non-ANSI C compilers
82  *
83  * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
84  * ----------------------------------------------------------------
85  */
86
87 /*
88  * CppAsString
89  *              Convert the argument to a string, using the C preprocessor.
90  * CppConcat
91  *              Concatenate two arguments together, using the C preprocessor.
92  *
93  * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
94  * whether #identifier works, but if we have that we likely have ## too.
95  */
96 #if defined(HAVE_STRINGIZE)
97
98 #define CppAsString(identifier) #identifier
99 #define CppConcat(x, y)                 x##y
100
101 #else                                                   /* !HAVE_STRINGIZE */
102
103 #define CppAsString(identifier) "identifier"
104
105 /*
106  * CppIdentity -- On Reiser based cpp's this is used to concatenate
107  *              two tokens.  That is
108  *                              CppIdentity(A)B ==> AB
109  *              We renamed it to _private_CppIdentity because it should not
110  *              be referenced outside this file.  On other cpp's it
111  *              produces  A  B.
112  */
113 #define _priv_CppIdentity(x)x
114 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
115
116 #endif   /* !HAVE_STRINGIZE */
117
118 /*
119  * dummyret is used to set return values in macros that use ?: to make
120  * assignments.  gcc wants these to be void, other compilers like char
121  */
122 #ifdef __GNUC__                                 /* GNU cc */
123 #define dummyret        void
124 #else
125 #define dummyret        char
126 #endif
127
128 #ifndef __GNUC__
129 #define __attribute__(x)
130 #endif
131
132 /* ----------------------------------------------------------------
133  *                              Section 2:      bool, true, false, TRUE, FALSE, NULL
134  * ----------------------------------------------------------------
135  */
136 /*
137  * bool
138  *              Boolean value, either true or false.
139  *
140  */
141
142 /* BeOS defines bool already, but the compiler chokes on the
143  * #ifndef unless we wrap it in this check.
144  */
145 #ifndef __BEOS__
146
147 #ifndef __cplusplus
148 #ifndef bool
149 typedef char bool;
150
151 #endif   /* ndef bool */
152 #endif   /* not C++ */
153
154 #ifndef true
155 #define true    ((bool) 1)
156 #endif
157
158 #ifndef false
159 #define false   ((bool) 0)
160 #endif
161
162 #endif   /* __BEOS__ */
163
164 typedef bool *BoolPtr;
165
166 #ifndef TRUE
167 #define TRUE    1
168 #endif
169
170 #ifndef FALSE
171 #define FALSE   0
172 #endif
173
174 /*
175  * NULL
176  *              Null pointer.
177  */
178 #ifndef NULL
179 #define NULL    ((void *) 0)
180 #endif
181
182
183 /* ----------------------------------------------------------------
184  *                              Section 3:      standard system types
185  * ----------------------------------------------------------------
186  */
187
188 /*
189  * Pointer
190  *              Variable holding address of any memory resident object.
191  *
192  *              XXX Pointer arithmetic is done with this, so it can't be void *
193  *              under "true" ANSI compilers.
194  */
195 typedef char *Pointer;
196
197 /*
198  * intN
199  *              Signed integer, EXACTLY N BITS IN SIZE,
200  *              used for numerical computations and the
201  *              frontend/backend protocol.
202  */
203 #ifndef __BEOS__                                /* this shouldn't be required, but is is! */
204 typedef signed char int8;               /* == 8 bits */
205 typedef signed short int16;             /* == 16 bits */
206 typedef signed int int32;               /* == 32 bits */
207
208 #endif   /* __BEOS__ */
209
210 /*
211  * uintN
212  *              Unsigned integer, EXACTLY N BITS IN SIZE,
213  *              used for numerical computations and the
214  *              frontend/backend protocol.
215  */
216 #ifndef __BEOS__                                /* this shouldn't be required, but is is! */
217 typedef unsigned char uint8;    /* == 8 bits */
218 typedef unsigned short uint16;  /* == 16 bits */
219 typedef unsigned int uint32;    /* == 32 bits */
220
221 #endif   /* __BEOS__ */
222
223 /*
224  * boolN
225  *              Boolean value, AT LEAST N BITS IN SIZE.
226  */
227 typedef uint8 bool8;                    /* >= 8 bits */
228 typedef uint16 bool16;                  /* >= 16 bits */
229 typedef uint32 bool32;                  /* >= 32 bits */
230
231 /*
232  * bitsN
233  *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
234  */
235 typedef uint8 bits8;                    /* >= 8 bits */
236 typedef uint16 bits16;                  /* >= 16 bits */
237 typedef uint32 bits32;                  /* >= 32 bits */
238
239 /*
240  * wordN
241  *              Unit of storage, AT LEAST N BITS IN SIZE,
242  *              used to fetch/store data.
243  */
244 typedef uint8 word8;                    /* >= 8 bits */
245 typedef uint16 word16;                  /* >= 16 bits */
246 typedef uint32 word32;                  /* >= 32 bits */
247
248 /*
249  * floatN
250  *              Floating point number, AT LEAST N BITS IN SIZE,
251  *              used for numerical computations.
252  *
253  *              Since sizeof(floatN) may be > sizeof(char *), always pass
254  *              floatN by reference.
255  *
256  * XXX: these typedefs are now deprecated in favor of float4 and float8.
257  * They will eventually go away.
258  */
259 typedef float float32data;
260 typedef double float64data;
261 typedef float *float32;
262 typedef double *float64;
263
264 /*
265  * 64-bit integers
266  */
267 #ifndef __BEOS__                                /* this is already defined on BeOS */
268 #ifdef HAVE_LONG_INT_64
269 /* Plain "long int" fits, use it */
270 typedef long int int64;
271 typedef unsigned long int uint64;
272
273 #else
274 #ifdef HAVE_LONG_LONG_INT_64
275 /* We have working support for "long long int", use that */
276 typedef long long int int64;
277 typedef unsigned long long int uint64;
278
279 #else
280 /* Won't actually work, but fall back to long int so that code compiles */
281 typedef long int int64;
282 typedef unsigned long int uint64;
283
284 #define INT64_IS_BUSTED
285 #endif
286 #endif
287 #endif   /* __BEOS__ */
288
289 /*
290  * Size
291  *              Size of any memory resident object, as returned by sizeof.
292  */
293 typedef size_t Size;
294
295 /*
296  * Index
297  *              Index into any memory resident array.
298  *
299  * Note:
300  *              Indices are non negative.
301  */
302 typedef unsigned int Index;
303
304 /*
305  * Offset
306  *              Offset into any memory resident array.
307  *
308  * Note:
309  *              This differs from an Index in that an Index is always
310  *              non negative, whereas Offset may be negative.
311  */
312 typedef signed int Offset;
313
314 /*
315  * Common Postgres datatype names (as used in the catalogs)
316  */
317 typedef int16 int2;
318 typedef int32 int4;
319 typedef float float4;
320 typedef double float8;
321
322 /*
323  * Oid, RegProcedure, TransactionId, CommandId
324  */
325
326 /* typedef Oid is in postgres_ext.h */
327
328 /* unfortunately, both regproc and RegProcedure are used */
329 typedef Oid regproc;
330 typedef Oid RegProcedure;
331
332 typedef uint32 TransactionId;
333
334 typedef uint32 CommandId;
335
336 #define FirstCommandId  ((CommandId) 0)
337
338 /*
339  * Array indexing support
340  */
341 #define MAXDIM 6
342 typedef struct
343 {
344         int                     indx[MAXDIM];
345 } IntArray;
346
347 /* ----------------
348  *              Variable-length datatypes all share the 'struct varlena' header.
349  *
350  * NOTE: for TOASTable types, this is an oversimplification, since the value may be
351  * compressed or moved out-of-line.  However datatype-specific routines are mostly
352  * content to deal with de-TOASTed values only, and of course client-side routines
353  * should never see a TOASTed value.  See postgres.h for details of the TOASTed form.
354  * ----------------
355  */
356 struct varlena
357 {
358         int32           vl_len;
359         char            vl_dat[1];
360 };
361
362 #define VARHDRSZ                ((int32) sizeof(int32))
363
364 /*
365  * These widely-used datatypes are just a varlena header and the data bytes.
366  * There is no terminating null or anything like that --- the data length is
367  * always VARSIZE(ptr) - VARHDRSZ.
368  */
369 typedef struct varlena bytea;
370 typedef struct varlena text;
371 typedef struct varlena BpChar;  /* blank-padded char, ie SQL char(n) */
372 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
373
374 /*
375  * Fixed-length array types (these are not varlena's!)
376  */
377
378 typedef int2 int2vector[INDEX_MAX_KEYS];
379 typedef Oid oidvector[INDEX_MAX_KEYS];
380
381 /*
382  * We want NameData to have length NAMEDATALEN and int alignment,
383  * because that's how the data type 'name' is defined in pg_type.
384  * Use a union to make sure the compiler agrees.
385  */
386 typedef union nameData
387 {
388         char            data[NAMEDATALEN];
389         int                     alignmentDummy;
390 } NameData;
391 typedef NameData *Name;
392
393 #define NameStr(name)   ((name).data)
394
395
396 /* ----------------------------------------------------------------
397  *                              Section 4:      IsValid macros for system types
398  * ----------------------------------------------------------------
399  */
400 /*
401  * BoolIsValid
402  *              True iff bool is valid.
403  */
404 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
405
406 /*
407  * PointerIsValid
408  *              True iff pointer is valid.
409  */
410 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
411
412 /*
413  * PointerIsAligned
414  *              True iff pointer is properly aligned to point to the given type.
415  */
416 #define PointerIsAligned(pointer, type) \
417                 (((long)(pointer) % (sizeof (type))) == 0)
418
419 #define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
420
421 #define RegProcedureIsValid(p)  OidIsValid(p)
422
423
424 /* ----------------------------------------------------------------
425  *                              Section 5:      offsetof, lengthof, endof, alignment
426  * ----------------------------------------------------------------
427  */
428 /*
429  * offsetof
430  *              Offset of a structure/union field within that structure/union.
431  *
432  *              XXX This is supposed to be part of stddef.h, but isn't on
433  *              some systems (like SunOS 4).
434  */
435 #ifndef offsetof
436 #define offsetof(type, field)   ((long) &((type *)0)->field)
437 #endif   /* offsetof */
438
439 /*
440  * lengthof
441  *              Number of elements in an array.
442  */
443 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
444
445 /*
446  * endof
447  *              Address of the element one past the last in an array.
448  */
449 #define endof(array)    (&array[lengthof(array)])
450
451 /* ----------------
452  * Alignment macros: align a length or address appropriately for a given type.
453  *
454  * There used to be some incredibly crufty platform-dependent hackery here,
455  * but now we rely on the configure script to get the info for us. Much nicer.
456  *
457  * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
458  * That case seems extremely unlikely to occur in practice, however.
459  * ----------------
460  */
461
462 #define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
463
464 #define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
465 #define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
466 #define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
467 #define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
468 #define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
469
470
471 /* ----------------------------------------------------------------
472  *                              Section 6:      widely useful macros
473  * ----------------------------------------------------------------
474  */
475 /*
476  * Max
477  *              Return the maximum of two numbers.
478  */
479 #define Max(x, y)               ((x) > (y) ? (x) : (y))
480
481 /*
482  * Min
483  *              Return the minimum of two numbers.
484  */
485 #define Min(x, y)               ((x) < (y) ? (x) : (y))
486
487 /*
488  * Abs
489  *              Return the absolute value of the argument.
490  */
491 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
492
493 /*
494  * StrNCpy
495  *      Like standard library function strncpy(), except that result string
496  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
497  *      of the source string will be kept.
498  *      Also, the macro returns no result (too hard to do that without
499  *      evaluating the arguments multiple times, which seems worse).
500  *
501  *      BTW: when you need to copy a non-null-terminated string (like a text
502  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
503  *      might seem to work, but it fetches one byte more than there is in the
504  *      text object.  One fine day you'll have a SIGSEGV because there isn't
505  *      another byte before the end of memory.  Don't laugh, we've had real
506  *      live bug reports from real live users over exactly this mistake.
507  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
508  */
509 #define StrNCpy(dst,src,len) \
510         do \
511         { \
512                 char * _dst = (dst); \
513                 Size _len = (len); \
514 \
515                 if (_len > 0) \
516                 { \
517                         strncpy(_dst, (src), _len); \
518                         _dst[_len-1] = '\0'; \
519                 } \
520         } while (0)
521
522
523 /* Get a bit mask of the bits set in non-int32 aligned addresses */
524 #define INT_ALIGN_MASK (sizeof(int32) - 1)
525
526 /*
527  * MemSet
528  *      Exactly the same as standard library function memset(), but considerably
529  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
530  *      This has to be a macro because the main point is to avoid function-call
531  *      overhead.
532  *
533  *      We got the 64 number by testing this against the stock memset() on
534  *      BSD/OS 3.0. Larger values were slower.  bjm 1997/09/11
535  *
536  *      I think the crossover point could be a good deal higher for
537  *      most platforms, actually.  tgl 2000-03-19
538  */
539 #define MemSet(start, val, len) \
540         do \
541         { \
542                 int32 * _start = (int32 *) (start); \
543                 int             _val = (val); \
544                 Size    _len = (len); \
545 \
546                 if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
547                         (_len & INT_ALIGN_MASK) == 0 && \
548                         _val == 0 && \
549                         _len <= MEMSET_LOOP_LIMIT) \
550                 { \
551                         int32 * _stop = (int32 *) ((char *) _start + _len); \
552                         while (_start < _stop) \
553                                 *_start++ = 0; \
554                 } \
555                 else \
556                         memset((char *) _start, _val, _len); \
557         } while (0)
558
559 #define MEMSET_LOOP_LIMIT  64
560
561
562 /* ----------------------------------------------------------------
563  *                              Section 7:      random stuff
564  * ----------------------------------------------------------------
565  */
566
567 /* msb for char */
568 #define CSIGNBIT (0x80)
569
570 #define STATUS_OK                               (0)
571 #define STATUS_ERROR                    (-1)
572 #define STATUS_NOT_FOUND                (-2)
573 #define STATUS_INVALID                  (-3)
574 #define STATUS_UNCATALOGUED             (-4)
575 #define STATUS_REPLACED                 (-5)
576 #define STATUS_NOT_DONE                 (-6)
577 #define STATUS_BAD_PACKET               (-7)
578 #define STATUS_FOUND                    (1)
579
580
581 /* ----------------------------------------------------------------
582  *                              Section 8: system-specific hacks
583  *
584  *              This should be limited to things that absolutely have to be
585  *              included in every source file.  The port-specific header file
586  *              is usually a better place for this sort of thing.
587  * ----------------------------------------------------------------
588  */
589
590 #ifdef __CYGWIN__
591 #define PG_BINARY       O_BINARY
592 #define PG_BINARY_R "rb"
593 #define PG_BINARY_W "wb"
594 #else
595 #define PG_BINARY       0
596 #define PG_BINARY_R "r"
597 #define PG_BINARY_W "w"
598 #endif
599
600 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
601 #include <unistd.h>
602 #endif
603
604 /* These are for things that are one way on Unix and another on NT */
605 #define NULL_DEV                "/dev/null"
606
607 /* defines for dynamic linking on Win32 platform */
608 #ifdef __CYGWIN__
609 #if __GNUC__ && ! defined (__declspec)
610 #error You need egcs 1.1 or newer for compiling!
611 #endif
612 #ifdef BUILDING_DLL
613 #define DLLIMPORT __declspec (dllexport)
614 #else                                                   /* not BUILDING_DLL */
615 #define DLLIMPORT __declspec (dllimport)
616 #endif
617 #elif defined(WIN32) && defined(_MSC_VER)       /* not CYGWIN */
618 #if defined(_DLL)
619 #define DLLIMPORT __declspec (dllexport)
620 #else                                                   /* not _DLL */
621 #define DLLIMPORT __declspec (dllimport)
622 #endif
623 #else                                                   /* not CYGWIN, not MSVC */
624 #define DLLIMPORT
625 #endif
626
627 /* Provide prototypes for routines not present in a particular machine's
628  * standard C library.  It'd be better to put these in pg_config.h, but
629  * in pg_config.h we haven't yet included anything that defines size_t...
630  */
631
632 #ifndef HAVE_SNPRINTF_DECL
633 extern int      snprintf(char *str, size_t count, const char *fmt,...);
634
635 #endif
636
637 #ifndef HAVE_VSNPRINTF_DECL
638 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
639
640 #endif
641
642 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
643 #define memmove(d, s, c)                bcopy(s, d, c)
644 #endif
645
646 /* ----------------
647  *              end of c.h
648  * ----------------
649  */
650 #endif   /* C_H */