~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-proposed

« back to all changes in this revision

Viewing changes to src/include/c.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2009, PostgreSQL Global Development Group
 
13
 * Portions Copyright (c) 1994, Regents of the University of California
 
14
 *
 
15
 * $PostgreSQL$
 
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 "pg_config_manual.h"   /* must be after pg_config.h */
 
55
#if !defined(WIN32) && !defined(__CYGWIN__)             /* win32 will include further
 
56
                                                                                                 * down */
 
57
#include "pg_config_os.h"               /* must be before any system header files */
 
58
#endif
 
59
#include "postgres_ext.h"
 
60
 
 
61
#if _MSC_VER >= 1400
 
62
#define errcode __msvc_errcode
 
63
#include <crtdefs.h>
 
64
#undef errcode
 
65
#endif
 
66
 
 
67
#include <stdio.h>
 
68
#include <stdlib.h>
 
69
#include <string.h>
 
70
#include <stddef.h>
 
71
#include <stdarg.h>
 
72
#ifdef HAVE_STRINGS_H
 
73
#include <strings.h>
 
74
#endif
 
75
#include <sys/types.h>
 
76
 
 
77
#include <errno.h>
 
78
#if defined(WIN32) || defined(__CYGWIN__)
 
79
#include <fcntl.h>                              /* ensure O_BINARY is available */
 
80
#endif
 
81
#ifdef HAVE_SUPPORTDEFS_H
 
82
#include <SupportDefs.h>
 
83
#endif
 
84
 
 
85
#if defined(WIN32) || defined(__CYGWIN__)
 
86
/* We have to redefine some system functions after they are included above. */
 
87
#include "pg_config_os.h"
 
88
#endif
 
89
 
 
90
/* Must be before gettext() games below */
 
91
#include <locale.h>
 
92
 
 
93
#define _(x) gettext(x)
 
94
 
 
95
#ifdef ENABLE_NLS
 
96
#include <libintl.h>
 
97
#else
 
98
#define gettext(x) (x)
 
99
#define dgettext(d,x) (x)
 
100
#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
 
101
#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
 
102
#endif
 
103
 
 
104
/*
 
105
 *      Use this to mark string constants as needing translation at some later
 
106
 *      time, rather than immediately.  This is useful for cases where you need
 
107
 *      access to the original string and translated string, and for cases where
 
108
 *      immediate translation is not possible, like when initializing global
 
109
 *      variables.
 
110
 *              http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
 
111
 */
 
112
#define gettext_noop(x) (x)
 
113
 
 
114
 
 
115
/* ----------------------------------------------------------------
 
116
 *                              Section 1: hacks to cope with non-ANSI C compilers
 
117
 *
 
118
 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
 
119
 * ----------------------------------------------------------------
 
120
 */
 
121
 
 
122
/*
 
123
 * CppAsString
 
124
 *              Convert the argument to a string, using the C preprocessor.
 
125
 * CppConcat
 
126
 *              Concatenate two arguments together, using the C preprocessor.
 
127
 *
 
128
 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
 
129
 * whether #identifier works, but if we have that we likely have ## too.
 
130
 */
 
131
#if defined(HAVE_STRINGIZE)
 
132
 
 
133
#define CppAsString(identifier) #identifier
 
134
#define CppConcat(x, y)                 x##y
 
135
#else                                                   /* !HAVE_STRINGIZE */
 
136
 
 
137
#define CppAsString(identifier) "identifier"
 
138
 
 
139
/*
 
140
 * CppIdentity -- On Reiser based cpp's this is used to concatenate
 
141
 *              two tokens.  That is
 
142
 *                              CppIdentity(A)B ==> AB
 
143
 *              We renamed it to _private_CppIdentity because it should not
 
144
 *              be referenced outside this file.  On other cpp's it
 
145
 *              produces  A  B.
 
146
 */
 
147
#define _priv_CppIdentity(x)x
 
148
#define CppConcat(x, y)                 _priv_CppIdentity(x)y
 
149
#endif   /* !HAVE_STRINGIZE */
 
150
 
 
151
/*
 
152
 * dummyret is used to set return values in macros that use ?: to make
 
153
 * assignments.  gcc wants these to be void, other compilers like char
 
154
 */
 
155
#ifdef __GNUC__                                 /* GNU cc */
 
156
#define dummyret        void
 
157
#else
 
158
#define dummyret        char
 
159
#endif
 
160
 
 
161
#ifndef __GNUC__
 
162
#define __attribute__(_arg_)
 
163
#endif
 
164
 
 
165
/* ----------------------------------------------------------------
 
166
 *                              Section 2:      bool, true, false, TRUE, FALSE, NULL
 
167
 * ----------------------------------------------------------------
 
168
 */
 
169
 
 
170
/*
 
171
 * bool
 
172
 *              Boolean value, either true or false.
 
173
 *
 
174
 * XXX for C++ compilers, we assume the compiler has a compatible
 
175
 * built-in definition of bool.
 
176
 */
 
177
 
 
178
#ifndef __cplusplus
 
179
 
 
180
#ifndef bool
 
181
typedef char bool;
 
182
#endif
 
183
 
 
184
#ifndef true
 
185
#define true    ((bool) 1)
 
186
#endif
 
187
 
 
188
#ifndef false
 
189
#define false   ((bool) 0)
 
190
#endif
 
191
#endif   /* not C++ */
 
192
 
 
193
typedef bool *BoolPtr;
 
194
 
 
195
#ifndef TRUE
 
196
#define TRUE    1
 
197
#endif
 
198
 
 
199
#ifndef FALSE
 
200
#define FALSE   0
 
201
#endif
 
202
 
 
203
/*
 
204
 * NULL
 
205
 *              Null pointer.
 
206
 */
 
207
#ifndef NULL
 
208
#define NULL    ((void *) 0)
 
209
#endif
 
210
 
 
211
 
 
212
/* ----------------------------------------------------------------
 
213
 *                              Section 3:      standard system types
 
214
 * ----------------------------------------------------------------
 
215
 */
 
216
 
 
217
/*
 
218
 * Pointer
 
219
 *              Variable holding address of any memory resident object.
 
220
 *
 
221
 *              XXX Pointer arithmetic is done with this, so it can't be void *
 
222
 *              under "true" ANSI compilers.
 
223
 */
 
224
typedef char *Pointer;
 
225
 
 
226
/*
 
227
 * intN
 
228
 *              Signed integer, EXACTLY N BITS IN SIZE,
 
229
 *              used for numerical computations and the
 
230
 *              frontend/backend protocol.
 
231
 */
 
232
#ifndef HAVE_INT8
 
233
typedef signed char int8;               /* == 8 bits */
 
234
typedef signed short int16;             /* == 16 bits */
 
235
typedef signed int int32;               /* == 32 bits */
 
236
#endif   /* not HAVE_INT8 */
 
237
 
 
238
/*
 
239
 * uintN
 
240
 *              Unsigned integer, EXACTLY N BITS IN SIZE,
 
241
 *              used for numerical computations and the
 
242
 *              frontend/backend protocol.
 
243
 */
 
244
#ifndef HAVE_UINT8
 
245
typedef unsigned char uint8;    /* == 8 bits */
 
246
typedef unsigned short uint16;  /* == 16 bits */
 
247
typedef unsigned int uint32;    /* == 32 bits */
 
248
#endif   /* not HAVE_UINT8 */
 
249
 
 
250
/*
 
251
 * bitsN
 
252
 *              Unit of bitwise operation, AT LEAST N BITS IN SIZE.
 
253
 */
 
254
typedef uint8 bits8;                    /* >= 8 bits */
 
255
typedef uint16 bits16;                  /* >= 16 bits */
 
256
typedef uint32 bits32;                  /* >= 32 bits */
 
257
 
 
258
/*
 
259
 * 64-bit integers
 
260
 */
 
261
#ifdef HAVE_LONG_INT_64
 
262
/* Plain "long int" fits, use it */
 
263
 
 
264
#ifndef HAVE_INT64
 
265
typedef long int int64;
 
266
#endif
 
267
#ifndef HAVE_UINT64
 
268
typedef unsigned long int uint64;
 
269
#endif
 
270
#elif defined(HAVE_LONG_LONG_INT_64)
 
271
/* We have working support for "long long int", use that */
 
272
 
 
273
#ifndef HAVE_INT64
 
274
typedef long long int int64;
 
275
#endif
 
276
#ifndef HAVE_UINT64
 
277
typedef unsigned long long int uint64;
 
278
#endif
 
279
#else                                                   /* not HAVE_LONG_INT_64 and not
 
280
                                                                 * HAVE_LONG_LONG_INT_64 */
 
281
 
 
282
/* Won't actually work, but fall back to long int so that code compiles */
 
283
#ifndef HAVE_INT64
 
284
typedef long int int64;
 
285
#endif
 
286
#ifndef HAVE_UINT64
 
287
typedef unsigned long int uint64;
 
288
#endif
 
289
 
 
290
#define INT64_IS_BUSTED
 
291
#endif   /* not HAVE_LONG_INT_64 and not
 
292
                                                                 * HAVE_LONG_LONG_INT_64 */
 
293
 
 
294
/* Decide if we need to decorate 64-bit constants */
 
295
#ifdef HAVE_LL_CONSTANTS
 
296
#define INT64CONST(x)  ((int64) x##LL)
 
297
#define UINT64CONST(x) ((uint64) x##ULL)
 
298
#else
 
299
#define INT64CONST(x)  ((int64) x)
 
300
#define UINT64CONST(x) ((uint64) x)
 
301
#endif
 
302
 
 
303
 
 
304
/* Select timestamp representation (float8 or int64) */
 
305
#if defined(USE_INTEGER_DATETIMES) && !defined(INT64_IS_BUSTED)
 
306
#define HAVE_INT64_TIMESTAMP
 
307
#endif
 
308
 
 
309
/* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
 
310
#ifndef HAVE_SIG_ATOMIC_T
 
311
typedef int sig_atomic_t;
 
312
#endif
 
313
 
 
314
/*
 
315
 * Size
 
316
 *              Size of any memory resident object, as returned by sizeof.
 
317
 */
 
318
typedef size_t Size;
 
319
 
 
320
/*
 
321
 * Index
 
322
 *              Index into any memory resident array.
 
323
 *
 
324
 * Note:
 
325
 *              Indices are non negative.
 
326
 */
 
327
typedef unsigned int Index;
 
328
 
 
329
/*
 
330
 * Offset
 
331
 *              Offset into any memory resident array.
 
332
 *
 
333
 * Note:
 
334
 *              This differs from an Index in that an Index is always
 
335
 *              non negative, whereas Offset may be negative.
 
336
 */
 
337
typedef signed int Offset;
 
338
 
 
339
/*
 
340
 * Common Postgres datatype names (as used in the catalogs)
 
341
 */
 
342
typedef int16 int2;
 
343
typedef int32 int4;
 
344
typedef float float4;
 
345
typedef double float8;
 
346
 
 
347
/*
 
348
 * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
 
349
 * CommandId
 
350
 */
 
351
 
 
352
/* typedef Oid is in postgres_ext.h */
 
353
 
 
354
/*
 
355
 * regproc is the type name used in the include/catalog headers, but
 
356
 * RegProcedure is the preferred name in C code.
 
357
 */
 
358
typedef Oid regproc;
 
359
typedef regproc RegProcedure;
 
360
 
 
361
typedef uint32 TransactionId;
 
362
 
 
363
typedef uint32 LocalTransactionId;
 
364
 
 
365
typedef uint32 SubTransactionId;
 
366
 
 
367
#define InvalidSubTransactionId         ((SubTransactionId) 0)
 
368
#define TopSubTransactionId                     ((SubTransactionId) 1)
 
369
 
 
370
/* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
 
371
typedef TransactionId MultiXactId;
 
372
 
 
373
typedef uint32 MultiXactOffset;
 
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.  But even in a
 
395
 * de-TOASTed value, beware of touching vl_len_ directly, as its representation
 
396
 * is no longer convenient.  It's recommended that code always use the VARDATA,
 
397
 * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of
 
398
 * the struct fields.  See postgres.h for details of the TOASTed form.
 
399
 * ----------------
 
400
 */
 
401
struct varlena
 
402
{
 
403
        char            vl_len_[4];             /* Do not touch this field directly! */
 
404
        char            vl_dat[1];
 
405
};
 
406
 
 
407
#define VARHDRSZ                ((int32) sizeof(int32))
 
408
 
 
409
/*
 
410
 * These widely-used datatypes are just a varlena header and the data bytes.
 
411
 * There is no terminating null or anything like that --- the data length is
 
412
 * always VARSIZE(ptr) - VARHDRSZ.
 
413
 */
 
414
typedef struct varlena bytea;
 
415
typedef struct varlena text;
 
416
typedef struct varlena BpChar;  /* blank-padded char, ie SQL char(n) */
 
417
typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
 
418
 
 
419
/*
 
420
 * Specialized array types.  These are physically laid out just the same
 
421
 * as regular arrays (so that the regular array subscripting code works
 
422
 * with them).  They exist as distinct types mostly for historical reasons:
 
423
 * they have nonstandard I/O behavior which we don't want to change for fear
 
424
 * of breaking applications that look at the system catalogs.  There is also
 
425
 * an implementation issue for oidvector: it's part of the primary key for
 
426
 * pg_proc, and we can't use the normal btree array support routines for that
 
427
 * without circularity.
 
428
 */
 
429
typedef struct
 
430
{
 
431
        int32           vl_len_;                /* these fields must match ArrayType! */
 
432
        int                     ndim;                   /* always 1 for int2vector */
 
433
        int32           dataoffset;             /* always 0 for int2vector */
 
434
        Oid                     elemtype;
 
435
        int                     dim1;
 
436
        int                     lbound1;
 
437
        int2            values[1];              /* VARIABLE LENGTH ARRAY */
 
438
} int2vector;                                   /* VARIABLE LENGTH STRUCT */
 
439
 
 
440
typedef struct
 
441
{
 
442
        int32           vl_len_;                /* these fields must match ArrayType! */
 
443
        int                     ndim;                   /* always 1 for oidvector */
 
444
        int32           dataoffset;             /* always 0 for oidvector */
 
445
        Oid                     elemtype;
 
446
        int                     dim1;
 
447
        int                     lbound1;
 
448
        Oid                     values[1];              /* VARIABLE LENGTH ARRAY */
 
449
} oidvector;                                    /* VARIABLE LENGTH STRUCT */
 
450
 
 
451
/*
 
452
 * Representation of a Name: effectively just a C string, but null-padded to
 
453
 * exactly NAMEDATALEN bytes.  The use of a struct is historical.
 
454
 */
 
455
typedef struct nameData
 
456
{
 
457
        char            data[NAMEDATALEN];
 
458
} NameData;
 
459
typedef NameData *Name;
 
460
 
 
461
#define NameStr(name)   ((name).data)
 
462
 
 
463
/*
 
464
 * Support macros for escaping strings.  escape_backslash should be TRUE
 
465
 * if generating a non-standard-conforming string.      Prefixing a string
 
466
 * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
 
467
 * Beware of multiple evaluation of the "ch" argument!
 
468
 */
 
469
#define SQL_STR_DOUBLE(ch, escape_backslash)    \
 
470
        ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
 
471
 
 
472
#define ESCAPE_STRING_SYNTAX    'E'
 
473
 
 
474
/* ----------------------------------------------------------------
 
475
 *                              Section 4:      IsValid macros for system types
 
476
 * ----------------------------------------------------------------
 
477
 */
 
478
/*
 
479
 * BoolIsValid
 
480
 *              True iff bool is valid.
 
481
 */
 
482
#define BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
 
483
 
 
484
/*
 
485
 * PointerIsValid
 
486
 *              True iff pointer is valid.
 
487
 */
 
488
#define PointerIsValid(pointer) ((void*)(pointer) != NULL)
 
489
 
 
490
/*
 
491
 * PointerIsAligned
 
492
 *              True iff pointer is properly aligned to point to the given type.
 
493
 */
 
494
#define PointerIsAligned(pointer, type) \
 
495
                (((long)(pointer) % (sizeof (type))) == 0)
 
496
 
 
497
#define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))
 
498
 
 
499
#define RegProcedureIsValid(p)  OidIsValid(p)
 
500
 
 
501
 
 
502
/* ----------------------------------------------------------------
 
503
 *                              Section 5:      offsetof, lengthof, endof, alignment
 
504
 * ----------------------------------------------------------------
 
505
 */
 
506
/*
 
507
 * offsetof
 
508
 *              Offset of a structure/union field within that structure/union.
 
509
 *
 
510
 *              XXX This is supposed to be part of stddef.h, but isn't on
 
511
 *              some systems (like SunOS 4).
 
512
 */
 
513
#ifndef offsetof
 
514
#define offsetof(type, field)   ((long) &((type *)0)->field)
 
515
#endif   /* offsetof */
 
516
 
 
517
/*
 
518
 * lengthof
 
519
 *              Number of elements in an array.
 
520
 */
 
521
#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
 
522
 
 
523
/*
 
524
 * endof
 
525
 *              Address of the element one past the last in an array.
 
526
 */
 
527
#define endof(array)    (&(array)[lengthof(array)])
 
528
 
 
529
/* ----------------
 
530
 * Alignment macros: align a length or address appropriately for a given type.
 
531
 * The fooALIGN() macros round up to a multiple of the required alignment,
 
532
 * while the fooALIGN_DOWN() macros round down.  The latter are more useful
 
533
 * for problems like "how many X-sized structures will fit in a page?".
 
534
 *
 
535
 * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
 
536
 * That case seems extremely unlikely to be needed in practice, however.
 
537
 * ----------------
 
538
 */
 
539
 
 
540
#define TYPEALIGN(ALIGNVAL,LEN)  \
 
541
        (((long) (LEN) + ((ALIGNVAL) - 1)) & ~((long) ((ALIGNVAL) - 1)))
 
542
 
 
543
#define SHORTALIGN(LEN)                 TYPEALIGN(ALIGNOF_SHORT, (LEN))
 
544
#define INTALIGN(LEN)                   TYPEALIGN(ALIGNOF_INT, (LEN))
 
545
#define LONGALIGN(LEN)                  TYPEALIGN(ALIGNOF_LONG, (LEN))
 
546
#define DOUBLEALIGN(LEN)                TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
 
547
#define MAXALIGN(LEN)                   TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
 
548
/* MAXALIGN covers only built-in types, not buffers */
 
549
#define BUFFERALIGN(LEN)                TYPEALIGN(ALIGNOF_BUFFER, (LEN))
 
550
 
 
551
#define TYPEALIGN_DOWN(ALIGNVAL,LEN)  \
 
552
        (((long) (LEN)) & ~((long) ((ALIGNVAL) - 1)))
 
553
 
 
554
#define SHORTALIGN_DOWN(LEN)    TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
 
555
#define INTALIGN_DOWN(LEN)              TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
 
556
#define LONGALIGN_DOWN(LEN)             TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
 
557
#define DOUBLEALIGN_DOWN(LEN)   TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
 
558
#define MAXALIGN_DOWN(LEN)              TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
 
559
 
 
560
/* ----------------------------------------------------------------
 
561
 *                              Section 6:      widely useful macros
 
562
 * ----------------------------------------------------------------
 
563
 */
 
564
/*
 
565
 * Max
 
566
 *              Return the maximum of two numbers.
 
567
 */
 
568
#define Max(x, y)               ((x) > (y) ? (x) : (y))
 
569
 
 
570
/*
 
571
 * Min
 
572
 *              Return the minimum of two numbers.
 
573
 */
 
574
#define Min(x, y)               ((x) < (y) ? (x) : (y))
 
575
 
 
576
/*
 
577
 * Abs
 
578
 *              Return the absolute value of the argument.
 
579
 */
 
580
#define Abs(x)                  ((x) >= 0 ? (x) : -(x))
 
581
 
 
582
/*
 
583
 * StrNCpy
 
584
 *      Like standard library function strncpy(), except that result string
 
585
 *      is guaranteed to be null-terminated --- that is, at most N-1 bytes
 
586
 *      of the source string will be kept.
 
587
 *      Also, the macro returns no result (too hard to do that without
 
588
 *      evaluating the arguments multiple times, which seems worse).
 
589
 *
 
590
 *      BTW: when you need to copy a non-null-terminated string (like a text
 
591
 *      datum) and add a null, do not do it with StrNCpy(..., len+1).  That
 
592
 *      might seem to work, but it fetches one byte more than there is in the
 
593
 *      text object.  One fine day you'll have a SIGSEGV because there isn't
 
594
 *      another byte before the end of memory.  Don't laugh, we've had real
 
595
 *      live bug reports from real live users over exactly this mistake.
 
596
 *      Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
 
597
 */
 
598
#define StrNCpy(dst,src,len) \
 
599
        do \
 
600
        { \
 
601
                char * _dst = (dst); \
 
602
                Size _len = (len); \
 
603
\
 
604
                if (_len > 0) \
 
605
                { \
 
606
                        strncpy(_dst, (src), _len); \
 
607
                        _dst[_len-1] = '\0'; \
 
608
                } \
 
609
        } while (0)
 
610
 
 
611
 
 
612
/* Get a bit mask of the bits set in non-long aligned addresses */
 
613
#define LONG_ALIGN_MASK (sizeof(long) - 1)
 
614
 
 
615
/*
 
616
 * MemSet
 
617
 *      Exactly the same as standard library function memset(), but considerably
 
618
 *      faster for zeroing small word-aligned structures (such as parsetree nodes).
 
619
 *      This has to be a macro because the main point is to avoid function-call
 
620
 *      overhead.       However, we have also found that the loop is faster than
 
621
 *      native libc memset() on some platforms, even those with assembler
 
622
 *      memset() functions.  More research needs to be done, perhaps with
 
623
 *      MEMSET_LOOP_LIMIT tests in configure.
 
624
 */
 
625
#define MemSet(start, val, len) \
 
626
        do \
 
627
        { \
 
628
                /* must be void* because we don't know if it is integer aligned yet */ \
 
629
                void   *_vstart = (void *) (start); \
 
630
                int             _val = (val); \
 
631
                Size    _len = (len); \
 
632
\
 
633
                if ((((long) _vstart) & LONG_ALIGN_MASK) == 0 && \
 
634
                        (_len & LONG_ALIGN_MASK) == 0 && \
 
635
                        _val == 0 && \
 
636
                        _len <= MEMSET_LOOP_LIMIT && \
 
637
                        /* \
 
638
                         *      If MEMSET_LOOP_LIMIT == 0, optimizer should find \
 
639
                         *      the whole "if" false at compile time. \
 
640
                         */ \
 
641
                        MEMSET_LOOP_LIMIT != 0) \
 
642
                { \
 
643
                        long *_start = (long *) _vstart; \
 
644
                        long *_stop = (long *) ((char *) _start + _len); \
 
645
                        while (_start < _stop) \
 
646
                                *_start++ = 0; \
 
647
                } \
 
648
                else \
 
649
                        memset(_vstart, _val, _len); \
 
650
        } while (0)
 
651
 
 
652
/*
 
653
 * MemSetAligned is the same as MemSet except it omits the test to see if
 
654
 * "start" is word-aligned.  This is okay to use if the caller knows a-priori
 
655
 * that the pointer is suitably aligned (typically, because he just got it
 
656
 * from palloc(), which always delivers a max-aligned pointer).
 
657
 */
 
658
#define MemSetAligned(start, val, len) \
 
659
        do \
 
660
        { \
 
661
                long   *_start = (long *) (start); \
 
662
                int             _val = (val); \
 
663
                Size    _len = (len); \
 
664
\
 
665
                if ((_len & LONG_ALIGN_MASK) == 0 && \
 
666
                        _val == 0 && \
 
667
                        _len <= MEMSET_LOOP_LIMIT && \
 
668
                        MEMSET_LOOP_LIMIT != 0) \
 
669
                { \
 
670
                        long *_stop = (long *) ((char *) _start + _len); \
 
671
                        while (_start < _stop) \
 
672
                                *_start++ = 0; \
 
673
                } \
 
674
                else \
 
675
                        memset(_start, _val, _len); \
 
676
        } while (0)
 
677
 
 
678
 
 
679
/*
 
680
 * MemSetTest/MemSetLoop are a variant version that allow all the tests in
 
681
 * MemSet to be done at compile time in cases where "val" and "len" are
 
682
 * constants *and* we know the "start" pointer must be word-aligned.
 
683
 * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
 
684
 * MemSetAligned.  Beware of multiple evaluations of the arguments when using
 
685
 * this approach.
 
686
 */
 
687
#define MemSetTest(val, len) \
 
688
        ( ((len) & LONG_ALIGN_MASK) == 0 && \
 
689
        (len) <= MEMSET_LOOP_LIMIT && \
 
690
        MEMSET_LOOP_LIMIT != 0 && \
 
691
        (val) == 0 )
 
692
 
 
693
#define MemSetLoop(start, val, len) \
 
694
        do \
 
695
        { \
 
696
                long * _start = (long *) (start); \
 
697
                long * _stop = (long *) ((char *) _start + (Size) (len)); \
 
698
        \
 
699
                while (_start < _stop) \
 
700
                        *_start++ = 0; \
 
701
        } while (0)
 
702
 
 
703
 
 
704
/* ----------------------------------------------------------------
 
705
 *                              Section 7:      random stuff
 
706
 * ----------------------------------------------------------------
 
707
 */
 
708
 
 
709
/* msb for char */
 
710
#define HIGHBIT                                 (0x80)
 
711
#define IS_HIGHBIT_SET(ch)              ((unsigned char)(ch) & HIGHBIT)
 
712
 
 
713
#define STATUS_OK                               (0)
 
714
#define STATUS_ERROR                    (-1)
 
715
#define STATUS_EOF                              (-2)
 
716
#define STATUS_FOUND                    (1)
 
717
#define STATUS_WAITING                  (2)
 
718
 
 
719
 
 
720
/* gettext domain name mangling */
 
721
 
 
722
/*
 
723
 * To better support parallel installations of major PostgeSQL
 
724
 * versions as well as parallel installations of major library soname
 
725
 * versions, we mangle the gettext domain name by appending those
 
726
 * version numbers.  The coding rule ought to be that whereever the
 
727
 * domain name is mentioned as a literal, it must be wrapped into
 
728
 * PG_TEXTDOMAIN().  The macros below do not work on non-literals; but
 
729
 * that is somewhat intentional because it avoids having to worry
 
730
 * about multiple states of premangling and postmangling as the values
 
731
 * are being passed around.
 
732
 *
 
733
 * Make sure this matches the installation rules in nls-global.mk.
 
734
 */
 
735
 
 
736
/* need a second indirection because we want to stringize the macro value, not the name */
 
737
#define CppAsString2(x) CppAsString(x)
 
738
 
 
739
#ifdef SO_MAJOR_VERSION
 
740
# define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
 
741
#else
 
742
# define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
 
743
#endif
 
744
 
 
745
 
 
746
/* ----------------------------------------------------------------
 
747
 *                              Section 8: system-specific hacks
 
748
 *
 
749
 *              This should be limited to things that absolutely have to be
 
750
 *              included in every source file.  The port-specific header file
 
751
 *              is usually a better place for this sort of thing.
 
752
 * ----------------------------------------------------------------
 
753
 */
 
754
 
 
755
/*
 
756
 *      NOTE:  this is also used for opening text files.
 
757
 *      WIN32 treats Control-Z as EOF in files opened in text mode.
 
758
 *      Therefore, we open files in binary mode on Win32 so we can read
 
759
 *      literal control-Z.      The other affect is that we see CRLF, but
 
760
 *      that is OK because we can already handle those cleanly.
 
761
 */
 
762
#if defined(WIN32) || defined(__CYGWIN__)
 
763
#define PG_BINARY       O_BINARY
 
764
#define PG_BINARY_A "ab"
 
765
#define PG_BINARY_R "rb"
 
766
#define PG_BINARY_W "wb"
 
767
#else
 
768
#define PG_BINARY       0
 
769
#define PG_BINARY_A "a"
 
770
#define PG_BINARY_R "r"
 
771
#define PG_BINARY_W "w"
 
772
#endif
 
773
 
 
774
/*
 
775
 * Provide prototypes for routines not present in a particular machine's
 
776
 * standard C library.
 
777
 */
 
778
 
 
779
#if !HAVE_DECL_SNPRINTF
 
780
extern int
 
781
snprintf(char *str, size_t count, const char *fmt,...)
 
782
/* This extension allows gcc to check the format string */
 
783
__attribute__((format(printf, 3, 4)));
 
784
#endif
 
785
 
 
786
#if !HAVE_DECL_VSNPRINTF
 
787
extern int      vsnprintf(char *str, size_t count, const char *fmt, va_list args);
 
788
#endif
 
789
 
 
790
#if !defined(HAVE_MEMMOVE) && !defined(memmove)
 
791
#define memmove(d, s, c)                bcopy(s, d, c)
 
792
#endif
 
793
 
 
794
#ifndef PGDLLIMPORT
 
795
#define PGDLLIMPORT                             /* no special DLL markers on most ports */
 
796
#endif
 
797
 
 
798
/*
 
799
 * The following is used as the arg list for signal handlers.  Any ports
 
800
 * that take something other than an int argument should override this in
 
801
 * their pg_config_os.h file.  Note that variable names are required
 
802
 * because it is used in both the prototypes as well as the definitions.
 
803
 * Note also the long name.  We expect that this won't collide with
 
804
 * other names causing compiler warnings.
 
805
 */
 
806
 
 
807
#ifndef SIGNAL_ARGS
 
808
#define SIGNAL_ARGS  int postgres_signal_arg
 
809
#endif
 
810
 
 
811
/*
 
812
 * When there is no sigsetjmp, its functionality is provided by plain
 
813
 * setjmp. Incidentally, nothing provides setjmp's functionality in
 
814
 * that case.
 
815
 */
 
816
#ifndef HAVE_SIGSETJMP
 
817
#define sigjmp_buf jmp_buf
 
818
#define sigsetjmp(x,y) setjmp(x)
 
819
#define siglongjmp longjmp
 
820
#endif
 
821
 
 
822
#if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
 
823
extern int      fdatasync(int fildes);
 
824
#endif
 
825
 
 
826
/* If strtoq() exists, rename it to the more standard strtoll() */
 
827
#if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
 
828
#define strtoll strtoq
 
829
#define HAVE_STRTOLL 1
 
830
#endif
 
831
 
 
832
/* If strtouq() exists, rename it to the more standard strtoull() */
 
833
#if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
 
834
#define strtoull strtouq
 
835
#define HAVE_STRTOULL 1
 
836
#endif
 
837
 
 
838
/*
 
839
 * We assume if we have these two functions, we have their friends too, and
 
840
 * can use the wide-character functions.
 
841
 */
 
842
#if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
 
843
#define USE_WIDE_UPPER_LOWER
 
844
#endif
 
845
   
 
846
/* EXEC_BACKEND defines */
 
847
#ifdef EXEC_BACKEND
 
848
#define NON_EXEC_STATIC
 
849
#else
 
850
#define NON_EXEC_STATIC static
 
851
#endif
 
852
 
 
853
/* /port compatibility functions */
 
854
#include "port.h"
 
855
 
 
856
#endif   /* C_H */