]> granicus.if.org Git - postgresql/blob - src/include/c.h
Back out use of palloc0 in place if palloc/MemSet. Seems constant len
[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
8  *        of the frontend interface libraries --- so we don't worry much about
9  *        polluting 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.131 2002/11/11 03:02:19 momjian 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
40  * macros are the kind of thing that might go here.
41  *
42  *----------------------------------------------------------------
43  */
44 #ifndef C_H
45 #define C_H
46
47 /*
48  * We have to include stdlib.h here because it defines many of these macros
49  * on some platforms, and we only want our definitions used if stdlib.h doesn't
50  * have its own.  The same goes for stddef and stdarg if present.
51  */
52
53 #include "pg_config.h"
54 #include "postgres_ext.h"
55
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <stddef.h>
60 #include <stdarg.h>
61 #ifdef HAVE_STRINGS_H
62 #include <strings.h>
63 #endif
64 #include <sys/types.h>
65
66 #ifdef __CYGWIN__
67 #include <errno.h>
68 #include <sys/fcntl.h>                  /* ensure O_BINARY is available */
69 #endif
70 #ifdef HAVE_SUPPORTDEFS_H
71 #include <SupportDefs.h>
72 #endif
73
74 /* Must be before gettext() games below */
75 #include <locale.h>
76
77 #ifdef ENABLE_NLS
78 #include <libintl.h>
79 #else
80 #define gettext(x) (x)
81 #endif
82 #define gettext_noop(x) (x)
83
84
85 /* ----------------------------------------------------------------
86  *                              Section 1: hacks to cope with non-ANSI C compilers
87  *
88  * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
89  * ----------------------------------------------------------------
90  */
91
92 /*
93  * CppAsString
94  *              Convert the argument to a string, using the C preprocessor.
95  * CppConcat
96  *              Concatenate two arguments together, using the C preprocessor.
97  *
98  * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
99  * whether #identifier works, but if we have that we likely have ## too.
100  */
101 #if defined(HAVE_STRINGIZE)
102
103 #define CppAsString(identifier) #identifier
104 #define CppConcat(x, y)                 x##y
105
106 #else                                                   /* !HAVE_STRINGIZE */
107
108 #define CppAsString(identifier) "identifier"
109
110 /*
111  * CppIdentity -- On Reiser based cpp's this is used to concatenate
112  *              two tokens.  That is
113  *                              CppIdentity(A)B ==> AB
114  *              We renamed it to _private_CppIdentity because it should not
115  *              be referenced outside this file.  On other cpp's it
116  *              produces  A  B.
117  */
118 #define _priv_CppIdentity(x)x
119 #define CppConcat(x, y)                 _priv_CppIdentity(x)y
120 #endif   /* !HAVE_STRINGIZE */
121
122 /*
123  * dummyret is used to set return values in macros that use ?: to make
124  * assignments.  gcc wants these to be void, other compilers like char
125  */
126 #ifdef __GNUC__                                 /* GNU cc */
127 #define dummyret        void
128 #else
129 #define dummyret        char
130 #endif
131
132 #ifndef __GNUC__
133 #define __attribute__(_arg_)
134 #endif
135
136 /* ----------------------------------------------------------------
137  *                              Section 2:      bool, true, false, TRUE, FALSE, NULL
138  * ----------------------------------------------------------------
139  */
140
141 /*
142  * bool
143  *              Boolean value, either true or false.
144  *
145  * XXX for C++ compilers, we assume the compiler has a compatible
146  * built-in definition of bool.
147  */
148
149 /* BeOS defines bool already, but the compiler chokes on the
150  * #ifndef unless we wrap it in this check.
151  */
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
293                                                                  * HAVE_LONG_LONG_INT_64 */
294
295 /* Won't actually work, but fall back to long int so that code compiles */
296 #ifndef HAVE_INT64
297 typedef long int int64;
298 #endif
299 #ifndef HAVE_UINT64
300 typedef unsigned long int uint64;
301 #endif
302
303 #define INT64_IS_BUSTED
304 #endif   /* not HAVE_LONG_INT_64 and not
305                                                                  * HAVE_LONG_LONG_INT_64 */
306
307 /* Decide if we need to decorate 64-bit constants */
308 #ifdef HAVE_LL_CONSTANTS
309 #define INT64CONST(x)  ((int64) x##LL)
310 #define UINT64CONST(x) ((uint64) x##LL)
311 #else
312 #define INT64CONST(x)  ((int64) x)
313 #define UINT64CONST(x) ((uint64) x)
314 #endif
315
316
317 /* Select timestamp representation (float8 or int64) */
318 #if defined(USE_INTEGER_DATETIMES) && !defined(INT64_IS_BUSTED)
319 #define HAVE_INT64_TIMESTAMP
320 #endif
321
322 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
323 #ifndef HAVE_SIG_ATOMIC_T
324 typedef int sig_atomic_t;
325 #endif
326
327 /*
328  * Size
329  *              Size of any memory resident object, as returned by sizeof.
330  */
331 typedef size_t Size;
332
333 /*
334  * Index
335  *              Index into any memory resident array.
336  *
337  * Note:
338  *              Indices are non negative.
339  */
340 typedef unsigned int Index;
341
342 /*
343  * Offset
344  *              Offset into any memory resident array.
345  *
346  * Note:
347  *              This differs from an Index in that an Index is always
348  *              non negative, whereas Offset may be negative.
349  */
350 typedef signed int Offset;
351
352 /*
353  * Common Postgres datatype names (as used in the catalogs)
354  */
355 typedef int16 int2;
356 typedef int32 int4;
357 typedef float float4;
358 typedef double float8;
359
360 /*
361  * Oid, RegProcedure, TransactionId, CommandId
362  */
363
364 /* typedef Oid is in postgres_ext.h */
365
366 /*
367  * regproc is the type name used in the include/catalog headers, but
368  * RegProcedure is the preferred name in C code.
369  */
370 typedef Oid regproc;
371 typedef regproc RegProcedure;
372
373 typedef uint32 TransactionId;
374
375 typedef uint32 CommandId;
376
377 #define FirstCommandId  ((CommandId) 0)
378
379 /*
380  * Array indexing support
381  */
382 #define MAXDIM 6
383 typedef struct
384 {
385         int                     indx[MAXDIM];
386 } IntArray;
387
388 /* ----------------
389  *              Variable-length datatypes all share the 'struct varlena' header.
390  *
391  * NOTE: for TOASTable types, this is an oversimplification, since the value
392  * may be compressed or moved out-of-line.      However datatype-specific routines
393  * are mostly content to deal with de-TOASTed values only, and of course
394  * client-side routines should never see a TOASTed value.  See postgres.h for
395  * details of the TOASTed form.
396  * ----------------
397  */
398 struct varlena
399 {
400         int32           vl_len;
401         char            vl_dat[1];
402 };
403
404 #define VARHDRSZ                ((int32) sizeof(int32))
405
406 /*
407  * These widely-used datatypes are just a varlena header and the data bytes.
408  * There is no terminating null or anything like that --- the data length is
409  * always VARSIZE(ptr) - VARHDRSZ.
410  */
411 typedef struct varlena bytea;
412 typedef struct varlena text;
413 typedef struct varlena BpChar;  /* blank-padded char, ie SQL char(n) */
414 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
415
416 /*
417  * Fixed-length array types (these are not varlena's!)
418  */
419
420 typedef int2 int2vector[INDEX_MAX_KEYS];
421 typedef Oid oidvector[INDEX_MAX_KEYS];
422
423 /*
424  * We want NameData to have length NAMEDATALEN and int alignment,
425  * because that's how the data type 'name' is defined in pg_type.
426  * Use a union to make sure the compiler agrees.  Note that NAMEDATALEN
427  * must be a multiple of sizeof(int), else sizeof(NameData) will probably
428  * not come out equal to NAMEDATALEN.
429  */
430 typedef union nameData
431 {
432         char            data[NAMEDATALEN];
433         int                     alignmentDummy;
434 } NameData;
435 typedef NameData *Name;
436
437 #define NameStr(name)   ((name).data)
438
439
440 /* ----------------------------------------------------------------
441  *                              Section 4:      IsValid macros for system types
442  * ----------------------------------------------------------------
443  */
444 /*
445  * BoolIsValid
446  *              True iff bool is valid.
447  */
448 #define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
449
450 /*
451  * PointerIsValid
452  *              True iff pointer is valid.
453  */
454 #define PointerIsValid(pointer) ((void*)(pointer) != NULL)
455
456 /*
457  * PointerIsAligned
458  *              True iff pointer is properly aligned to point to the given type.
459  */
460 #define PointerIsAligned(pointer, type) \
461                 (((long)(pointer) % (sizeof (type))) == 0)
462
463 #define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
464
465 #define RegProcedureIsValid(p)  OidIsValid(p)
466
467
468 /* ----------------------------------------------------------------
469  *                              Section 5:      offsetof, lengthof, endof, alignment
470  * ----------------------------------------------------------------
471  */
472 /*
473  * offsetof
474  *              Offset of a structure/union field within that structure/union.
475  *
476  *              XXX This is supposed to be part of stddef.h, but isn't on
477  *              some systems (like SunOS 4).
478  */
479 #ifndef offsetof
480 #define offsetof(type, field)   ((long) &((type *)0)->field)
481 #endif   /* offsetof */
482
483 /*
484  * lengthof
485  *              Number of elements in an array.
486  */
487 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
488
489 /*
490  * endof
491  *              Address of the element one past the last in an array.
492  */
493 #define endof(array)    (&array[lengthof(array)])
494
495 /* ----------------
496  * Alignment macros: align a length or address appropriately for a given type.
497  *
498  * There used to be some incredibly crufty platform-dependent hackery here,
499  * but now we rely on the configure script to get the info for us. Much nicer.
500  *
501  * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
502  * That case seems extremely unlikely to occur in practice, however.
503  * ----------------
504  */
505
506 #define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
507
508 #define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
509 #define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
510 #define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
511 #define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
512 #define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
513
514
515 /* ----------------------------------------------------------------
516  *                              Section 6:      widely useful macros
517  * ----------------------------------------------------------------
518  */
519 /*
520  * Max
521  *              Return the maximum of two numbers.
522  */
523 #define Max(x, y)               ((x) > (y) ? (x) : (y))
524
525 /*
526  * Min
527  *              Return the minimum of two numbers.
528  */
529 #define Min(x, y)               ((x) < (y) ? (x) : (y))
530
531 /*
532  * Abs
533  *              Return the absolute value of the argument.
534  */
535 #define Abs(x)                  ((x) >= 0 ? (x) : -(x))
536
537 /*
538  * StrNCpy
539  *      Like standard library function strncpy(), except that result string
540  *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
541  *      of the source string will be kept.
542  *      Also, the macro returns no result (too hard to do that without
543  *      evaluating the arguments multiple times, which seems worse).
544  *
545  *      BTW: when you need to copy a non-null-terminated string (like a text
546  *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
547  *      might seem to work, but it fetches one byte more than there is in the
548  *      text object.  One fine day you'll have a SIGSEGV because there isn't
549  *      another byte before the end of memory.  Don't laugh, we've had real
550  *      live bug reports from real live users over exactly this mistake.
551  *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
552  */
553 #define StrNCpy(dst,src,len) \
554         do \
555         { \
556                 char * _dst = (dst); \
557                 Size _len = (len); \
558 \
559                 if (_len > 0) \
560                 { \
561                         strncpy(_dst, (src), _len); \
562                         _dst[_len-1] = '\0'; \
563                 } \
564         } while (0)
565
566
567 /* Get a bit mask of the bits set in non-int32 aligned addresses */
568 #define INT_ALIGN_MASK (sizeof(int32) - 1)
569
570 /*
571  * MemSet
572  *      Exactly the same as standard library function memset(), but considerably
573  *      faster for zeroing small word-aligned structures (such as parsetree nodes).
574  *      This has to be a macro because the main point is to avoid function-call
575  *      overhead.   However, we have also found that the loop is faster than
576  *      native libc memset() on some platforms, even those with assembler
577  *      memset() functions.  More research needs to be done, perhaps with
578  *      platform-specific MEMSET_LOOP_LIMIT values or tests in configure.
579  *
580  *      bjm 2002-10-08
581  */
582 #define MemSet(start, val, len) \
583         do \
584         { \
585                 int32 * _start = (int32 *) (start); \
586                 int             _val = (val); \
587                 Size    _len = (len); \
588 \
589                 if ((( ((long) _start) | _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  1024
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 #if defined(bsdi) || defined(netbsd)
642 int fseeko(FILE *stream, off_t offset, int whence);
643 off_t ftello(FILE *stream);
644 #endif
645
646 /* These are for things that are one way on Unix and another on NT */
647 #define NULL_DEV                "/dev/null"
648
649 /*
650  * Provide prototypes for routines not present in a particular machine's
651  * standard C library.  It'd be better to put these in pg_config.h, but
652  * in pg_config.h we haven't yet included anything that defines size_t...
653  */
654
655 #if !HAVE_DECL_SNPRINTF
656 extern int
657 snprintf(char *str, size_t count, const char *fmt,...)
658 /* This extension allows gcc to check the format string */
659 __attribute__((format(printf, 3, 4)));
660 #endif
661
662 #if !HAVE_DECL_VSNPRINTF
663 extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
664 #endif
665
666 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
667 #define memmove(d, s, c)                bcopy(s, d, c)
668 #endif
669
670 #endif   /* C_H */