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