~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to src/include/postgres.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * postgres.h
 
4
 *        Primary include file for PostgreSQL server .c files
 
5
 *
 
6
 * This should be the first file included by PostgreSQL backend modules.
 
7
 * Client-side code should include postgres_fe.h instead.
 
8
 *
 
9
 *
 
10
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 
11
 * Portions Copyright (c) 1995, Regents of the University of California
 
12
 *
 
13
 * src/include/postgres.h
 
14
 *
 
15
 *-------------------------------------------------------------------------
 
16
 */
 
17
/*
 
18
 *----------------------------------------------------------------
 
19
 *       TABLE OF CONTENTS
 
20
 *
 
21
 *              When adding stuff to this file, please try to put stuff
 
22
 *              into the relevant section, or add new sections as appropriate.
 
23
 *
 
24
 *        section       description
 
25
 *        -------       ------------------------------------------------
 
26
 *              1)              variable-length datatypes (TOAST support)
 
27
 *              2)              datum type + support macros
 
28
 *              3)              exception handling definitions
 
29
 *
 
30
 *       NOTES
 
31
 *
 
32
 *      In general, this file should contain declarations that are widely needed
 
33
 *      in the backend environment, but are of no interest outside the backend.
 
34
 *
 
35
 *      Simple type definitions live in c.h, where they are shared with
 
36
 *      postgres_fe.h.  We do that since those type definitions are needed by
 
37
 *      frontend modules that want to deal with binary data transmission to or
 
38
 *      from the backend.  Type definitions in this file should be for
 
39
 *      representations that never escape the backend, such as Datum or
 
40
 *      TOASTed varlena objects.
 
41
 *
 
42
 *----------------------------------------------------------------
 
43
 */
 
44
#ifndef POSTGRES_H
 
45
#define POSTGRES_H
 
46
 
 
47
#include "c.h"
 
48
#include "utils/elog.h"
 
49
#include "utils/palloc.h"
 
50
 
 
51
/* ----------------------------------------------------------------
 
52
 *                              Section 1:      variable-length datatypes (TOAST support)
 
53
 * ----------------------------------------------------------------
 
54
 */
 
55
 
 
56
/*
 
57
 * struct varatt_external is a "TOAST pointer", that is, the information
 
58
 * needed to fetch a stored-out-of-line Datum.  The data is compressed
 
59
 * if and only if va_extsize < va_rawsize - VARHDRSZ.  This struct must not
 
60
 * contain any padding, because we sometimes compare pointers using memcmp.
 
61
 *
 
62
 * Note that this information is stored unaligned within actual tuples, so
 
63
 * you need to memcpy from the tuple into a local struct variable before
 
64
 * you can look at these fields!  (The reason we use memcmp is to avoid
 
65
 * having to do that just to detect equality of two TOAST pointers...)
 
66
 */
 
67
struct varatt_external
 
68
{
 
69
        int32           va_rawsize;             /* Original data size (includes header) */
 
70
        int32           va_extsize;             /* External saved size (doesn't) */
 
71
        Oid                     va_valueid;             /* Unique ID of value within TOAST table */
 
72
        Oid                     va_toastrelid;  /* RelID of TOAST table containing it */
 
73
};
 
74
 
 
75
/*
 
76
 * These structs describe the header of a varlena object that may have been
 
77
 * TOASTed.  Generally, don't reference these structs directly, but use the
 
78
 * macros below.
 
79
 *
 
80
 * We use separate structs for the aligned and unaligned cases because the
 
81
 * compiler might otherwise think it could generate code that assumes
 
82
 * alignment while touching fields of a 1-byte-header varlena.
 
83
 */
 
84
typedef union
 
85
{
 
86
        struct                                          /* Normal varlena (4-byte length) */
 
87
        {
 
88
                uint32          va_header;
 
89
                char            va_data[1];
 
90
        }                       va_4byte;
 
91
        struct                                          /* Compressed-in-line format */
 
92
        {
 
93
                uint32          va_header;
 
94
                uint32          va_rawsize; /* Original data size (excludes header) */
 
95
                char            va_data[1]; /* Compressed data */
 
96
        }                       va_compressed;
 
97
} varattrib_4b;
 
98
 
 
99
typedef struct
 
100
{
 
101
        uint8           va_header;
 
102
        char            va_data[1];             /* Data begins here */
 
103
} varattrib_1b;
 
104
 
 
105
typedef struct
 
106
{
 
107
        uint8           va_header;              /* Always 0x80 or 0x01 */
 
108
        uint8           va_len_1be;             /* Physical length of datum */
 
109
        char            va_data[1];             /* Data (for now always a TOAST pointer) */
 
110
} varattrib_1b_e;
 
111
 
 
112
/*
 
113
 * Bit layouts for varlena headers on big-endian machines:
 
114
 *
 
115
 * 00xxxxxx 4-byte length word, aligned, uncompressed data (up to 1G)
 
116
 * 01xxxxxx 4-byte length word, aligned, *compressed* data (up to 1G)
 
117
 * 10000000 1-byte length word, unaligned, TOAST pointer
 
118
 * 1xxxxxxx 1-byte length word, unaligned, uncompressed data (up to 126b)
 
119
 *
 
120
 * Bit layouts for varlena headers on little-endian machines:
 
121
 *
 
122
 * xxxxxx00 4-byte length word, aligned, uncompressed data (up to 1G)
 
123
 * xxxxxx10 4-byte length word, aligned, *compressed* data (up to 1G)
 
124
 * 00000001 1-byte length word, unaligned, TOAST pointer
 
125
 * xxxxxxx1 1-byte length word, unaligned, uncompressed data (up to 126b)
 
126
 *
 
127
 * The "xxx" bits are the length field (which includes itself in all cases).
 
128
 * In the big-endian case we mask to extract the length, in the little-endian
 
129
 * case we shift.  Note that in both cases the flag bits are in the physically
 
130
 * first byte.  Also, it is not possible for a 1-byte length word to be zero;
 
131
 * this lets us disambiguate alignment padding bytes from the start of an
 
132
 * unaligned datum.  (We now *require* pad bytes to be filled with zero!)
 
133
 */
 
134
 
 
135
/*
 
136
 * Endian-dependent macros.  These are considered internal --- use the
 
137
 * external macros below instead of using these directly.
 
138
 *
 
139
 * Note: IS_1B is true for external toast records but VARSIZE_1B will return 0
 
140
 * for such records. Hence you should usually check for IS_EXTERNAL before
 
141
 * checking for IS_1B.
 
142
 */
 
143
 
 
144
#ifdef WORDS_BIGENDIAN
 
145
 
 
146
#define VARATT_IS_4B(PTR) \
 
147
        ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
 
148
#define VARATT_IS_4B_U(PTR) \
 
149
        ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
 
150
#define VARATT_IS_4B_C(PTR) \
 
151
        ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
 
152
#define VARATT_IS_1B(PTR) \
 
153
        ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
 
154
#define VARATT_IS_1B_E(PTR) \
 
155
        ((((varattrib_1b *) (PTR))->va_header) == 0x80)
 
156
#define VARATT_NOT_PAD_BYTE(PTR) \
 
157
        (*((uint8 *) (PTR)) != 0)
 
158
 
 
159
/* VARSIZE_4B() should only be used on known-aligned data */
 
160
#define VARSIZE_4B(PTR) \
 
161
        (((varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
 
162
#define VARSIZE_1B(PTR) \
 
163
        (((varattrib_1b *) (PTR))->va_header & 0x7F)
 
164
#define VARSIZE_1B_E(PTR) \
 
165
        (((varattrib_1b_e *) (PTR))->va_len_1be)
 
166
 
 
167
#define SET_VARSIZE_4B(PTR,len) \
 
168
        (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
 
169
#define SET_VARSIZE_4B_C(PTR,len) \
 
170
        (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 0x40000000)
 
171
#define SET_VARSIZE_1B(PTR,len) \
 
172
        (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
 
173
#define SET_VARSIZE_1B_E(PTR,len) \
 
174
        (((varattrib_1b_e *) (PTR))->va_header = 0x80, \
 
175
         ((varattrib_1b_e *) (PTR))->va_len_1be = (len))
 
176
#else                                                   /* !WORDS_BIGENDIAN */
 
177
 
 
178
#define VARATT_IS_4B(PTR) \
 
179
        ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
 
180
#define VARATT_IS_4B_U(PTR) \
 
181
        ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
 
182
#define VARATT_IS_4B_C(PTR) \
 
183
        ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
 
184
#define VARATT_IS_1B(PTR) \
 
185
        ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
 
186
#define VARATT_IS_1B_E(PTR) \
 
187
        ((((varattrib_1b *) (PTR))->va_header) == 0x01)
 
188
#define VARATT_NOT_PAD_BYTE(PTR) \
 
189
        (*((uint8 *) (PTR)) != 0)
 
190
 
 
191
/* VARSIZE_4B() should only be used on known-aligned data */
 
192
#define VARSIZE_4B(PTR) \
 
193
        ((((varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
 
194
#define VARSIZE_1B(PTR) \
 
195
        ((((varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
 
196
#define VARSIZE_1B_E(PTR) \
 
197
        (((varattrib_1b_e *) (PTR))->va_len_1be)
 
198
 
 
199
#define SET_VARSIZE_4B(PTR,len) \
 
200
        (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
 
201
#define SET_VARSIZE_4B_C(PTR,len) \
 
202
        (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) | 0x02)
 
203
#define SET_VARSIZE_1B(PTR,len) \
 
204
        (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
 
205
#define SET_VARSIZE_1B_E(PTR,len) \
 
206
        (((varattrib_1b_e *) (PTR))->va_header = 0x01, \
 
207
         ((varattrib_1b_e *) (PTR))->va_len_1be = (len))
 
208
#endif   /* WORDS_BIGENDIAN */
 
209
 
 
210
#define VARHDRSZ_SHORT                  1
 
211
#define VARATT_SHORT_MAX                0x7F
 
212
#define VARATT_CAN_MAKE_SHORT(PTR) \
 
213
        (VARATT_IS_4B_U(PTR) && \
 
214
         (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT) <= VARATT_SHORT_MAX)
 
215
#define VARATT_CONVERTED_SHORT_SIZE(PTR) \
 
216
        (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT)
 
217
 
 
218
#define VARHDRSZ_EXTERNAL               2
 
219
 
 
220
#define VARDATA_4B(PTR)         (((varattrib_4b *) (PTR))->va_4byte.va_data)
 
221
#define VARDATA_4B_C(PTR)       (((varattrib_4b *) (PTR))->va_compressed.va_data)
 
222
#define VARDATA_1B(PTR)         (((varattrib_1b *) (PTR))->va_data)
 
223
#define VARDATA_1B_E(PTR)       (((varattrib_1b_e *) (PTR))->va_data)
 
224
 
 
225
#define VARRAWSIZE_4B_C(PTR) \
 
226
        (((varattrib_4b *) (PTR))->va_compressed.va_rawsize)
 
227
 
 
228
/* Externally visible macros */
 
229
 
 
230
/*
 
231
 * VARDATA, VARSIZE, and SET_VARSIZE are the recommended API for most code
 
232
 * for varlena datatypes.  Note that they only work on untoasted,
 
233
 * 4-byte-header Datums!
 
234
 *
 
235
 * Code that wants to use 1-byte-header values without detoasting should
 
236
 * use VARSIZE_ANY/VARSIZE_ANY_EXHDR/VARDATA_ANY.  The other macros here
 
237
 * should usually be used only by tuple assembly/disassembly code and
 
238
 * code that specifically wants to work with still-toasted Datums.
 
239
 *
 
240
 * WARNING: It is only safe to use VARDATA_ANY() -- typically with
 
241
 * PG_DETOAST_DATUM_PACKED() -- if you really don't care about the alignment.
 
242
 * Either because you're working with something like text where the alignment
 
243
 * doesn't matter or because you're not going to access its constituent parts
 
244
 * and just use things like memcpy on it anyways.
 
245
 */
 
246
#define VARDATA(PTR)                                            VARDATA_4B(PTR)
 
247
#define VARSIZE(PTR)                                            VARSIZE_4B(PTR)
 
248
 
 
249
#define VARSIZE_SHORT(PTR)                                      VARSIZE_1B(PTR)
 
250
#define VARDATA_SHORT(PTR)                                      VARDATA_1B(PTR)
 
251
 
 
252
#define VARSIZE_EXTERNAL(PTR)                           VARSIZE_1B_E(PTR)
 
253
#define VARDATA_EXTERNAL(PTR)                           VARDATA_1B_E(PTR)
 
254
 
 
255
#define VARATT_IS_COMPRESSED(PTR)                       VARATT_IS_4B_C(PTR)
 
256
#define VARATT_IS_EXTERNAL(PTR)                         VARATT_IS_1B_E(PTR)
 
257
#define VARATT_IS_SHORT(PTR)                            VARATT_IS_1B(PTR)
 
258
#define VARATT_IS_EXTENDED(PTR)                         (!VARATT_IS_4B_U(PTR))
 
259
 
 
260
#define SET_VARSIZE(PTR, len)                           SET_VARSIZE_4B(PTR, len)
 
261
#define SET_VARSIZE_SHORT(PTR, len)                     SET_VARSIZE_1B(PTR, len)
 
262
#define SET_VARSIZE_COMPRESSED(PTR, len)        SET_VARSIZE_4B_C(PTR, len)
 
263
#define SET_VARSIZE_EXTERNAL(PTR, len)          SET_VARSIZE_1B_E(PTR, len)
 
264
 
 
265
#define VARSIZE_ANY(PTR) \
 
266
        (VARATT_IS_1B_E(PTR) ? VARSIZE_1B_E(PTR) : \
 
267
         (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR) : \
 
268
          VARSIZE_4B(PTR)))
 
269
 
 
270
#define VARSIZE_ANY_EXHDR(PTR) \
 
271
        (VARATT_IS_1B_E(PTR) ? VARSIZE_1B_E(PTR)-VARHDRSZ_EXTERNAL : \
 
272
         (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR)-VARHDRSZ_SHORT : \
 
273
          VARSIZE_4B(PTR)-VARHDRSZ))
 
274
 
 
275
/* caution: this will not work on an external or compressed-in-line Datum */
 
276
/* caution: this will return a possibly unaligned pointer */
 
277
#define VARDATA_ANY(PTR) \
 
278
         (VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR))
 
279
 
 
280
 
 
281
/* ----------------------------------------------------------------
 
282
 *                              Section 2:      datum type + support macros
 
283
 * ----------------------------------------------------------------
 
284
 */
 
285
 
 
286
/*
 
287
 * Port Notes:
 
288
 *      Postgres makes the following assumptions about datatype sizes:
 
289
 *
 
290
 *      sizeof(Datum) == sizeof(void *) == 4 or 8
 
291
 *      sizeof(char) == 1
 
292
 *      sizeof(short) == 2
 
293
 *
 
294
 * When a type narrower than Datum is stored in a Datum, we place it in the
 
295
 * low-order bits and are careful that the DatumGetXXX macro for it discards
 
296
 * the unused high-order bits (as opposed to, say, assuming they are zero).
 
297
 * This is needed to support old-style user-defined functions, since depending
 
298
 * on architecture and compiler, the return value of a function returning char
 
299
 * or short may contain garbage when called as if it returned Datum.
 
300
 */
 
301
 
 
302
typedef uintptr_t Datum;
 
303
 
 
304
#define SIZEOF_DATUM SIZEOF_VOID_P
 
305
 
 
306
typedef Datum *DatumPtr;
 
307
 
 
308
#define GET_1_BYTE(datum)       (((Datum) (datum)) & 0x000000ff)
 
309
#define GET_2_BYTES(datum)      (((Datum) (datum)) & 0x0000ffff)
 
310
#define GET_4_BYTES(datum)      (((Datum) (datum)) & 0xffffffff)
 
311
#if SIZEOF_DATUM == 8
 
312
#define GET_8_BYTES(datum)      ((Datum) (datum))
 
313
#endif
 
314
#define SET_1_BYTE(value)       (((Datum) (value)) & 0x000000ff)
 
315
#define SET_2_BYTES(value)      (((Datum) (value)) & 0x0000ffff)
 
316
#define SET_4_BYTES(value)      (((Datum) (value)) & 0xffffffff)
 
317
#if SIZEOF_DATUM == 8
 
318
#define SET_8_BYTES(value)      ((Datum) (value))
 
319
#endif
 
320
 
 
321
/*
 
322
 * DatumGetBool
 
323
 *              Returns boolean value of a datum.
 
324
 *
 
325
 * Note: any nonzero value will be considered TRUE, but we ignore bits to
 
326
 * the left of the width of bool, per comment above.
 
327
 */
 
328
 
 
329
#define DatumGetBool(X) ((bool) (((bool) (X)) != 0))
 
330
 
 
331
/*
 
332
 * BoolGetDatum
 
333
 *              Returns datum representation for a boolean.
 
334
 *
 
335
 * Note: any nonzero value will be considered TRUE.
 
336
 */
 
337
 
 
338
#define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
 
339
 
 
340
/*
 
341
 * DatumGetChar
 
342
 *              Returns character value of a datum.
 
343
 */
 
344
 
 
345
#define DatumGetChar(X) ((char) GET_1_BYTE(X))
 
346
 
 
347
/*
 
348
 * CharGetDatum
 
349
 *              Returns datum representation for a character.
 
350
 */
 
351
 
 
352
#define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
 
353
 
 
354
/*
 
355
 * Int8GetDatum
 
356
 *              Returns datum representation for an 8-bit integer.
 
357
 */
 
358
 
 
359
#define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
 
360
 
 
361
/*
 
362
 * DatumGetUInt8
 
363
 *              Returns 8-bit unsigned integer value of a datum.
 
364
 */
 
365
 
 
366
#define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
 
367
 
 
368
/*
 
369
 * UInt8GetDatum
 
370
 *              Returns datum representation for an 8-bit unsigned integer.
 
371
 */
 
372
 
 
373
#define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
 
374
 
 
375
/*
 
376
 * DatumGetInt16
 
377
 *              Returns 16-bit integer value of a datum.
 
378
 */
 
379
 
 
380
#define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
 
381
 
 
382
/*
 
383
 * Int16GetDatum
 
384
 *              Returns datum representation for a 16-bit integer.
 
385
 */
 
386
 
 
387
#define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
 
388
 
 
389
/*
 
390
 * DatumGetUInt16
 
391
 *              Returns 16-bit unsigned integer value of a datum.
 
392
 */
 
393
 
 
394
#define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
 
395
 
 
396
/*
 
397
 * UInt16GetDatum
 
398
 *              Returns datum representation for a 16-bit unsigned integer.
 
399
 */
 
400
 
 
401
#define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
 
402
 
 
403
/*
 
404
 * DatumGetInt32
 
405
 *              Returns 32-bit integer value of a datum.
 
406
 */
 
407
 
 
408
#define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
 
409
 
 
410
/*
 
411
 * Int32GetDatum
 
412
 *              Returns datum representation for a 32-bit integer.
 
413
 */
 
414
 
 
415
#define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
 
416
 
 
417
/*
 
418
 * DatumGetUInt32
 
419
 *              Returns 32-bit unsigned integer value of a datum.
 
420
 */
 
421
 
 
422
#define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
 
423
 
 
424
/*
 
425
 * UInt32GetDatum
 
426
 *              Returns datum representation for a 32-bit unsigned integer.
 
427
 */
 
428
 
 
429
#define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
 
430
 
 
431
/*
 
432
 * DatumGetObjectId
 
433
 *              Returns object identifier value of a datum.
 
434
 */
 
435
 
 
436
#define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
 
437
 
 
438
/*
 
439
 * ObjectIdGetDatum
 
440
 *              Returns datum representation for an object identifier.
 
441
 */
 
442
 
 
443
#define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
 
444
 
 
445
/*
 
446
 * DatumGetTransactionId
 
447
 *              Returns transaction identifier value of a datum.
 
448
 */
 
449
 
 
450
#define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
 
451
 
 
452
/*
 
453
 * TransactionIdGetDatum
 
454
 *              Returns datum representation for a transaction identifier.
 
455
 */
 
456
 
 
457
#define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
 
458
 
 
459
/*
 
460
 * DatumGetCommandId
 
461
 *              Returns command identifier value of a datum.
 
462
 */
 
463
 
 
464
#define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
 
465
 
 
466
/*
 
467
 * CommandIdGetDatum
 
468
 *              Returns datum representation for a command identifier.
 
469
 */
 
470
 
 
471
#define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
 
472
 
 
473
/*
 
474
 * DatumGetPointer
 
475
 *              Returns pointer value of a datum.
 
476
 */
 
477
 
 
478
#define DatumGetPointer(X) ((Pointer) (X))
 
479
 
 
480
/*
 
481
 * PointerGetDatum
 
482
 *              Returns datum representation for a pointer.
 
483
 */
 
484
 
 
485
#define PointerGetDatum(X) ((Datum) (X))
 
486
 
 
487
/*
 
488
 * DatumGetCString
 
489
 *              Returns C string (null-terminated string) value of a datum.
 
490
 *
 
491
 * Note: C string is not a full-fledged Postgres type at present,
 
492
 * but type input functions use this conversion for their inputs.
 
493
 */
 
494
 
 
495
#define DatumGetCString(X) ((char *) DatumGetPointer(X))
 
496
 
 
497
/*
 
498
 * CStringGetDatum
 
499
 *              Returns datum representation for a C string (null-terminated string).
 
500
 *
 
501
 * Note: C string is not a full-fledged Postgres type at present,
 
502
 * but type output functions use this conversion for their outputs.
 
503
 * Note: CString is pass-by-reference; caller must ensure the pointed-to
 
504
 * value has adequate lifetime.
 
505
 */
 
506
 
 
507
#define CStringGetDatum(X) PointerGetDatum(X)
 
508
 
 
509
/*
 
510
 * DatumGetName
 
511
 *              Returns name value of a datum.
 
512
 */
 
513
 
 
514
#define DatumGetName(X) ((Name) DatumGetPointer(X))
 
515
 
 
516
/*
 
517
 * NameGetDatum
 
518
 *              Returns datum representation for a name.
 
519
 *
 
520
 * Note: Name is pass-by-reference; caller must ensure the pointed-to
 
521
 * value has adequate lifetime.
 
522
 */
 
523
 
 
524
#define NameGetDatum(X) PointerGetDatum(X)
 
525
 
 
526
/*
 
527
 * DatumGetInt64
 
528
 *              Returns 64-bit integer value of a datum.
 
529
 *
 
530
 * Note: this macro hides whether int64 is pass by value or by reference.
 
531
 */
 
532
 
 
533
#ifdef USE_FLOAT8_BYVAL
 
534
#define DatumGetInt64(X) ((int64) GET_8_BYTES(X))
 
535
#else
 
536
#define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
 
537
#endif
 
538
 
 
539
/*
 
540
 * Int64GetDatum
 
541
 *              Returns datum representation for a 64-bit integer.
 
542
 *
 
543
 * Note: if int64 is pass by reference, this function returns a reference
 
544
 * to palloc'd space.
 
545
 */
 
546
 
 
547
#ifdef USE_FLOAT8_BYVAL
 
548
#define Int64GetDatum(X) ((Datum) SET_8_BYTES(X))
 
549
#else
 
550
extern Datum Int64GetDatum(int64 X);
 
551
#endif
 
552
 
 
553
/*
 
554
 * DatumGetFloat4
 
555
 *              Returns 4-byte floating point value of a datum.
 
556
 *
 
557
 * Note: this macro hides whether float4 is pass by value or by reference.
 
558
 */
 
559
 
 
560
#ifdef USE_FLOAT4_BYVAL
 
561
extern float4 DatumGetFloat4(Datum X);
 
562
#else
 
563
#define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
 
564
#endif
 
565
 
 
566
/*
 
567
 * Float4GetDatum
 
568
 *              Returns datum representation for a 4-byte floating point number.
 
569
 *
 
570
 * Note: if float4 is pass by reference, this function returns a reference
 
571
 * to palloc'd space.
 
572
 */
 
573
 
 
574
extern Datum Float4GetDatum(float4 X);
 
575
 
 
576
/*
 
577
 * DatumGetFloat8
 
578
 *              Returns 8-byte floating point value of a datum.
 
579
 *
 
580
 * Note: this macro hides whether float8 is pass by value or by reference.
 
581
 */
 
582
 
 
583
#ifdef USE_FLOAT8_BYVAL
 
584
extern float8 DatumGetFloat8(Datum X);
 
585
#else
 
586
#define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
 
587
#endif
 
588
 
 
589
/*
 
590
 * Float8GetDatum
 
591
 *              Returns datum representation for an 8-byte floating point number.
 
592
 *
 
593
 * Note: if float8 is pass by reference, this function returns a reference
 
594
 * to palloc'd space.
 
595
 */
 
596
 
 
597
extern Datum Float8GetDatum(float8 X);
 
598
 
 
599
 
 
600
/*
 
601
 * Int64GetDatumFast
 
602
 * Float8GetDatumFast
 
603
 * Float4GetDatumFast
 
604
 *
 
605
 * These macros are intended to allow writing code that does not depend on
 
606
 * whether int64, float8, float4 are pass-by-reference types, while not
 
607
 * sacrificing performance when they are.  The argument must be a variable
 
608
 * that will exist and have the same value for as long as the Datum is needed.
 
609
 * In the pass-by-ref case, the address of the variable is taken to use as
 
610
 * the Datum.  In the pass-by-val case, these will be the same as the non-Fast
 
611
 * macros.
 
612
 */
 
613
 
 
614
#ifdef USE_FLOAT8_BYVAL
 
615
#define Int64GetDatumFast(X)  Int64GetDatum(X)
 
616
#define Float8GetDatumFast(X) Float8GetDatum(X)
 
617
#else
 
618
#define Int64GetDatumFast(X)  PointerGetDatum(&(X))
 
619
#define Float8GetDatumFast(X) PointerGetDatum(&(X))
 
620
#endif
 
621
 
 
622
#ifdef USE_FLOAT4_BYVAL
 
623
#define Float4GetDatumFast(X) Float4GetDatum(X)
 
624
#else
 
625
#define Float4GetDatumFast(X) PointerGetDatum(&(X))
 
626
#endif
 
627
 
 
628
 
 
629
/* ----------------------------------------------------------------
 
630
 *                              Section 3:      exception handling definitions
 
631
 *                                                      Assert, Trap, etc macros
 
632
 * ----------------------------------------------------------------
 
633
 */
 
634
 
 
635
extern PGDLLIMPORT bool assert_enabled;
 
636
 
 
637
/*
 
638
 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
 
639
 * - plai  9/5/90
 
640
 *
 
641
 * It should _NOT_ be defined in releases or in benchmark copies
 
642
 */
 
643
 
 
644
/*
 
645
 * Trap
 
646
 *              Generates an exception if the given condition is true.
 
647
 */
 
648
#define Trap(condition, errorType) \
 
649
        do { \
 
650
                if ((assert_enabled) && (condition)) \
 
651
                        ExceptionalCondition(CppAsString(condition), (errorType), \
 
652
                                                                 __FILE__, __LINE__); \
 
653
        } while (0)
 
654
 
 
655
/*
 
656
 *      TrapMacro is the same as Trap but it's intended for use in macros:
 
657
 *
 
658
 *              #define foo(x) (AssertMacro(x != 0) && bar(x))
 
659
 *
 
660
 *      Isn't CPP fun?
 
661
 */
 
662
#define TrapMacro(condition, errorType) \
 
663
        ((bool) ((! assert_enabled) || ! (condition) || \
 
664
                         (ExceptionalCondition(CppAsString(condition), (errorType), \
 
665
                                                                   __FILE__, __LINE__))))
 
666
 
 
667
#ifndef USE_ASSERT_CHECKING
 
668
#define Assert(condition)
 
669
#define AssertMacro(condition)  ((void)true)
 
670
#define AssertArg(condition)
 
671
#define AssertState(condition)
 
672
#else
 
673
#define Assert(condition) \
 
674
                Trap(!(condition), "FailedAssertion")
 
675
 
 
676
#define AssertMacro(condition) \
 
677
                ((void) TrapMacro(!(condition), "FailedAssertion"))
 
678
 
 
679
#define AssertArg(condition) \
 
680
                Trap(!(condition), "BadArgument")
 
681
 
 
682
#define AssertState(condition) \
 
683
                Trap(!(condition), "BadState")
 
684
#endif   /* USE_ASSERT_CHECKING */
 
685
 
 
686
extern int ExceptionalCondition(const char *conditionName,
 
687
                                         const char *errorType,
 
688
                                         const char *fileName, int lineNumber);
 
689
 
 
690
#endif   /* POSTGRES_H */