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