c.h 17.0 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * c.h
4
 *	  Fundamental C definitions.  This is included by every .c file in
5 6 7 8 9
 *	  PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate).
 *
 *	  Note that the definitions here are not intended to be exposed to clients of
 *	  the frontend interface libraries --- so we don't worry much about polluting
 *	  the namespace with lots of stuff...
10 11
 *
 *
12
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
13
 * Portions Copyright (c) 1994, Regents of the University of California
14
 *
15
 * $Id: c.h,v 1.113 2001/12/03 17:44:52 tgl Exp $
16 17 18 19
 *
 *-------------------------------------------------------------------------
 */
/*
20
 *----------------------------------------------------------------
21
 *	 TABLE OF CONTENTS
22
 *
23
 *		When adding stuff to this file, please try to put stuff
24
 *		into the relevant section, or add new sections as appropriate.
25
 *
26 27
 *	  section	description
 *	  -------	------------------------------------------------
28
 *		0)		pg_config.h and standard system headers
29 30
 *		1)		hacks to cope with non-ANSI C compilers
 *		2)		bool, true, false, TRUE, FALSE, NULL
31
 *		3)		standard system types
32 33 34 35 36
 *		4)		IsValid macros for system types
 *		5)		offsetof, lengthof, endof, alignment
 *		6)		widely useful macros
 *		7)		random stuff
 *		8)		system-specific hacks
37
 *
38
 * NOTE: since this file is included by both frontend and backend modules, it's
B
Bruce Momjian 已提交
39
 * almost certainly wrong to put an "extern" declaration here.	typedefs and macros
40 41 42
 * are the kind of thing that might go here.
 *
 *----------------------------------------------------------------
43
 */
44
#ifndef C_H
45 46
#define C_H

47 48
/* We have to include stdlib.h here because it defines many of these macros
   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
*/
B
Bruce Momjian 已提交
51

52
#include "pg_config.h"
53
#include "postgres_ext.h"
B
Bruce Momjian 已提交
54

55
#include <stdio.h>
56
#include <stdlib.h>
57
#include <string.h>
58 59
#include <stddef.h>
#include <stdarg.h>
60 61 62
#ifdef STRING_H_WITH_STRINGS_H
#include <strings.h>
#endif
63

64
#ifdef __CYGWIN__
65
#include <errno.h>
B
Bruce Momjian 已提交
66
#include <sys/fcntl.h>			/* ensure O_BINARY is available */
67
#endif
68
#ifdef HAVE_SUPPORTDEFS_H
69 70
#include <SupportDefs.h>
#endif
71

72 73 74
/* Must be before gettext() games below */
#include <locale.h>

P
Peter Eisentraut 已提交
75 76 77 78 79
#ifdef ENABLE_NLS
#include <libintl.h>
#else
#define gettext(x) (x)
#endif
80 81
#define gettext_noop(x) (x)

82

83
/* ----------------------------------------------------------------
84
 *				Section 1: hacks to cope with non-ANSI C compilers
85
 *
86
 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
87
 * ----------------------------------------------------------------
88 89 90
 */

/*
B
Bruce Momjian 已提交
91
 * CppAsString
92
 *		Convert the argument to a string, using the C preprocessor.
B
Bruce Momjian 已提交
93
 * CppConcat
94
 *		Concatenate two arguments together, using the C preprocessor.
95 96 97
 *
 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
 * whether #identifier works, but if we have that we likely have ## too.
98
 */
99
#if defined(HAVE_STRINGIZE)
100

101 102
#define CppAsString(identifier) #identifier
#define CppConcat(x, y)			x##y
103

104
#else							/* !HAVE_STRINGIZE */
105

106
#define CppAsString(identifier) "identifier"
107 108 109

/*
 * CppIdentity -- On Reiser based cpp's this is used to concatenate
110 111 112 113 114
 *		two tokens.  That is
 *				CppIdentity(A)B ==> AB
 *		We renamed it to _private_CppIdentity because it should not
 *		be referenced outside this file.  On other cpp's it
 *		produces  A  B.
115 116
 */
#define _priv_CppIdentity(x)x
117
#define CppConcat(x, y)			_priv_CppIdentity(x)y
118
#endif   /* !HAVE_STRINGIZE */
B
Bruce Momjian 已提交
119 120 121 122 123

/*
 * dummyret is used to set return values in macros that use ?: to make
 * assignments.  gcc wants these to be void, other compilers like char
 */
B
Bruce Momjian 已提交
124
#ifdef __GNUC__					/* GNU cc */
B
Bruce Momjian 已提交
125
#define dummyret	void
126 127
#else
#define dummyret	char
128 129
#endif

130
#ifndef __GNUC__
131
#define __attribute__(_arg_)
132
#endif
133 134 135 136 137

/* ----------------------------------------------------------------
 *				Section 2:	bool, true, false, TRUE, FALSE, NULL
 * ----------------------------------------------------------------
 */
138

139 140 141 142
/*
 * bool
 *		Boolean value, either true or false.
 *
143 144
 * XXX for C++ compilers, we assume the compiler has a compatible
 * built-in definition of bool.
145 146 147 148 149
 */

/* BeOS defines bool already, but the compiler chokes on the
 * #ifndef unless we wrap it in this check.
 */
150
/* Also defined in interfaces/odbc/md5.h */
B
Bruce Momjian 已提交
151
#ifndef __BEOS__
152 153

#ifndef __cplusplus
154

155 156
#ifndef bool
typedef char bool;
157
#endif
158 159 160 161 162 163 164 165

#ifndef true
#define true	((bool) 1)
#endif

#ifndef false
#define false	((bool) 0)
#endif
166 167
#endif   /* not C++ */
#endif   /* __BEOS__ */
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

typedef bool *BoolPtr;

#ifndef TRUE
#define TRUE	1
#endif

#ifndef FALSE
#define FALSE	0
#endif

/*
 * NULL
 *		Null pointer.
 */
#ifndef NULL
#define NULL	((void *) 0)
#endif


188
/* ----------------------------------------------------------------
189
 *				Section 3:	standard system types
190 191 192
 * ----------------------------------------------------------------
 */

193
/*
B
Bruce Momjian 已提交
194
 * Pointer
195 196 197 198 199 200 201
 *		Variable holding address of any memory resident object.
 *
 *		XXX Pointer arithmetic is done with this, so it can't be void *
 *		under "true" ANSI compilers.
 */
typedef char *Pointer;

202
/*
B
Bruce Momjian 已提交
203
 * intN
204 205 206
 *		Signed integer, EXACTLY N BITS IN SIZE,
 *		used for numerical computations and the
 *		frontend/backend protocol.
207
 */
208
#ifndef HAVE_INT8
209 210 211
typedef signed char int8;		/* == 8 bits */
typedef signed short int16;		/* == 16 bits */
typedef signed int int32;		/* == 32 bits */
212
#endif /* not HAVE_INT8 */
213

214
/*
B
Bruce Momjian 已提交
215
 * uintN
216 217 218
 *		Unsigned integer, EXACTLY N BITS IN SIZE,
 *		used for numerical computations and the
 *		frontend/backend protocol.
219
 */
220
/* Also defined in interfaces/odbc/md5.h */
221
#ifndef HAVE_UINT8
222 223 224
typedef unsigned char uint8;	/* == 8 bits */
typedef unsigned short uint16;	/* == 16 bits */
typedef unsigned int uint32;	/* == 32 bits */
225
#endif /* not HAVE_UINT8 */
226 227

/*
B
Bruce Momjian 已提交
228
 * boolN
229
 *		Boolean value, AT LEAST N BITS IN SIZE.
230
 */
231 232 233
typedef uint8 bool8;			/* >= 8 bits */
typedef uint16 bool16;			/* >= 16 bits */
typedef uint32 bool32;			/* >= 32 bits */
234 235

/*
B
Bruce Momjian 已提交
236
 * bitsN
237
 *		Unit of bitwise operation, AT LEAST N BITS IN SIZE.
238
 */
239 240 241
typedef uint8 bits8;			/* >= 8 bits */
typedef uint16 bits16;			/* >= 16 bits */
typedef uint32 bits32;			/* >= 32 bits */
242 243

/*
B
Bruce Momjian 已提交
244
 * wordN
245 246
 *		Unit of storage, AT LEAST N BITS IN SIZE,
 *		used to fetch/store data.
247
 */
248 249 250
typedef uint8 word8;			/* >= 8 bits */
typedef uint16 word16;			/* >= 16 bits */
typedef uint32 word32;			/* >= 32 bits */
251 252

/*
253 254 255
 * floatN
 *		Floating point number, AT LEAST N BITS IN SIZE,
 *		used for numerical computations.
256
 *
257 258
 *		Since sizeof(floatN) may be > sizeof(char *), always pass
 *		floatN by reference.
259
 *
260 261
 * XXX: these typedefs are now deprecated in favor of float4 and float8.
 * They will eventually go away.
262
 */
263 264 265 266
typedef float float32data;
typedef double float64data;
typedef float *float32;
typedef double *float64;
267

268
/*
269
 * 64-bit integers
270 271 272
 */
#ifdef HAVE_LONG_INT_64
/* Plain "long int" fits, use it */
273 274

#ifndef HAVE_INT64
275
typedef long int int64;
276
#endif
277
#ifndef HAVE_UINT64
278
typedef unsigned long int uint64;
279
#endif
B
Bruce Momjian 已提交
280

281
#elif defined(HAVE_LONG_LONG_INT_64)
282
/* We have working support for "long long int", use that */
283 284

#ifndef HAVE_INT64
285
typedef long long int int64;
286
#endif
287
#ifndef HAVE_UINT64
288
typedef unsigned long long int uint64;
289
#endif
B
Bruce Momjian 已提交
290

291 292
#else /* not HAVE_LONG_INT_64 and not HAVE_LONG_LONG_INT_64 */

293
/* Won't actually work, but fall back to long int so that code compiles */
294
#ifndef HAVE_INT64
295
typedef long int int64;
296
#endif
297
#ifndef HAVE_UINT64
298
typedef unsigned long int uint64;
299
#endif
B
Bruce Momjian 已提交
300

301
#define INT64_IS_BUSTED
302 303

#endif /* not HAVE_LONG_INT_64 and not HAVE_LONG_LONG_INT_64 */
304

305 306 307 308 309
/* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
#ifndef HAVE_SIG_ATOMIC_T
typedef int sig_atomic_t;
#endif

310
/*
311 312
 * Size
 *		Size of any memory resident object, as returned by sizeof.
313
 */
314
typedef size_t Size;
315 316

/*
317 318
 * Index
 *		Index into any memory resident array.
319
 *
320 321
 * Note:
 *		Indices are non negative.
322
 */
323
typedef unsigned int Index;
324 325

/*
326 327
 * Offset
 *		Offset into any memory resident array.
328
 *
329 330 331
 * Note:
 *		This differs from an Index in that an Index is always
 *		non negative, whereas Offset may be negative.
332
 */
333
typedef signed int Offset;
334 335

/*
336
 * Common Postgres datatype names (as used in the catalogs)
337
 */
338 339 340 341
typedef int16 int2;
typedef int32 int4;
typedef float float4;
typedef double float8;
342 343

/*
344
 * Oid, RegProcedure, TransactionId, CommandId
345 346
 */

347
/* typedef Oid is in postgres_ext.h */
348

349 350 351
/* unfortunately, both regproc and RegProcedure are used */
typedef Oid regproc;
typedef Oid RegProcedure;
352

353
typedef uint32 TransactionId;
354

355
typedef uint32 CommandId;
356

357
#define FirstCommandId	((CommandId) 0)
358 359

/*
360
 * Array indexing support
361
 */
362 363 364 365 366
#define MAXDIM 6
typedef struct
{
	int			indx[MAXDIM];
} IntArray;
367

368 369
/* ----------------
 *		Variable-length datatypes all share the 'struct varlena' header.
370
 *
371 372 373 374 375
 * NOTE: for TOASTable types, this is an oversimplification, since the value may be
 * compressed or moved out-of-line.  However datatype-specific routines are mostly
 * content to deal with de-TOASTed values only, and of course client-side routines
 * should never see a TOASTed value.  See postgres.h for details of the TOASTed form.
 * ----------------
376
 */
377 378 379 380 381
struct varlena
{
	int32		vl_len;
	char		vl_dat[1];
};
382

383
#define VARHDRSZ		((int32) sizeof(int32))
384 385

/*
386 387 388
 * These widely-used datatypes are just a varlena header and the data bytes.
 * There is no terminating null or anything like that --- the data length is
 * always VARSIZE(ptr) - VARHDRSZ.
389
 */
390 391 392
typedef struct varlena bytea;
typedef struct varlena text;
typedef struct varlena BpChar;	/* blank-padded char, ie SQL char(n) */
B
Bruce Momjian 已提交
393
typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
394 395

/*
396
 * Fixed-length array types (these are not varlena's!)
397 398
 */

399 400
typedef int2 int2vector[INDEX_MAX_KEYS];
typedef Oid oidvector[INDEX_MAX_KEYS];
401

402
/*
403 404 405
 * We want NameData to have length NAMEDATALEN and int alignment,
 * because that's how the data type 'name' is defined in pg_type.
 * Use a union to make sure the compiler agrees.
406
 */
407 408 409 410 411 412
typedef union nameData
{
	char		data[NAMEDATALEN];
	int			alignmentDummy;
} NameData;
typedef NameData *Name;
413

414
#define NameStr(name)	((name).data)
415 416


417
/* ----------------------------------------------------------------
418
 *				Section 4:	IsValid macros for system types
419 420 421
 * ----------------------------------------------------------------
 */
/*
B
Bruce Momjian 已提交
422
 * BoolIsValid
423
 *		True iff bool is valid.
424
 */
425
#define BoolIsValid(boolean)	((boolean) == false || (boolean) == true)
426 427

/*
B
Bruce Momjian 已提交
428
 * PointerIsValid
429
 *		True iff pointer is valid.
430
 */
431
#define PointerIsValid(pointer) ((void*)(pointer) != NULL)
432 433

/*
B
Bruce Momjian 已提交
434
 * PointerIsAligned
435
 *		True iff pointer is properly aligned to point to the given type.
436
 */
437 438
#define PointerIsAligned(pointer, type) \
		(((long)(pointer) % (sizeof (type))) == 0)
439

440 441 442 443 444
#define OidIsValid(objectId)  ((bool) ((objectId) != InvalidOid))

#define RegProcedureIsValid(p)	OidIsValid(p)


445
/* ----------------------------------------------------------------
446
 *				Section 5:	offsetof, lengthof, endof, alignment
447 448 449
 * ----------------------------------------------------------------
 */
/*
B
Bruce Momjian 已提交
450
 * offsetof
451
 *		Offset of a structure/union field within that structure/union.
452
 *
453 454
 *		XXX This is supposed to be part of stddef.h, but isn't on
 *		some systems (like SunOS 4).
455 456 457
 */
#ifndef offsetof
#define offsetof(type, field)	((long) &((type *)0)->field)
458
#endif   /* offsetof */
459 460

/*
B
Bruce Momjian 已提交
461
 * lengthof
462
 *		Number of elements in an array.
463
 */
464
#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
465 466

/*
B
Bruce Momjian 已提交
467
 * endof
468
 *		Address of the element one past the last in an array.
469 470 471
 */
#define endof(array)	(&array[lengthof(array)])

472 473
/* ----------------
 * Alignment macros: align a length or address appropriately for a given type.
474
 *
475 476
 * There used to be some incredibly crufty platform-dependent hackery here,
 * but now we rely on the configure script to get the info for us. Much nicer.
477
 *
478 479 480
 * NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
 * That case seems extremely unlikely to occur in practice, however.
 * ----------------
481 482
 */

483
#define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
484

485 486 487 488 489
#define SHORTALIGN(LEN)			TYPEALIGN(ALIGNOF_SHORT, (LEN))
#define INTALIGN(LEN)			TYPEALIGN(ALIGNOF_INT, (LEN))
#define LONGALIGN(LEN)			TYPEALIGN(ALIGNOF_LONG, (LEN))
#define DOUBLEALIGN(LEN)		TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
#define MAXALIGN(LEN)			TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
490 491 492


/* ----------------------------------------------------------------
493
 *				Section 6:	widely useful macros
494 495 496
 * ----------------------------------------------------------------
 */
/*
B
Bruce Momjian 已提交
497
 * Max
498
 *		Return the maximum of two numbers.
499
 */
500
#define Max(x, y)		((x) > (y) ? (x) : (y))
501 502

/*
B
Bruce Momjian 已提交
503
 * Min
504
 *		Return the minimum of two numbers.
505
 */
506
#define Min(x, y)		((x) < (y) ? (x) : (y))
507 508

/*
B
Bruce Momjian 已提交
509
 * Abs
510
 *		Return the absolute value of the argument.
511
 */
512
#define Abs(x)			((x) >= 0 ? (x) : -(x))
513

514
/*
B
Bruce Momjian 已提交
515
 * StrNCpy
516 517 518 519 520 521 522 523 524 525
 *	Like standard library function strncpy(), except that result string
 *	is guaranteed to be null-terminated --- that is, at most N-1 bytes
 *	of the source string will be kept.
 *	Also, the macro returns no result (too hard to do that without
 *	evaluating the arguments multiple times, which seems worse).
 *
 *	BTW: when you need to copy a non-null-terminated string (like a text
 *	datum) and add a null, do not do it with StrNCpy(..., len+1).  That
 *	might seem to work, but it fetches one byte more than there is in the
 *	text object.  One fine day you'll have a SIGSEGV because there isn't
B
Bruce Momjian 已提交
526
 *	another byte before the end of memory.	Don't laugh, we've had real
527 528
 *	live bug reports from real live users over exactly this mistake.
 *	Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
529 530 531 532 533 534 535 536 537 538 539 540 541 542
 */
#define StrNCpy(dst,src,len) \
	do \
	{ \
		char * _dst = (dst); \
		Size _len = (len); \
\
		if (_len > 0) \
		{ \
			strncpy(_dst, (src), _len); \
			_dst[_len-1] = '\0'; \
		} \
	} while (0)

543

B
Bruce Momjian 已提交
544 545 546
/* Get a bit mask of the bits set in non-int32 aligned addresses */
#define INT_ALIGN_MASK (sizeof(int32) - 1)

B
Bruce Momjian 已提交
547
/*
548 549 550 551 552 553
 * MemSet
 *	Exactly the same as standard library function memset(), but considerably
 *	faster for zeroing small word-aligned structures (such as parsetree nodes).
 *	This has to be a macro because the main point is to avoid function-call
 *	overhead.
 *
B
Bruce Momjian 已提交
554
 *	We got the 64 number by testing this against the stock memset() on
555
 *	BSD/OS 3.0. Larger values were slower.	bjm 1997/09/11
556
 *
557
 *	I think the crossover point could be a good deal higher for
558
 *	most platforms, actually.  tgl 2000-03-19
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
 */
#define MemSet(start, val, len) \
	do \
	{ \
		int32 * _start = (int32 *) (start); \
		int		_val = (val); \
		Size	_len = (len); \
\
		if ((((long) _start) & INT_ALIGN_MASK) == 0 && \
			(_len & INT_ALIGN_MASK) == 0 && \
			_val == 0 && \
			_len <= MEMSET_LOOP_LIMIT) \
		{ \
			int32 * _stop = (int32 *) ((char *) _start + _len); \
			while (_start < _stop) \
				*_start++ = 0; \
		} \
		else \
			memset((char *) _start, _val, _len); \
	} while (0)

#define MEMSET_LOOP_LIMIT  64

582

583
/* ----------------------------------------------------------------
584
 *				Section 7:	random stuff
585 586 587
 * ----------------------------------------------------------------
 */

588 589
/* msb for char */
#define CSIGNBIT (0x80)
590

591 592
#define STATUS_OK				(0)
#define STATUS_ERROR			(-1)
593
#define STATUS_EOF				(-2)
594
#define STATUS_FOUND			(1)
595 596 597


/* ----------------------------------------------------------------
598
 *				Section 8: system-specific hacks
599
 *
600
 *		This should be limited to things that absolutely have to be
B
Bruce Momjian 已提交
601
 *		included in every source file.	The port-specific header file
602
 *		is usually a better place for this sort of thing.
603 604 605
 * ----------------------------------------------------------------
 */

606
#ifdef __CYGWIN__
607
#define PG_BINARY	O_BINARY
B
Bruce Momjian 已提交
608 609
#define PG_BINARY_R "rb"
#define PG_BINARY_W "wb"
610
#else
B
Bruce Momjian 已提交
611 612 613
#define PG_BINARY	0
#define PG_BINARY_R "r"
#define PG_BINARY_W "w"
614 615
#endif

616
#if defined(sun) && defined(__sparc__) && !defined(__SVR4)
617
#include <unistd.h>
618 619 620
#endif

/* These are for things that are one way on Unix and another on NT */
621
#define NULL_DEV		"/dev/null"
622

M
 
Marc G. Fournier 已提交
623
/* defines for dynamic linking on Win32 platform */
624
#ifdef __CYGWIN__
M
 
Marc G. Fournier 已提交
625 626 627 628 629
#if __GNUC__ && ! defined (__declspec)
#error You need egcs 1.1 or newer for compiling!
#endif
#ifdef BUILDING_DLL
#define DLLIMPORT __declspec (dllexport)
B
Bruce Momjian 已提交
630
#else							/* not BUILDING_DLL */
M
 
Marc G. Fournier 已提交
631 632
#define DLLIMPORT __declspec (dllimport)
#endif
633
#elif defined(WIN32) && defined(_MSC_VER)		/* not CYGWIN */
B
Bruce Momjian 已提交
634 635 636 637 638 639
#if defined(_DLL)
#define DLLIMPORT __declspec (dllexport)
#else							/* not _DLL */
#define DLLIMPORT __declspec (dllimport)
#endif
#else							/* not CYGWIN, not MSVC */
M
 
Marc G. Fournier 已提交
640 641 642
#define DLLIMPORT
#endif

643
/* Provide prototypes for routines not present in a particular machine's
644 645
 * standard C library.	It'd be better to put these in pg_config.h, but
 * in pg_config.h we haven't yet included anything that defines size_t...
646 647
 */

648
#ifndef HAVE_SNPRINTF_DECL
649 650
extern int
snprintf(char *str, size_t count, const char *fmt,...)
651 652
/* This extension allows gcc to check the format string */
__attribute__((format(printf, 3, 4)));
653 654
#endif

655
#ifndef HAVE_VSNPRINTF_DECL
B
Bruce Momjian 已提交
656
extern int	vsnprintf(char *str, size_t count, const char *fmt, va_list args);
657
#endif
B
Bruce Momjian 已提交
658

659 660
#if !defined(HAVE_MEMMOVE) && !defined(memmove)
#define memmove(d, s, c)		bcopy(s, d, c)
661 662
#endif

663
/* ----------------
664
 *		end of c.h
665 666
 * ----------------
 */
667

668
#endif   /* C_H */