00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #ifndef Fennel_CommonPreamble_Included
00025 #define Fennel_CommonPreamble_Included
00026 
00027 
00028 #include <config.h>
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 #ifndef __MSVC__
00038 #define __STDC_LIMIT_MACROS
00039 #else
00040 #define NOMINMAX
00041 #pragma warning (disable : 4355)
00042 #endif
00043 
00044 #define _XOPEN_SOURCE 500
00045 #define _GNU_SOURCE 1
00046 
00047 
00048 
00049 #define _FILE_OFFSET_BITS 64
00050 
00051 #ifndef __MSVC__
00052 #include <inttypes.h>
00053 #endif
00054 
00055 #include <stddef.h>
00056 #include <limits.h>
00057 
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060 #include <stdexcept>
00061 #include <iostream>
00062 #include <sstream>
00063 #include <string>
00064 #include <string.h>
00065 #include <time.h>
00066 #include <new>
00067 #include <cassert>
00068 #include <vector>
00069 #include <boost/thread/tss.hpp>
00070 
00071 #ifndef __MSVC__
00072 #if __WORDSIZE == 64
00073 #define FMT_INT64      "ld"
00074 #define FMT_UINT64     "lu"
00075 #else
00076 #define FMT_INT64      "lld"
00077 #define FMT_UINT64     "llu"
00078 #endif
00079 #else
00080 
00081 
00082 #define FMT_INT64      "I64d"
00083 #define FMT_UINT64     "I64u"
00084 #define strtoll _strtoi64
00085 #define strtoull _strtoui64
00086 #define strncasecmp strnicmp
00087 #define isnan _isnan
00088 
00089 #define round(x) (((x) >= 0) ? floor((x) + 0.5) : ceil((x) - 0.5))
00090 #define roundf round
00091 
00092 #endif
00093 
00094 typedef unsigned uint;
00095 
00096 
00097 
00098 #ifdef __MSVC__
00099 
00100 #if defined(FENNEL_COMMON_EXPORTS) || defined(FENNEL_SYNCH_EXPORTS)
00101 #define FENNEL_COMMON_EXPORT __declspec(dllexport)
00102 #define FENNEL_SYNCH_EXPORT __declspec(dllexport)
00103 #else
00104 #define FENNEL_COMMON_EXPORT __declspec(dllimport)
00105 #define FENNEL_SYNCH_EXPORT __declspec(dllimport)
00106 #endif
00107 
00108 #ifdef FENNEL_DEVICE_EXPORTS
00109 #define FENNEL_DEVICE_EXPORT __declspec(dllexport)
00110 #else
00111 #define FENNEL_DEVICE_EXPORT __declspec(dllimport)
00112 #endif
00113 
00114 #ifdef FENNEL_CACHE_EXPORTS
00115 #define FENNEL_CACHE_EXPORT __declspec(dllexport)
00116 #else
00117 #define FENNEL_CACHE_EXPORT __declspec(dllimport)
00118 #endif
00119 
00120 #ifdef FENNEL_SEGMENT_EXPORTS
00121 #define FENNEL_SEGMENT_EXPORT __declspec(dllexport)
00122 #else
00123 #define FENNEL_SEGMENT_EXPORT __declspec(dllimport)
00124 #endif
00125 
00126 #ifdef FENNEL_TXN_EXPORTS
00127 #define FENNEL_TXN_EXPORT __declspec(dllexport)
00128 #else
00129 #define FENNEL_TXN_EXPORT __declspec(dllimport)
00130 #endif
00131 
00132 #ifdef FENNEL_TUPLE_EXPORTS
00133 #define FENNEL_TUPLE_EXPORT __declspec(dllexport)
00134 #else
00135 #define FENNEL_TUPLE_EXPORT __declspec(dllimport)
00136 #endif
00137 
00138 #ifdef FENNEL_EXEC_EXPORTS
00139 #define FENNEL_EXEC_EXPORT __declspec(dllexport)
00140 #else
00141 #define FENNEL_EXEC_EXPORT __declspec(dllimport)
00142 #endif
00143 
00144 #ifdef FENNEL_BTREE_EXPORTS
00145 #define FENNEL_BTREE_EXPORT __declspec(dllexport)
00146 #else
00147 #define FENNEL_BTREE_EXPORT __declspec(dllimport)
00148 #endif
00149 
00150 #ifdef FENNEL_DB_EXPORTS
00151 #define FENNEL_DB_EXPORT __declspec(dllexport)
00152 #else
00153 #define FENNEL_DB_EXPORT __declspec(dllimport)
00154 #endif
00155 
00156 #ifdef FENNEL_FTRS_EXPORTS
00157 #define FENNEL_FTRS_EXPORT __declspec(dllexport)
00158 #else
00159 #define FENNEL_FTRS_EXPORT __declspec(dllimport)
00160 #endif
00161 
00162 #ifdef FENNEL_SORTER_EXPORTS
00163 #define FENNEL_SORTER_EXPORT __declspec(dllexport)
00164 #else
00165 #define FENNEL_SORTER_EXPORT __declspec(dllimport)
00166 #endif
00167 
00168 #ifdef FENNEL_FLATFILE_EXPORTS
00169 #define FENNEL_FLATFILE_EXPORT __declspec(dllexport)
00170 #else
00171 #define FENNEL_FLATFILE_EXPORT __declspec(dllimport)
00172 #endif
00173 
00174 #ifdef FENNEL_HASHEXE_EXPORTS
00175 #define FENNEL_HASHEXE_EXPORT __declspec(dllexport)
00176 #else
00177 #define FENNEL_HASHEXE_EXPORT __declspec(dllimport)
00178 #endif
00179 
00180 #ifdef FENNEL_CALCULATOR_EXPORTS
00181 #define FENNEL_CALCULATOR_EXPORT __declspec(dllexport)
00182 #else
00183 #define FENNEL_CALCULATOR_EXPORT __declspec(dllimport)
00184 #endif
00185 
00186 #ifdef FENNEL_FARRAGO_EXPORTS
00187 #define FENNEL_FARRAGO_EXPORT __declspec(dllexport)
00188 #else
00189 #define FENNEL_FARRAGO_EXPORT __declspec(dllimport)
00190 #endif
00191 
00192 #ifdef FENNEL_TEST_EXPORTS
00193 #define FENNEL_TEST_EXPORT __declspec(dllexport)
00194 #else
00195 #define FENNEL_TEST_EXPORT __declspec(dllimport)
00196 #endif
00197 
00198 #if defined(FENNEL_LCS_EXPORTS) || defined(FENNEL_LBM_EXPORTS)
00199 #define FENNEL_LCS_EXPORT __declspec(dllexport)
00200 #define FENNEL_LBM_EXPORT __declspec(dllexport)
00201 #else
00202 #define FENNEL_LCS_EXPORT __declspec(dllimport)
00203 #define FENNEL_LBM_EXPORT __declspec(dllimport)
00204 
00205 #endif
00206 
00207 #else
00208 #define FENNEL_COMMON_EXPORT
00209 #define FENNEL_SYNCH_EXPORT
00210 #define FENNEL_DEVICE_EXPORT
00211 #define FENNEL_CACHE_EXPORT
00212 #define FENNEL_SEGMENT_EXPORT
00213 #define FENNEL_TXN_EXPORT
00214 #define FENNEL_TUPLE_EXPORT
00215 #define FENNEL_EXEC_EXPORT
00216 #define FENNEL_BTREE_EXPORT
00217 #define FENNEL_DB_EXPORT
00218 #define FENNEL_FTRS_EXPORT
00219 #define FENNEL_SORTER_EXPORT
00220 #define FENNEL_FLATFILE_EXPORT
00221 #define FENNEL_HASHEXE_EXPORT
00222 #define FENNEL_CALCULATOR_EXPORT
00223 #define FENNEL_FARRAGO_EXPORT
00224 #define FENNEL_TEST_EXPORT
00225 #define FENNEL_LCS_EXPORT
00226 #define FENNEL_LBM_EXPORT
00227 #endif
00228 
00229 #include "fennel/common/Namespace.h"
00230 
00231 
00232 
00233 
00234 
00235 #define FENNEL_BEGIN_CPPFILE(x) \
00236 FENNEL_BEGIN_NAMESPACE
00237 
00238 #define FENNEL_END_CPPFILE(x) \
00239 FENNEL_END_NAMESPACE
00240 
00241 #include "fennel/common/OpaqueInteger.h"
00242 #include "fennel/common/Types.h"
00243 
00244 
00245 namespace std
00246 {
00247 
00248 template<class T,class Dummy>
00249 struct hash
00250 < fennel::OpaqueInteger<T,Dummy> >
00251 {
00252     size_t operator() (const fennel::OpaqueInteger<T,Dummy> &key) const
00253     {
00254         return hash<T>()(fennel::opaqueToInt(key));
00255     }
00256 };
00257 
00258 } 
00259 
00260 
00261 
00262 
00263 inline void *operator new(size_t,fennel::PBuffer pBuffer)
00264 {
00265     return pBuffer;
00266 }
00267 
00268 inline void *operator new[](size_t,fennel::PBuffer pBuffer)
00269 {
00270     return pBuffer;
00271 }
00272 
00273 FENNEL_BEGIN_NAMESPACE
00274 
00275 class FENNEL_COMMON_EXPORT VectorOfUint : public std::vector<uint>
00276 {
00277 };
00278 
00283 template <class T>
00284 inline void deleteAndNullify(T *&p)
00285 {
00286     if (p) {
00287         delete p;
00288         p = NULL;
00289     }
00290 }
00291 
00296 template <class T>
00297 inline void deleteAndNullifyArray(T *&p)
00298 {
00299     if (p) {
00300         delete [] p;
00301         p = NULL;
00302     }
00303 }
00304 
00305 
00306 
00307 
00315 #if (__WORDSIZE == 64) || defined(sun)
00316 #define ARCH_ALIGN_BYTES 8
00317 #else
00318 #define ARCH_ALIGN_BYTES 4
00319 #endif
00320 
00324 #define ARCH_ALIGN_MASK (ARCH_ALIGN_BYTES - 1)
00325 
00329 template<class T>
00330 inline T alignRoundDown(T t)
00331 {
00332     return t & ~ARCH_ALIGN_MASK;
00333 }
00334 
00338 template<class T>
00339 inline T *alignRoundPtrDown(T *t)
00340 {
00341     return (T *) alignRoundDown(uint(t));
00342 }
00343 
00347 template<class T>
00348 inline T alignRoundUp(T t)
00349 {
00350     if (t & ARCH_ALIGN_MASK) {
00351         return alignRoundDown(t) + ARCH_ALIGN_BYTES;
00352     } else {
00353         return t;
00354     }
00355 }
00356 
00360 template<class T>
00361 inline T *alignRoundPtrUp(T *t)
00362 {
00363     return (T *) alignRoundUp(uint(t));
00364 }
00365 
00366 
00367 inline uint bytesForBits(uint cBits)
00368 {
00369     return (cBits >> 3) + ((cBits & 7) ? 1 : 0);
00370 }
00371 
00372 
00373 
00374 
00375 
00376 
00377 
00378 extern void FENNEL_COMMON_EXPORT hexDump(
00379     std::ostream &,void const *,uint cb,
00380     uint cbOffsetInitial = 0);
00381 
00382 template <class Numeric>
00383 inline Numeric sqr(Numeric n)
00384 {
00385     return n*n;
00386 }
00387 
00388 
00389 extern int FENNEL_COMMON_EXPORT getCurrentThreadId();
00390 
00391 extern std::logic_error FENNEL_COMMON_EXPORT constructAssertion(
00392     char const *pFilename,int lineNum,char const *condExpr);
00393 
00394 
00395 
00396 
00397 
00398 #ifdef NDEBUG
00399 
00400 #define permAssert(cond) \
00401 do { \
00402     if (!(cond)) { \
00403         throw fennel::constructAssertion(__FILE__,__LINE__,#cond); \
00404     } \
00405 } while (0)
00406 
00407 #else
00408 
00409 #define permAssert(cond) assert(cond)
00410 
00411 #endif
00412 
00413 
00414 
00415 
00416 
00417 
00418 
00419 
00420 
00421 #ifdef NDEBUG
00422 
00423 #define permFail(msg) \
00424 do { \
00425     std::ostringstream oss; \
00426     oss << msg; \
00427     throw fennel::constructAssertion(__FILE__,__LINE__, oss.str().c_str()); \
00428 } while (0)
00429 
00430 #else
00431 
00432 #define permFail(msg) \
00433 do { \
00434     std::cout << "Internal error: " << msg << std::endl; \
00435     assert(false); \
00436     throw 0; \
00437 } while (0)
00438 
00439 #endif
00440 
00441 
00442 
00443 #define ntohll(x) (((uint64_t) ntohl((uint32_t) (x)) << 32)\
00444                     | ntohl((uint32_t) (x >> 32)))
00445 
00446 #define htonll(x) ntohll(x)
00447 
00448 
00449 
00450 #if defined(HAVE_LIBAIO_H) && defined(HAVE_LIBAIO)
00451 #define USE_LIBAIO_H
00452 #elif HAVE_AIO_H
00453 #define USE_AIO_H
00454 #endif
00455 
00456 FENNEL_END_NAMESPACE
00457 
00458 #endif
00459 
00460