c.h 23.8 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * c.h
4 5
 *	  Fundamental C definitions.  This is included by every .c file in
 *	  postgres.
6 7
 *
 *
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
8 9
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
10
 *
11
 * $Id: c.h,v 1.80 2000/07/17 04:35:55 tgl Exp $
12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
/*
16
 *	 TABLE OF CONTENTS
17
 *
18
 *		When adding stuff to this file, please try to put stuff
19
 *		into the relevant section, or add new sections as appropriate.
20
 *
21 22
 *	  section	description
 *	  -------	------------------------------------------------
23 24
 *		1)		bool, true, false, TRUE, FALSE, NULL
 *		2)		non-ansi C definitions:
25
 *				type prefixes: const, signed, volatile, inline
26
 *				cpp magic macros
27 28 29 30 31
 *		3)		standard system types
 *		4)		datum type
 *		5)		IsValid macros for system types
 *		6)		offsetof, lengthof, endof
 *		7)		exception handling definitions, Assert, Trap, etc macros
32
 *		8)		Min, Max, Abs, StrNCpy macros
33
 *		9)		externs
34
 *		10)		Berkeley-specific defs
35
 *		11)		system-specific hacks
36 37 38
 *
 * ----------------------------------------------------------------
 */
39
#ifndef C_H
40 41
#define C_H

42 43
/* 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
44
   have its own.  The same goes for stddef and stdarg if present.
45
*/
Bruce Momjian's avatar
Bruce Momjian committed
46 47 48

#include "config.h"

49
#include <stdio.h>
50
#include <stdlib.h>
51
#include <string.h>
52 53
#include <stddef.h>
#include <stdarg.h>
54

55 56
#ifdef __CYGWIN32__
#include <errno.h>
57
#include <sys/fcntl.h>		/* ensure O_BINARY is available */
58 59
#endif

60
/* ----------------------------------------------------------------
61
 *				Section 1:	bool, true, false, TRUE, FALSE, NULL
62 63 64
 * ----------------------------------------------------------------
 */
/*
Bruce Momjian's avatar
Bruce Momjian committed
65
 * bool
66
 *		Boolean value, either true or false.
67 68
 *
 */
69
#ifndef __cplusplus
70
#ifndef bool
71
typedef char bool;
Bruce Momjian's avatar
Bruce Momjian committed
72 73

#endif	 /* ndef bool */
74
#endif	 /* not C++ */
75
#ifndef true
76
#define true	((bool) 1)
77 78 79 80
#endif
#ifndef false
#define false	((bool) 0)
#endif
81
typedef bool *BoolPtr;
82 83 84

#ifndef TRUE
#define TRUE	1
85
#endif	 /* TRUE */
86 87 88

#ifndef FALSE
#define FALSE	0
89
#endif	 /* FALSE */
90 91

/*
Bruce Momjian's avatar
Bruce Momjian committed
92
 * NULL
93
 *		Null pointer.
94
 */
95
#ifndef NULL
96
#define NULL	((void *) 0)
97
#endif	 /* !defined(NULL) */
98

99 100
/* ----------------------------------------------------------------
 *				Section 2: non-ansi C definitions:
101
 *
102 103 104
 *				type prefixes: const, signed, volatile, inline
 *				cpp magic macros
 * ----------------------------------------------------------------
105 106 107
 */

/*
Bruce Momjian's avatar
Bruce Momjian committed
108
 * CppAsString
109
 *		Convert the argument to a string, using the C preprocessor.
Bruce Momjian's avatar
Bruce Momjian committed
110
 * CppConcat
111
 *		Concatenate two arguments together, using the C preprocessor.
112 113 114
 *
 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
 * whether #identifier works, but if we have that we likely have ## too.
115
 */
116
#if defined(HAVE_STRINGIZE)
117

118 119
#define CppAsString(identifier) #identifier
#define CppConcat(x, y)			x##y
120

121
#else							/* !HAVE_STRINGIZE */
122

123
#define CppAsString(identifier) "identifier"
124 125 126

/*
 * CppIdentity -- On Reiser based cpp's this is used to concatenate
127 128 129 130 131
 *		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.
132 133
 */
#define _priv_CppIdentity(x)x
134
#define CppConcat(x, y)			_priv_CppIdentity(x)y
135

136
#endif	 /* !HAVE_STRINGIZE */
Bruce Momjian's avatar
Bruce Momjian committed
137 138 139 140 141

/*
 * dummyret is used to set return values in macros that use ?: to make
 * assignments.  gcc wants these to be void, other compilers like char
 */
Bruce Momjian's avatar
Bruce Momjian committed
142
#ifdef __GNUC__					/* GNU cc */
Bruce Momjian's avatar
Bruce Momjian committed
143
#define dummyret	void
144 145
#else
#define dummyret	char
146 147 148
#endif

/* ----------------------------------------------------------------
149
 *				Section 3:	standard system types
150 151 152
 * ----------------------------------------------------------------
 */

153
/*
Bruce Momjian's avatar
Bruce Momjian committed
154
 * Pointer
155 156 157 158 159 160 161
 *		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;

162
/*
Bruce Momjian's avatar
Bruce Momjian committed
163
 * intN
164 165 166
 *		Signed integer, EXACTLY N BITS IN SIZE,
 *		used for numerical computations and the
 *		frontend/backend protocol.
167
 */
168 169 170
typedef signed char int8;		/* == 8 bits */
typedef signed short int16;		/* == 16 bits */
typedef signed int int32;		/* == 32 bits */
171 172

/*
Bruce Momjian's avatar
Bruce Momjian committed
173
 * uintN
174 175 176
 *		Unsigned integer, EXACTLY N BITS IN SIZE,
 *		used for numerical computations and the
 *		frontend/backend protocol.
177
 */
178 179 180
typedef unsigned char uint8;	/* == 8 bits */
typedef unsigned short uint16;	/* == 16 bits */
typedef unsigned int uint32;	/* == 32 bits */
181 182

/*
Bruce Momjian's avatar
Bruce Momjian committed
183
 * floatN
184 185
 *		Floating point number, AT LEAST N BITS IN SIZE,
 *		used for numerical computations.
186
 *
187 188
 *		Since sizeof(floatN) may be > sizeof(char *), always pass
 *		floatN by reference.
189
 */
190 191 192
typedef float float32data;
typedef double float64data;
typedef float *float32;
193
typedef double *float64;
194 195

/*
Bruce Momjian's avatar
Bruce Momjian committed
196
 * boolN
197
 *		Boolean value, AT LEAST N BITS IN SIZE.
198
 */
199 200 201
typedef uint8 bool8;			/* >= 8 bits */
typedef uint16 bool16;			/* >= 16 bits */
typedef uint32 bool32;			/* >= 32 bits */
202 203

/*
Bruce Momjian's avatar
Bruce Momjian committed
204
 * bitsN
205
 *		Unit of bitwise operation, AT LEAST N BITS IN SIZE.
206
 */
207 208 209
typedef uint8 bits8;			/* >= 8 bits */
typedef uint16 bits16;			/* >= 16 bits */
typedef uint32 bits32;			/* >= 32 bits */
210 211

/*
Bruce Momjian's avatar
Bruce Momjian committed
212
 * wordN
213 214
 *		Unit of storage, AT LEAST N BITS IN SIZE,
 *		used to fetch/store data.
215
 */
216 217 218
typedef uint8 word8;			/* >= 8 bits */
typedef uint16 word16;			/* >= 16 bits */
typedef uint32 word32;			/* >= 32 bits */
219 220

/*
Bruce Momjian's avatar
Bruce Momjian committed
221
 * Size
222
 *		Size of any memory resident object, as returned by sizeof.
223
 */
224
typedef size_t Size;
225 226

/*
Bruce Momjian's avatar
Bruce Momjian committed
227
 * Index
228
 *		Index into any memory resident array.
229 230
 *
 * Note:
231
 *		Indices are non negative.
232
 */
233
typedef unsigned int Index;
234 235

#define MAXDIM 6
236 237
typedef struct
{
238
	int			indx[MAXDIM];
239
} IntArray;
240 241

/*
Bruce Momjian's avatar
Bruce Momjian committed
242
 * Offset
243
 *		Offset into any memory resident array.
244 245
 *
 * Note:
246 247
 *		This differs from an Index in that an Index is always
 *		non negative, whereas Offset may be negative.
248
 */
249
typedef signed int Offset;
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/*
 * Common Postgres datatypes.
 */
typedef int16 int2;
typedef int32 int4;
typedef float float4;
typedef double float8;

#ifdef HAVE_LONG_INT_64
/* Plain "long int" fits, use it */
typedef long int int64;
#else
#ifdef HAVE_LONG_LONG_INT_64
/* We have working support for "long long int", use that */
typedef long long int int64;
#else
/* Won't actually work, but fall back to long int so that code compiles */
typedef long int int64;
#define INT64_IS_BUSTED
#endif
#endif

273
/* ----------------------------------------------------------------
274
 *				Section 4:	datum type + support macros
275 276 277
 * ----------------------------------------------------------------
 */
/*
Bruce Momjian's avatar
Bruce Momjian committed
278
 * datum.h
279
 *		POSTGRES abstract data type datum representation definitions.
280 281 282 283
 *
 * Note:
 *
 * Port Notes:
284
 *	Postgres makes the following assumption about machines:
285
 *
286
 *	sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
287
 *
288
 *	Postgres also assumes that
289
 *
290
 *	sizeof(char) == 1
291
 *
292
 *	and that
293
 *
294
 *	sizeof(short) == 2
295
 *
296 297
 *	If your machine meets these requirements, Datums should also be checked
 *	to see if the positioning is correct.
298 299 300
 */

typedef unsigned long Datum;	/* XXX sizeof(long) >= sizeof(void *) */
301
typedef Datum *DatumPtr;
302

303 304 305 306 307 308
#define GET_1_BYTE(datum)	(((Datum) (datum)) & 0x000000ff)
#define GET_2_BYTES(datum)	(((Datum) (datum)) & 0x0000ffff)
#define GET_4_BYTES(datum)	(((Datum) (datum)) & 0xffffffff)
#define SET_1_BYTE(value)	(((Datum) (value)) & 0x000000ff)
#define SET_2_BYTES(value)	(((Datum) (value)) & 0x0000ffff)
#define SET_4_BYTES(value)	(((Datum) (value)) & 0xffffffff)
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/*
 * DatumGetBool
 *		Returns boolean value of a datum.
 *
 * Note: any nonzero value will be considered TRUE.
 */

#define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))

/*
 * BoolGetDatum
 *		Returns datum representation for a boolean.
 *
 * Note: any nonzero value will be considered TRUE.
 */

#define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))

328
/*
Bruce Momjian's avatar
Bruce Momjian committed
329
 * DatumGetChar
330
 *		Returns character value of a datum.
331 332 333 334 335
 */

#define DatumGetChar(X) ((char) GET_1_BYTE(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
336
 * CharGetDatum
337
 *		Returns datum representation for a character.
338 339 340 341 342
 */

#define CharGetDatum(X) ((Datum) SET_1_BYTE(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
343
 * Int8GetDatum
344
 *		Returns datum representation for an 8-bit integer.
345 346 347 348 349
 */

#define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
350
 * DatumGetUInt8
351
 *		Returns 8-bit unsigned integer value of a datum.
352 353 354 355 356
 */

#define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
357
 * UInt8GetDatum
358
 *		Returns datum representation for an 8-bit unsigned integer.
359 360 361 362 363
 */

#define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
364
 * DatumGetInt16
365
 *		Returns 16-bit integer value of a datum.
366 367 368 369 370
 */

#define DatumGetInt16(X) ((int16) GET_2_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
371
 * Int16GetDatum
372
 *		Returns datum representation for a 16-bit integer.
373 374 375 376 377
 */

#define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
378
 * DatumGetUInt16
379
 *		Returns 16-bit unsigned integer value of a datum.
380 381 382 383 384
 */

#define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
385
 * UInt16GetDatum
386
 *		Returns datum representation for a 16-bit unsigned integer.
387 388 389 390 391
 */

#define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
392
 * DatumGetInt32
393
 *		Returns 32-bit integer value of a datum.
394 395 396 397 398
 */

#define DatumGetInt32(X) ((int32) GET_4_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
399
 * Int32GetDatum
400
 *		Returns datum representation for a 32-bit integer.
401 402 403 404 405
 */

#define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
406
 * DatumGetUInt32
407
 *		Returns 32-bit unsigned integer value of a datum.
408 409 410 411 412
 */

#define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
413
 * UInt32GetDatum
414
 *		Returns datum representation for a 32-bit unsigned integer.
415 416 417 418 419
 */

#define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
420
 * DatumGetObjectId
421
 *		Returns object identifier value of a datum.
422 423 424 425 426
 */

#define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
427
 * ObjectIdGetDatum
428
 *		Returns datum representation for an object identifier.
429 430 431 432 433
 */

#define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
434
 * DatumGetPointer
435
 *		Returns pointer value of a datum.
436 437
 */

438
#define DatumGetPointer(X) ((Pointer) (X))
439 440

/*
Bruce Momjian's avatar
Bruce Momjian committed
441
 * PointerGetDatum
442
 *		Returns datum representation for a pointer.
443 444
 */

445
#define PointerGetDatum(X) ((Datum) (X))
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
/*
 * DatumGetCString
 *		Returns C string (null-terminated string) value of a datum.
 *
 * Note: C string is not a full-fledged Postgres type at present,
 * but type input functions use this conversion for their inputs.
 */

#define DatumGetCString(X) ((char *) DatumGetPointer(X))

/*
 * CStringGetDatum
 *		Returns datum representation for a C string (null-terminated string).
 *
 * Note: C string is not a full-fledged Postgres type at present,
 * but type output functions use this conversion for their outputs.
 * Note: CString is pass-by-reference; caller must ensure the pointed-to
 * value has adequate lifetime.
 */

#define CStringGetDatum(X) PointerGetDatum(X)

469
/*
Bruce Momjian's avatar
Bruce Momjian committed
470
 * DatumGetName
471
 *		Returns name value of a datum.
472 473
 */

474
#define DatumGetName(X) ((Name) DatumGetPointer(X))
475 476

/*
Bruce Momjian's avatar
Bruce Momjian committed
477
 * NameGetDatum
478
 *		Returns datum representation for a name.
479 480 481
 *
 * Note: Name is pass-by-reference; caller must ensure the pointed-to
 * value has adequate lifetime.
482 483
 */

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
#define NameGetDatum(X) PointerGetDatum(X)

/*
 * DatumGetInt64
 *		Returns 64-bit integer value of a datum.
 *
 * Note: this macro hides the fact that int64 is currently a
 * pass-by-reference type.  Someday it may be pass-by-value,
 * at least on some platforms.
 */

#define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))

/*
 * Int64GetDatum
 *		Returns datum representation for a 64-bit integer.
 *
 * Note: this routine returns a reference to palloc'd space.
 */

extern Datum Int64GetDatum(int64 X);

/*
 * DatumGetFloat4
 *		Returns 4-byte floating point value of a datum.
 *
 * Note: this macro hides the fact that float4 is currently a
 * pass-by-reference type.  Someday it may be pass-by-value.
 */

#define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))

/*
 * Float4GetDatum
 *		Returns datum representation for a 4-byte floating point number.
 *
 * Note: this routine returns a reference to palloc'd space.
 */

extern Datum Float4GetDatum(float4 X);

/*
 * DatumGetFloat8
 *		Returns 8-byte floating point value of a datum.
 *
 * Note: this macro hides the fact that float8 is currently a
 * pass-by-reference type.  Someday it may be pass-by-value,
 * at least on some platforms.
 */

#define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))

/*
 * Float8GetDatum
 *		Returns datum representation for an 8-byte floating point number.
 *
 * Note: this routine returns a reference to palloc'd space.
 */

extern Datum Float8GetDatum(float8 X);
544 545 546


/*
Bruce Momjian's avatar
Bruce Momjian committed
547
 * DatumGetFloat32
548 549
 *		Returns 32-bit floating point value of a datum.
 *		This is really a pointer, of course.
550 551 552
 *
 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
 * It will eventually go away.
553 554
 */

555
#define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
556 557

/*
Bruce Momjian's avatar
Bruce Momjian committed
558
 * Float32GetDatum
559 560
 *		Returns datum representation for a 32-bit floating point number.
 *		This is really a pointer, of course.
561 562 563
 *
 * XXX: this macro is now deprecated in favor of Float4GetDatum.
 * It will eventually go away.
564 565
 */

566
#define Float32GetDatum(X) PointerGetDatum(X)
567 568

/*
Bruce Momjian's avatar
Bruce Momjian committed
569
 * DatumGetFloat64
570 571
 *		Returns 64-bit floating point value of a datum.
 *		This is really a pointer, of course.
572 573 574
 *
 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
 * It will eventually go away.
575 576 577 578 579
 */

#define DatumGetFloat64(X) ((float64) DatumGetPointer(X))

/*
Bruce Momjian's avatar
Bruce Momjian committed
580
 * Float64GetDatum
581 582
 *		Returns datum representation for a 64-bit floating point number.
 *		This is really a pointer, of course.
583 584 585
 *
 * XXX: this macro is now deprecated in favor of Float8GetDatum.
 * It will eventually go away.
586 587
 */

588
#define Float64GetDatum(X) PointerGetDatum(X)
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
/*
 * Int64GetDatumFast
 * Float4GetDatumFast
 * Float8GetDatumFast
 *
 * These macros are intended to allow writing code that does not depend on
 * whether int64, float4, float8 are pass-by-reference types, while not
 * sacrificing performance when they are.  The argument must be a variable
 * that will exist and have the same value for as long as the Datum is needed.
 * In the pass-by-ref case, the address of the variable is taken to use as
 * the Datum.  In the pass-by-val case, these will be the same as the non-Fast
 * macros.
 */

#define Int64GetDatumFast(X)  PointerGetDatum(&(X))
#define Float4GetDatumFast(X) PointerGetDatum(&(X))
#define Float8GetDatumFast(X) PointerGetDatum(&(X))


609
/* ----------------------------------------------------------------
610
 *				Section 5:	IsValid macros for system types
611 612 613
 * ----------------------------------------------------------------
 */
/*
Bruce Momjian's avatar
Bruce Momjian committed
614
 * BoolIsValid
615
 *		True iff bool is valid.
616
 */
617
#define BoolIsValid(boolean)	((boolean) == false || (boolean) == true)
618 619

/*
Bruce Momjian's avatar
Bruce Momjian committed
620
 * PointerIsValid
621
 *		True iff pointer is valid.
622
 */
623
#define PointerIsValid(pointer) ((void*)(pointer) != NULL)
624 625

/*
Bruce Momjian's avatar
Bruce Momjian committed
626
 * PointerIsAligned
627
 *		True iff pointer is properly aligned to point to the given type.
628
 */
629 630
#define PointerIsAligned(pointer, type) \
		(((long)(pointer) % (sizeof (type))) == 0)
631 632

/* ----------------------------------------------------------------
633
 *				Section 6:	offsetof, lengthof, endof
634 635 636
 * ----------------------------------------------------------------
 */
/*
Bruce Momjian's avatar
Bruce Momjian committed
637
 * offsetof
638
 *		Offset of a structure/union field within that structure/union.
639
 *
640 641
 *		XXX This is supposed to be part of stddef.h, but isn't on
 *		some systems (like SunOS 4).
642 643 644
 */
#ifndef offsetof
#define offsetof(type, field)	((long) &((type *)0)->field)
645
#endif	 /* offsetof */
646 647

/*
Bruce Momjian's avatar
Bruce Momjian committed
648
 * lengthof
649
 *		Number of elements in an array.
650
 */
651
#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
652 653

/*
Bruce Momjian's avatar
Bruce Momjian committed
654
 * endof
655
 *		Address of the element one past the last in an array.
656 657 658 659
 */
#define endof(array)	(&array[lengthof(array)])

/* ----------------------------------------------------------------
660 661
 *				Section 7:	exception handling definitions
 *							Assert, Trap, etc macros
662 663 664 665 666 667
 * ----------------------------------------------------------------
 */
/*
 * Exception Handling definitions
 */

668
typedef char *ExcMessage;
669 670
typedef struct Exception
{
671
	ExcMessage	message;
672
} Exception;
673 674

/*
675
 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
676 677
 * - plai  9/5/90
 *
678
 * It should _NOT_ be defined in releases or in benchmark copies
679 680 681
 */

/*
Bruce Momjian's avatar
Bruce Momjian committed
682
 * Trap
683
 *		Generates an exception if the given condition is true.
684 685 686
 *
 */
#define Trap(condition, exception) \
687 688
		do { \
			if ((assert_enabled) && (condition)) \
689
				ExceptionalCondition(CppAsString(condition), &(exception), \
690 691
						(char*)NULL, __FILE__, __LINE__); \
		} while (0)
692

693 694
/*
 *	TrapMacro is the same as Trap but it's intended for use in macros:
695
 *
696
 *		#define foo(x) (AssertM(x != 0) && bar(x))
697
 *
698
 *	Isn't CPP fun?
699 700
 */
#define TrapMacro(condition, exception) \
701
	((bool) ((! assert_enabled) || ! (condition) || \
702 703 704 705
			 (ExceptionalCondition(CppAsString(condition), \
								  &(exception), \
								  (char*) NULL, __FILE__, __LINE__))))

Bruce Momjian's avatar
Bruce Momjian committed
706
#ifndef USE_ASSERT_CHECKING
707
#define Assert(condition)
708
#define AssertMacro(condition)	((void)true)
709 710
#define AssertArg(condition)
#define AssertState(condition)
711
#define assert_enabled 0
712 713
#else
#define Assert(condition) \
714
		Trap(!(condition), FailedAssertion)
715 716

#define AssertMacro(condition) \
717
		((void) TrapMacro(!(condition), FailedAssertion))
718 719

#define AssertArg(condition) \
720
		Trap(!(condition), BadArg)
721 722

#define AssertState(condition) \
723
		Trap(!(condition), BadState)
724

725
extern bool	assert_enabled;
726 727

#endif	 /* USE_ASSERT_CHECKING */
728 729

/*
Bruce Momjian's avatar
Bruce Momjian committed
730
 * LogTrap
731
 *		Generates an exception with a message if the given condition is true.
732 733 734
 *
 */
#define LogTrap(condition, exception, printArgs) \
735 736
		do { \
			if ((assert_enabled) && (condition)) \
737
				ExceptionalCondition(CppAsString(condition), &(exception), \
738 739
						vararg_format printArgs, __FILE__, __LINE__); \
		} while (0)
740

741 742
/*
 *	LogTrapMacro is the same as LogTrap but it's intended for use in macros:
743
 *
744
 *		#define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
745 746
 */
#define LogTrapMacro(condition, exception, printArgs) \
747
	((bool) ((! assert_enabled) || ! (condition) || \
748 749
			 (ExceptionalCondition(CppAsString(condition), \
								   &(exception), \
Bruce Momjian's avatar
Bruce Momjian committed
750
								   vararg_format printArgs, __FILE__, __LINE__))))
751

Bruce Momjian's avatar
Bruce Momjian committed
752
#ifndef USE_ASSERT_CHECKING
753 754 755 756 757 758
#define LogAssert(condition, printArgs)
#define LogAssertMacro(condition, printArgs) true
#define LogAssertArg(condition, printArgs)
#define LogAssertState(condition, printArgs)
#else
#define LogAssert(condition, printArgs) \
759
		LogTrap(!(condition), FailedAssertion, printArgs)
760 761

#define LogAssertMacro(condition, printArgs) \
762
		LogTrapMacro(!(condition), FailedAssertion, printArgs)
763 764

#define LogAssertArg(condition, printArgs) \
765
		LogTrap(!(condition), BadArg, printArgs)
766 767

#define LogAssertState(condition, printArgs) \
768
		LogTrap(!(condition), BadState, printArgs)
769

770 771
#ifdef ASSERT_CHECKING_TEST
extern int	assertTest(int val);
772

773
#endif
774
#endif	 /* USE_ASSERT_CHECKING */
775 776

/* ----------------------------------------------------------------
777
 *				Section 8:	Min, Max, Abs macros
778 779 780
 * ----------------------------------------------------------------
 */
/*
Bruce Momjian's avatar
Bruce Momjian committed
781
 * Max
782
 *		Return the maximum of two numbers.
783
 */
784
#define Max(x, y)		((x) > (y) ? (x) : (y))
785 786

/*
Bruce Momjian's avatar
Bruce Momjian committed
787
 * Min
788
 *		Return the minimum of two numbers.
789
 */
790
#define Min(x, y)		((x) < (y) ? (x) : (y))
791 792

/*
Bruce Momjian's avatar
Bruce Momjian committed
793
 * Abs
794
 *		Return the absolute value of the argument.
795
 */
796
#define Abs(x)			((x) >= 0 ? (x) : -(x))
797

798
/*
Bruce Momjian's avatar
Bruce Momjian committed
799
 * StrNCpy
800 801 802 803 804 805 806 807 808 809 810 811 812
 *	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
 *	another byte before the end of memory.  Don't laugh, we've had real
 *	live bug reports from real live users over exactly this mistake.
 *	Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
813 814 815 816 817 818 819 820 821 822 823 824 825 826
 */
#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)

827

Bruce Momjian's avatar
Bruce Momjian committed
828 829 830
/* Get a bit mask of the bits set in non-int32 aligned addresses */
#define INT_ALIGN_MASK (sizeof(int32) - 1)

Bruce Momjian's avatar
Bruce Momjian committed
831
/*
832 833 834 835 836 837
 * 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.
 *
Bruce Momjian's avatar
Bruce Momjian committed
838
 *	We got the 64 number by testing this against the stock memset() on
839
 *	BSD/OS 3.0. Larger values were slower.	bjm 1997/09/11
840
 *
841
 *	I think the crossover point could be a good deal higher for
842
 *	most platforms, actually.  tgl 2000-03-19
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
 */
#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

866

867
/* ----------------------------------------------------------------
868
 *				Section 9: externs
869 870 871
 * ----------------------------------------------------------------
 */

872 873 874
extern Exception FailedAssertion;
extern Exception BadArg;
extern Exception BadState;
875 876

/* in utils/error/assert.c */
877
extern int ExceptionalCondition(char *conditionName,
878
					 Exception *exceptionP, char *details,
879
					 char *fileName, int lineNumber);
880 881 882


/* ----------------
Bruce Momjian's avatar
Bruce Momjian committed
883
 *		vararg_format is used by assert and the exception handling stuff
884 885
 * ----------------
 */
Bruce Momjian's avatar
Bruce Momjian committed
886
extern char *vararg_format(const char *fmt,...);
887 888 889 890



/* ----------------------------------------------------------------
891
 *				Section 10: berkeley-specific configuration
892 893 894 895 896 897 898
 *
 * this section contains settings which are only relevant to the UC Berkeley
 * sites.  Other sites can ignore this
 * ----------------------------------------------------------------
 */

/* ----------------
899
 *		storage managers
900
 *
901 902
 *		These are experimental and are not supported in the code that
 *		we distribute to other sites.
903 904
 * ----------------
 */
905 906
#ifdef NOT_USED
#define STABLE_MEMORY_STORAGE
907 908 909 910 911
#endif



/* ----------------------------------------------------------------
912
 *				Section 11: system-specific hacks
913
 *
914 915 916 917
 *		This should be limited to things that absolutely have to be
 *		included in every source file.	The changes should be factored
 *		into a separate file so that changes to one port don't require
 *		changes to c.h (and everyone recompiling their whole system).
918 919 920
 * ----------------------------------------------------------------
 */

Bruce Momjian's avatar
Bruce Momjian committed
921
#ifdef __CYGWIN32__
922
#define PG_BINARY	O_BINARY
923 924 925
#define	PG_BINARY_R	"rb"
#define	PG_BINARY_W	"wb"
#else
926
#define	PG_BINARY	0
927 928 929 930
#define	PG_BINARY_R	"r"
#define	PG_BINARY_W	"w"
#endif

931
#if defined(sun) && defined(__sparc__) && !defined(__SVR4)
932
#define memmove(d, s, l)		bcopy(s, d, l)
933
#include <unistd.h>
934
#include <varargs.h>
935 936 937
#endif

/* These are for things that are one way on Unix and another on NT */
938 939
#define NULL_DEV		"/dev/null"
#define SEP_CHAR		'/'
940

941 942 943 944 945 946 947
/* defines for dynamic linking on Win32 platform */
#ifdef __CYGWIN32__
#if __GNUC__ && ! defined (__declspec)
#error You need egcs 1.1 or newer for compiling!
#endif
#ifdef BUILDING_DLL
#define DLLIMPORT __declspec (dllexport)
Bruce Momjian's avatar
Bruce Momjian committed
948
#else							/* not BUILDING_DLL */
949 950
#define DLLIMPORT __declspec (dllimport)
#endif
Bruce Momjian's avatar
Bruce Momjian committed
951
#else							/* not CYGWIN */
952 953 954
#define DLLIMPORT
#endif

955
/* Provide prototypes for routines not present in a particular machine's
Bruce Momjian's avatar
Bruce Momjian committed
956
 * standard C library.	It'd be better to put these in config.h, but
957 958 959
 * in config.h we haven't yet included anything that defines size_t...
 */

960
#ifndef HAVE_SNPRINTF_DECL
Bruce Momjian's avatar
Bruce Momjian committed
961
extern int	snprintf(char *str, size_t count, const char *fmt,...);
962

963 964
#endif

965
#ifndef HAVE_VSNPRINTF_DECL
Bruce Momjian's avatar
Bruce Momjian committed
966
extern int	vsnprintf(char *str, size_t count, const char *fmt, va_list args);
967

968
#endif
Bruce Momjian's avatar
Bruce Momjian committed
969

970 971
#ifndef HAVE_MEMMOVE
#include <regex/utils.h>
972 973
#endif

974
/* ----------------
975
 *		end of c.h
976 977
 * ----------------
 */
978
#endif	 /* C_H */