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