00001 /*------------------------------------------------------------------------- 00002 * 00003 * c.h 00004 * Fundamental C definitions. This is included by every .c file in 00005 * PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate). 00006 * 00007 * Note that the definitions here are not intended to be exposed to clients 00008 * of the frontend interface libraries --- so we don't worry much about 00009 * polluting the namespace with lots of stuff... 00010 * 00011 * 00012 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group 00013 * Portions Copyright (c) 1994, Regents of the University of California 00014 * 00015 * $PostgreSQL: pgsql/src/include/c.h,v 1.236 2009/06/11 14:49:08 momjian Exp $ 00016 * 00017 *------------------------------------------------------------------------- 00018 */ 00019 /* 00020 *---------------------------------------------------------------- 00021 * TABLE OF CONTENTS 00022 * 00023 * When adding stuff to this file, please try to put stuff 00024 * into the relevant section, or add new sections as appropriate. 00025 * 00026 * section description 00027 * ------- ------------------------------------------------ 00028 * 0) pg_config.h and standard system headers 00029 * 1) hacks to cope with non-ANSI C compilers 00030 * 2) bool, true, false, TRUE, FALSE, NULL 00031 * 3) standard system types 00032 * 4) IsValid macros for system types 00033 * 5) offsetof, lengthof, endof, alignment 00034 * 6) widely useful macros 00035 * 7) random stuff 00036 * 8) system-specific hacks 00037 * 00038 * NOTE: since this file is included by both frontend and backend modules, it's 00039 * almost certainly wrong to put an "extern" declaration here. typedefs and 00040 * macros are the kind of thing that might go here. 00041 * 00042 *---------------------------------------------------------------- 00043 */ 00044 #ifndef C_H 00045 #define C_H 00046 00047 /* 00048 * We have to include stdlib.h here because it defines many of these macros 00049 * on some platforms, and we only want our definitions used if stdlib.h doesn't 00050 * have its own. The same goes for stddef and stdarg if present. 00051 */ 00052 00053 #include "pg_config.h" 00054 #include "pg_config_manual.h" /* must be after pg_config.h */ 00055 #if !defined(WIN32) && !defined(__CYGWIN__) /* win32 will include further 00056 * down */ 00057 #include "pg_config_os.h" /* must be before any system header files */ 00058 #endif 00059 #include "postgres_ext.h" 00060 00061 #if _MSC_VER >= 1400 00062 #define errcode __msvc_errcode 00063 #include <crtdefs.h> 00064 #undef errcode 00065 #endif 00066 00067 #include <stdio.h> 00068 #include <stdlib.h> 00069 #include <string.h> 00070 #include <stddef.h> 00071 #include <stdarg.h> 00072 #ifdef HAVE_STRINGS_H 00073 #include <strings.h> 00074 #endif 00075 #include <sys/types.h> 00076 00077 #include <errno.h> 00078 #if defined(WIN32) || defined(__CYGWIN__) 00079 #include <fcntl.h> /* ensure O_BINARY is available */ 00080 #endif 00081 #ifdef HAVE_SUPPORTDEFS_H 00082 #include <SupportDefs.h> 00083 #endif 00084 00085 #if defined(WIN32) || defined(__CYGWIN__) 00086 /* We have to redefine some system functions after they are included above. */ 00087 #include "pg_config_os.h" 00088 #endif 00089 00090 /* Must be before gettext() games below */ 00091 #include <locale.h> 00092 00093 #define _(x) gettext(x) 00094 00095 #ifdef ENABLE_NLS 00096 #include <libintl.h> 00097 #else 00098 #define gettext(x) (x) 00099 #define dgettext(d,x) (x) 00100 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p)) 00101 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p)) 00102 #endif 00103 00104 /* 00105 * Use this to mark string constants as needing translation at some later 00106 * time, rather than immediately. This is useful for cases where you need 00107 * access to the original string and translated string, and for cases where 00108 * immediate translation is not possible, like when initializing global 00109 * variables. 00110 * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html 00111 */ 00112 #define gettext_noop(x) (x) 00113 00114 00115 /* ---------------------------------------------------------------- 00116 * Section 1: hacks to cope with non-ANSI C compilers 00117 * 00118 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h. 00119 * ---------------------------------------------------------------- 00120 */ 00121 00122 /* 00123 * CppAsString 00124 * Convert the argument to a string, using the C preprocessor. 00125 * CppConcat 00126 * Concatenate two arguments together, using the C preprocessor. 00127 * 00128 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks 00129 * whether #identifier works, but if we have that we likely have ## too. 00130 */ 00131 #if defined(HAVE_STRINGIZE) 00132 00133 #define CppAsString(identifier) #identifier 00134 #define CppConcat(x, y) x##y 00135 #else /* !HAVE_STRINGIZE */ 00136 00137 #define CppAsString(identifier) "identifier" 00138 00139 /* 00140 * CppIdentity -- On Reiser based cpp's this is used to concatenate 00141 * two tokens. That is 00142 * CppIdentity(A)B ==> AB 00143 * We renamed it to _private_CppIdentity because it should not 00144 * be referenced outside this file. On other cpp's it 00145 * produces A B. 00146 */ 00147 #define _priv_CppIdentity(x)x 00148 #define CppConcat(x, y) _priv_CppIdentity(x)y 00149 #endif /* !HAVE_STRINGIZE */ 00150 00151 /* 00152 * dummyret is used to set return values in macros that use ?: to make 00153 * assignments. gcc wants these to be void, other compilers like char 00154 */ 00155 #ifdef __GNUC__ /* GNU cc */ 00156 #define dummyret void 00157 #else 00158 #define dummyret char 00159 #endif 00160 00161 #ifndef __GNUC__ 00162 #define __attribute__(_arg_) 00163 #endif 00164 00165 /* ---------------------------------------------------------------- 00166 * Section 2: bool, true, false, TRUE, FALSE, NULL 00167 * ---------------------------------------------------------------- 00168 */ 00169 00170 /* 00171 * bool 00172 * Boolean value, either true or false. 00173 * 00174 * XXX for C++ compilers, we assume the compiler has a compatible 00175 * built-in definition of bool. 00176 */ 00177 00178 #ifndef __cplusplus 00179 00180 #ifndef bool 00181 typedef char bool; 00182 #endif 00183 00184 #ifndef true 00185 #define true ((bool) 1) 00186 #endif 00187 00188 #ifndef false 00189 #define false ((bool) 0) 00190 #endif 00191 #endif /* not C++ */ 00192 00193 typedef bool *BoolPtr; 00194 00195 #ifndef TRUE 00196 #define TRUE 1 00197 #endif 00198 00199 #ifndef FALSE 00200 #define FALSE 0 00201 #endif 00202 00203 /* 00204 * NULL 00205 * Null pointer. 00206 */ 00207 #ifndef NULL 00208 #define NULL ((void *) 0) 00209 #endif 00210 00211 00212 /* ---------------------------------------------------------------- 00213 * Section 3: standard system types 00214 * ---------------------------------------------------------------- 00215 */ 00216 00217 /* 00218 * Pointer 00219 * Variable holding address of any memory resident object. 00220 * 00221 * XXX Pointer arithmetic is done with this, so it can't be void * 00222 * under "true" ANSI compilers. 00223 */ 00224 typedef char *Pointer; 00225 00226 /* 00227 * intN 00228 * Signed integer, EXACTLY N BITS IN SIZE, 00229 * used for numerical computations and the 00230 * frontend/backend protocol. 00231 */ 00232 #ifndef HAVE_INT8 00233 typedef signed char int8; /* == 8 bits */ 00234 typedef signed short int16; /* == 16 bits */ 00235 typedef signed int int32; /* == 32 bits */ 00236 #endif /* not HAVE_INT8 */ 00237 00238 /* 00239 * uintN 00240 * Unsigned integer, EXACTLY N BITS IN SIZE, 00241 * used for numerical computations and the 00242 * frontend/backend protocol. 00243 */ 00244 #ifndef HAVE_UINT8 00245 typedef unsigned char uint8; /* == 8 bits */ 00246 typedef unsigned short uint16; /* == 16 bits */ 00247 typedef unsigned int uint32; /* == 32 bits */ 00248 #endif /* not HAVE_UINT8 */ 00249 00250 /* 00251 * bitsN 00252 * Unit of bitwise operation, AT LEAST N BITS IN SIZE. 00253 */ 00254 typedef uint8 bits8; /* >= 8 bits */ 00255 typedef uint16 bits16; /* >= 16 bits */ 00256 typedef uint32 bits32; /* >= 32 bits */ 00257 00258 /* 00259 * 64-bit integers 00260 */ 00261 #ifdef HAVE_LONG_INT_64 00262 /* Plain "long int" fits, use it */ 00263 00264 #ifndef HAVE_INT64 00265 typedef long int int64; 00266 #endif 00267 #ifndef HAVE_UINT64 00268 typedef unsigned long int uint64; 00269 #endif 00270 #elif defined(HAVE_LONG_LONG_INT_64) 00271 /* We have working support for "long long int", use that */ 00272 00273 #ifndef HAVE_INT64 00274 typedef long long int int64; 00275 #endif 00276 #ifndef HAVE_UINT64 00277 typedef unsigned long long int uint64; 00278 #endif 00279 #else /* not HAVE_LONG_INT_64 and not 00280 * HAVE_LONG_LONG_INT_64 */ 00281 00282 /* Won't actually work, but fall back to long int so that code compiles */ 00283 #ifndef HAVE_INT64 00284 typedef long int int64; 00285 #endif 00286 #ifndef HAVE_UINT64 00287 typedef unsigned long int uint64; 00288 #endif 00289 00290 #define INT64_IS_BUSTED 00291 #endif /* not HAVE_LONG_INT_64 and not 00292 * HAVE_LONG_LONG_INT_64 */ 00293 00294 /* Decide if we need to decorate 64-bit constants */ 00295 #ifdef HAVE_LL_CONSTANTS 00296 #define INT64CONST(x) ((int64) x##LL) 00297 #define UINT64CONST(x) ((uint64) x##ULL) 00298 #else 00299 #define INT64CONST(x) ((int64) x) 00300 #define UINT64CONST(x) ((uint64) x) 00301 #endif 00302 00303 00304 /* Select timestamp representation (float8 or int64) */ 00305 #if defined(USE_INTEGER_DATETIMES) && !defined(INT64_IS_BUSTED) 00306 #define HAVE_INT64_TIMESTAMP 00307 #endif 00308 00309 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */ 00310 #ifndef HAVE_SIG_ATOMIC_T 00311 typedef int sig_atomic_t; 00312 #endif 00313 00314 /* 00315 * Size 00316 * Size of any memory resident object, as returned by sizeof. 00317 */ 00318 typedef size_t Size; 00319 00320 /* 00321 * Index 00322 * Index into any memory resident array. 00323 * 00324 * Note: 00325 * Indices are non negative. 00326 */ 00327 typedef unsigned int Index; 00328 00329 /* 00330 * Offset 00331 * Offset into any memory resident array. 00332 * 00333 * Note: 00334 * This differs from an Index in that an Index is always 00335 * non negative, whereas Offset may be negative. 00336 */ 00337 typedef signed int Offset; 00338 00339 /* 00340 * Common Postgres datatype names (as used in the catalogs) 00341 */ 00342 typedef int16 int2; 00343 typedef int32 int4; 00344 typedef float float4; 00345 typedef double float8; 00346 00347 /* 00348 * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId, 00349 * CommandId 00350 */ 00351 00352 /* typedef Oid is in postgres_ext.h */ 00353 00354 /* 00355 * regproc is the type name used in the include/catalog headers, but 00356 * RegProcedure is the preferred name in C code. 00357 */ 00358 typedef Oid regproc; 00359 typedef regproc RegProcedure; 00360 00361 typedef uint32 TransactionId; 00362 00363 typedef uint32 LocalTransactionId; 00364 00365 typedef uint32 SubTransactionId; 00366 00367 #define InvalidSubTransactionId ((SubTransactionId) 0) 00368 #define TopSubTransactionId ((SubTransactionId) 1) 00369 00370 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */ 00371 typedef TransactionId MultiXactId; 00372 00373 typedef uint32 MultiXactOffset; 00374 00375 typedef uint32 CommandId; 00376 00377 #define FirstCommandId ((CommandId) 0) 00378 00379 /* 00380 * Array indexing support 00381 */ 00382 #define MAXDIM 6 00383 typedef struct 00384 { 00385 int indx[MAXDIM]; 00386 } IntArray; 00387 00388 /* ---------------- 00389 * Variable-length datatypes all share the 'struct varlena' header. 00390 * 00391 * NOTE: for TOASTable types, this is an oversimplification, since the value 00392 * may be compressed or moved out-of-line. However datatype-specific routines 00393 * are mostly content to deal with de-TOASTed values only, and of course 00394 * client-side routines should never see a TOASTed value. But even in a 00395 * de-TOASTed value, beware of touching vl_len_ directly, as its representation 00396 * is no longer convenient. It's recommended that code always use the VARDATA, 00397 * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of 00398 * the struct fields. See postgres.h for details of the TOASTed form. 00399 * ---------------- 00400 */ 00401 struct varlena 00402 { 00403 char vl_len_[4]; /* Do not touch this field directly! */ 00404 char vl_dat[1]; 00405 }; 00406 00407 #define VARHDRSZ ((int32) sizeof(int32)) 00408 00409 /* 00410 * These widely-used datatypes are just a varlena header and the data bytes. 00411 * There is no terminating null or anything like that --- the data length is 00412 * always VARSIZE(ptr) - VARHDRSZ. 00413 */ 00414 typedef struct varlena bytea; 00415 typedef struct varlena text; 00416 typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */ 00417 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */ 00418 00419 /* 00420 * Specialized array types. These are physically laid out just the same 00421 * as regular arrays (so that the regular array subscripting code works 00422 * with them). They exist as distinct types mostly for historical reasons: 00423 * they have nonstandard I/O behavior which we don't want to change for fear 00424 * of breaking applications that look at the system catalogs. There is also 00425 * an implementation issue for oidvector: it's part of the primary key for 00426 * pg_proc, and we can't use the normal btree array support routines for that 00427 * without circularity. 00428 */ 00429 typedef struct 00430 { 00431 int32 vl_len_; /* these fields must match ArrayType! */ 00432 int ndim; /* always 1 for int2vector */ 00433 int32 dataoffset; /* always 0 for int2vector */ 00434 Oid elemtype; 00435 int dim1; 00436 int lbound1; 00437 int2 values[1]; /* VARIABLE LENGTH ARRAY */ 00438 } int2vector; /* VARIABLE LENGTH STRUCT */ 00439 00440 typedef struct 00441 { 00442 int32 vl_len_; /* these fields must match ArrayType! */ 00443 int ndim; /* always 1 for oidvector */ 00444 int32 dataoffset; /* always 0 for oidvector */ 00445 Oid elemtype; 00446 int dim1; 00447 int lbound1; 00448 Oid values[1]; /* VARIABLE LENGTH ARRAY */ 00449 } oidvector; /* VARIABLE LENGTH STRUCT */ 00450 00451 /* 00452 * Representation of a Name: effectively just a C string, but null-padded to 00453 * exactly NAMEDATALEN bytes. The use of a struct is historical. 00454 */ 00455 typedef struct nameData 00456 { 00457 char data[NAMEDATALEN]; 00458 } NameData; 00459 typedef NameData *Name; 00460 00461 #define NameStr(name) ((name).data) 00462 00463 /* 00464 * Support macros for escaping strings. escape_backslash should be TRUE 00465 * if generating a non-standard-conforming string. Prefixing a string 00466 * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming. 00467 * Beware of multiple evaluation of the "ch" argument! 00468 */ 00469 #define SQL_STR_DOUBLE(ch, escape_backslash) \ 00470 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash))) 00471 00472 #define ESCAPE_STRING_SYNTAX 'E' 00473 00474 /* ---------------------------------------------------------------- 00475 * Section 4: IsValid macros for system types 00476 * ---------------------------------------------------------------- 00477 */ 00478 /* 00479 * BoolIsValid 00480 * True iff bool is valid. 00481 */ 00482 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true) 00483 00484 /* 00485 * PointerIsValid 00486 * True iff pointer is valid. 00487 */ 00488 #define PointerIsValid(pointer) ((void*)(pointer) != NULL) 00489 00490 /* 00491 * PointerIsAligned 00492 * True iff pointer is properly aligned to point to the given type. 00493 */ 00494 #define PointerIsAligned(pointer, type) \ 00495 (((long)(pointer) % (sizeof (type))) == 0) 00496 00497 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid)) 00498 00499 #define RegProcedureIsValid(p) OidIsValid(p) 00500 00501 00502 /* ---------------------------------------------------------------- 00503 * Section 5: offsetof, lengthof, endof, alignment 00504 * ---------------------------------------------------------------- 00505 */ 00506 /* 00507 * offsetof 00508 * Offset of a structure/union field within that structure/union. 00509 * 00510 * XXX This is supposed to be part of stddef.h, but isn't on 00511 * some systems (like SunOS 4). 00512 */ 00513 #ifndef offsetof 00514 #define offsetof(type, field) ((long) &((type *)0)->field) 00515 #endif /* offsetof */ 00516 00517 /* 00518 * lengthof 00519 * Number of elements in an array. 00520 */ 00521 #define lengthof(array) (sizeof (array) / sizeof ((array)[0])) 00522 00523 /* 00524 * endof 00525 * Address of the element one past the last in an array. 00526 */ 00527 #define endof(array) (&(array)[lengthof(array)]) 00528 00529 /* ---------------- 00530 * Alignment macros: align a length or address appropriately for a given type. 00531 * The fooALIGN() macros round up to a multiple of the required alignment, 00532 * while the fooALIGN_DOWN() macros round down. The latter are more useful 00533 * for problems like "how many X-sized structures will fit in a page?". 00534 * 00535 * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2. 00536 * That case seems extremely unlikely to be needed in practice, however. 00537 * ---------------- 00538 */ 00539 00540 #define TYPEALIGN(ALIGNVAL,LEN) \ 00541 (((long) (LEN) + ((ALIGNVAL) - 1)) & ~((long) ((ALIGNVAL) - 1))) 00542 00543 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN)) 00544 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN)) 00545 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN)) 00546 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN)) 00547 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN)) 00548 /* MAXALIGN covers only built-in types, not buffers */ 00549 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN)) 00550 00551 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \ 00552 (((long) (LEN)) & ~((long) ((ALIGNVAL) - 1))) 00553 00554 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN)) 00555 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN)) 00556 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN)) 00557 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN)) 00558 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN)) 00559 00560 /* ---------------------------------------------------------------- 00561 * Section 6: widely useful macros 00562 * ---------------------------------------------------------------- 00563 */ 00564 /* 00565 * Max 00566 * Return the maximum of two numbers. 00567 */ 00568 #define Max(x, y) ((x) > (y) ? (x) : (y)) 00569 00570 /* 00571 * Min 00572 * Return the minimum of two numbers. 00573 */ 00574 #define Min(x, y) ((x) < (y) ? (x) : (y)) 00575 00576 /* 00577 * Abs 00578 * Return the absolute value of the argument. 00579 */ 00580 #define Abs(x) ((x) >= 0 ? (x) : -(x)) 00581 00582 /* 00583 * StrNCpy 00584 * Like standard library function strncpy(), except that result string 00585 * is guaranteed to be null-terminated --- that is, at most N-1 bytes 00586 * of the source string will be kept. 00587 * Also, the macro returns no result (too hard to do that without 00588 * evaluating the arguments multiple times, which seems worse). 00589 * 00590 * BTW: when you need to copy a non-null-terminated string (like a text 00591 * datum) and add a null, do not do it with StrNCpy(..., len+1). That 00592 * might seem to work, but it fetches one byte more than there is in the 00593 * text object. One fine day you'll have a SIGSEGV because there isn't 00594 * another byte before the end of memory. Don't laugh, we've had real 00595 * live bug reports from real live users over exactly this mistake. 00596 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead. 00597 */ 00598 #define StrNCpy(dst,src,len) \ 00599 do \ 00600 { \ 00601 char * _dst = (dst); \ 00602 Size _len = (len); \ 00603 \ 00604 if (_len > 0) \ 00605 { \ 00606 strncpy(_dst, (src), _len); \ 00607 _dst[_len-1] = '\0'; \ 00608 } \ 00609 } while (0) 00610 00611 00612 /* Get a bit mask of the bits set in non-long aligned addresses */ 00613 #define LONG_ALIGN_MASK (sizeof(long) - 1) 00614 00615 /* 00616 * MemSet 00617 * Exactly the same as standard library function memset(), but considerably 00618 * faster for zeroing small word-aligned structures (such as parsetree nodes). 00619 * This has to be a macro because the main point is to avoid function-call 00620 * overhead. However, we have also found that the loop is faster than 00621 * native libc memset() on some platforms, even those with assembler 00622 * memset() functions. More research needs to be done, perhaps with 00623 * MEMSET_LOOP_LIMIT tests in configure. 00624 */ 00625 #define MemSet(start, val, len) \ 00626 do \ 00627 { \ 00628 /* must be void* because we don't know if it is integer aligned yet */ \ 00629 void *_vstart = (void *) (start); \ 00630 int _val = (val); \ 00631 Size _len = (len); \ 00632 \ 00633 if ((((long) _vstart) & LONG_ALIGN_MASK) == 0 && \ 00634 (_len & LONG_ALIGN_MASK) == 0 && \ 00635 _val == 0 && \ 00636 _len <= MEMSET_LOOP_LIMIT && \ 00637 /* \ 00638 * If MEMSET_LOOP_LIMIT == 0, optimizer should find \ 00639 * the whole "if" false at compile time. \ 00640 */ \ 00641 MEMSET_LOOP_LIMIT != 0) \ 00642 { \ 00643 long *_start = (long *) _vstart; \ 00644 long *_stop = (long *) ((char *) _start + _len); \ 00645 while (_start < _stop) \ 00646 *_start++ = 0; \ 00647 } \ 00648 else \ 00649 memset(_vstart, _val, _len); \ 00650 } while (0) 00651 00652 /* 00653 * MemSetAligned is the same as MemSet except it omits the test to see if 00654 * "start" is word-aligned. This is okay to use if the caller knows a-priori 00655 * that the pointer is suitably aligned (typically, because he just got it 00656 * from palloc(), which always delivers a max-aligned pointer). 00657 */ 00658 #define MemSetAligned(start, val, len) \ 00659 do \ 00660 { \ 00661 long *_start = (long *) (start); \ 00662 int _val = (val); \ 00663 Size _len = (len); \ 00664 \ 00665 if ((_len & LONG_ALIGN_MASK) == 0 && \ 00666 _val == 0 && \ 00667 _len <= MEMSET_LOOP_LIMIT && \ 00668 MEMSET_LOOP_LIMIT != 0) \ 00669 { \ 00670 long *_stop = (long *) ((char *) _start + _len); \ 00671 while (_start < _stop) \ 00672 *_start++ = 0; \ 00673 } \ 00674 else \ 00675 memset(_start, _val, _len); \ 00676 } while (0) 00677 00678 00679 /* 00680 * MemSetTest/MemSetLoop are a variant version that allow all the tests in 00681 * MemSet to be done at compile time in cases where "val" and "len" are 00682 * constants *and* we know the "start" pointer must be word-aligned. 00683 * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use 00684 * MemSetAligned. Beware of multiple evaluations of the arguments when using 00685 * this approach. 00686 */ 00687 #define MemSetTest(val, len) \ 00688 ( ((len) & LONG_ALIGN_MASK) == 0 && \ 00689 (len) <= MEMSET_LOOP_LIMIT && \ 00690 MEMSET_LOOP_LIMIT != 0 && \ 00691 (val) == 0 ) 00692 00693 #define MemSetLoop(start, val, len) \ 00694 do \ 00695 { \ 00696 long * _start = (long *) (start); \ 00697 long * _stop = (long *) ((char *) _start + (Size) (len)); \ 00698 \ 00699 while (_start < _stop) \ 00700 *_start++ = 0; \ 00701 } while (0) 00702 00703 00704 /* ---------------------------------------------------------------- 00705 * Section 7: random stuff 00706 * ---------------------------------------------------------------- 00707 */ 00708 00709 /* msb for char */ 00710 #define HIGHBIT (0x80) 00711 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT) 00712 00713 #define STATUS_OK (0) 00714 #define STATUS_ERROR (-1) 00715 #define STATUS_EOF (-2) 00716 #define STATUS_FOUND (1) 00717 #define STATUS_WAITING (2) 00718 00719 00720 /* gettext domain name mangling */ 00721 00722 /* 00723 * To better support parallel installations of major PostgeSQL 00724 * versions as well as parallel installations of major library soname 00725 * versions, we mangle the gettext domain name by appending those 00726 * version numbers. The coding rule ought to be that whereever the 00727 * domain name is mentioned as a literal, it must be wrapped into 00728 * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but 00729 * that is somewhat intentional because it avoids having to worry 00730 * about multiple states of premangling and postmangling as the values 00731 * are being passed around. 00732 * 00733 * Make sure this matches the installation rules in nls-global.mk. 00734 */ 00735 00736 /* need a second indirection because we want to stringize the macro value, not the name */ 00737 #define CppAsString2(x) CppAsString(x) 00738 00739 #ifdef SO_MAJOR_VERSION 00740 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION) 00741 #else 00742 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION) 00743 #endif 00744 00745 00746 /* ---------------------------------------------------------------- 00747 * Section 8: system-specific hacks 00748 * 00749 * This should be limited to things that absolutely have to be 00750 * included in every source file. The port-specific header file 00751 * is usually a better place for this sort of thing. 00752 * ---------------------------------------------------------------- 00753 */ 00754 00755 /* 00756 * NOTE: this is also used for opening text files. 00757 * WIN32 treats Control-Z as EOF in files opened in text mode. 00758 * Therefore, we open files in binary mode on Win32 so we can read 00759 * literal control-Z. The other affect is that we see CRLF, but 00760 * that is OK because we can already handle those cleanly. 00761 */ 00762 #if defined(WIN32) || defined(__CYGWIN__) 00763 #define PG_BINARY O_BINARY 00764 #define PG_BINARY_A "ab" 00765 #define PG_BINARY_R "rb" 00766 #define PG_BINARY_W "wb" 00767 #else 00768 #define PG_BINARY 0 00769 #define PG_BINARY_A "a" 00770 #define PG_BINARY_R "r" 00771 #define PG_BINARY_W "w" 00772 #endif 00773 00774 /* 00775 * Provide prototypes for routines not present in a particular machine's 00776 * standard C library. 00777 */ 00778 00779 #if !HAVE_DECL_SNPRINTF 00780 extern int 00781 snprintf(char *str, size_t count, const char *fmt,...) 00782 /* This extension allows gcc to check the format string */ 00783 __attribute__((format(printf, 3, 4))); 00784 #endif 00785 00786 #if !HAVE_DECL_VSNPRINTF 00787 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args); 00788 #endif 00789 00790 #if !defined(HAVE_MEMMOVE) && !defined(memmove) 00791 #define memmove(d, s, c) bcopy(s, d, c) 00792 #endif 00793 00794 #ifndef PGDLLIMPORT 00795 #define PGDLLIMPORT /* no special DLL markers on most ports */ 00796 #endif 00797 00798 /* 00799 * The following is used as the arg list for signal handlers. Any ports 00800 * that take something other than an int argument should override this in 00801 * their pg_config_os.h file. Note that variable names are required 00802 * because it is used in both the prototypes as well as the definitions. 00803 * Note also the long name. We expect that this won't collide with 00804 * other names causing compiler warnings. 00805 */ 00806 00807 #ifndef SIGNAL_ARGS 00808 #define SIGNAL_ARGS int postgres_signal_arg 00809 #endif 00810 00811 /* 00812 * When there is no sigsetjmp, its functionality is provided by plain 00813 * setjmp. Incidentally, nothing provides setjmp's functionality in 00814 * that case. 00815 */ 00816 #ifndef HAVE_SIGSETJMP 00817 #define sigjmp_buf jmp_buf 00818 #define sigsetjmp(x,y) setjmp(x) 00819 #define siglongjmp longjmp 00820 #endif 00821 00822 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC 00823 extern int fdatasync(int fildes); 00824 #endif 00825 00826 /* If strtoq() exists, rename it to the more standard strtoll() */ 00827 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ) 00828 #define strtoll strtoq 00829 #define HAVE_STRTOLL 1 00830 #endif 00831 00832 /* If strtouq() exists, rename it to the more standard strtoull() */ 00833 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ) 00834 #define strtoull strtouq 00835 #define HAVE_STRTOULL 1 00836 #endif 00837 00838 /* 00839 * We assume if we have these two functions, we have their friends too, and 00840 * can use the wide-character functions. 00841 */ 00842 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER) 00843 #define USE_WIDE_UPPER_LOWER 00844 #endif 00845 00846 /* EXEC_BACKEND defines */ 00847 #ifdef EXEC_BACKEND 00848 #define NON_EXEC_STATIC 00849 #else 00850 #define NON_EXEC_STATIC static 00851 #endif 00852 00853 /* /port compatibility functions */ 00854 #include "port.h" 00855 00856 #endif /* C_H */
1.5.8