genesis 1
genesis 2
genesis 3
genesis 4
genesis 5 #ifndef BITCOIN_UTIL_H
genesis 6 #define BITCOIN_UTIL_H
genesis 7
genesis 8 #include "uint256.h"
genesis 9
asciilifeform-kil... 10 #include <stdint.h>
genesis 11 #include <sys/types.h>
genesis 12 #include <sys/time.h>
genesis 13 #include <sys/resource.h>
genesis 14 #include <map>
genesis 15 #include <vector>
genesis 16 #include <string>
genesis 17
genesis 18 #include <boost/thread.hpp>
genesis 19 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
genesis 20 #include <boost/date_time/gregorian/gregorian_types.hpp>
genesis 21 #include <boost/date_time/posix_time/posix_time_types.hpp>
genesis 22
genesis 23 #include <openssl/sha.h>
genesis 24 #include <openssl/ripemd.h>
genesis 25
genesis 26
genesis 27 typedef long long int64;
genesis 28 typedef unsigned long long uint64;
bitcoin-asciilife... 29
genesis 30 #define __forceinline inline
genesis 31
genesis 32 #define loop for (;;)
genesis 33 #define BEGIN(a) ((char*)&(a))
genesis 34 #define END(a) ((char*)&((&(a))[1]))
genesis 35 #define UBEGIN(a) ((unsigned char*)&(a))
genesis 36 #define UEND(a) ((unsigned char*)&((&(a))[1]))
genesis 37 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
genesis 38 #define printf OutputDebugStringF
genesis 39
genesis 40 #ifdef snprintf
genesis 41 #undef snprintf
genesis 42 #endif
genesis 43 #define snprintf my_snprintf
genesis 44
genesis 45 #ifndef PRI64d
genesis 46 #define PRI64d "lld"
genesis 47 #define PRI64u "llu"
genesis 48 #define PRI64x "llx"
genesis 49 #endif
genesis 50
genesis 51
genesis 52 #define PAIRTYPE(t1, t2) std::pair<t1, t2>
genesis 53
genesis 54
genesis 55 template <size_t nBytes, typename T>
genesis 56 T* alignup(T* p)
genesis 57 {
genesis 58 union
genesis 59 {
genesis 60 T* ptr;
genesis 61 size_t n;
genesis 62 } u;
genesis 63 u.ptr = p;
genesis 64 u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
genesis 65 return u.ptr;
genesis 66 }
genesis 67
genesis 68 #define WSAGetLastError() errno
genesis 69 #define WSAEINVAL EINVAL
genesis 70 #define WSAEALREADY EALREADY
genesis 71 #define WSAEWOULDBLOCK EWOULDBLOCK
genesis 72 #define WSAEMSGSIZE EMSGSIZE
genesis 73 #define WSAEINTR EINTR
genesis 74 #define WSAEINPROGRESS EINPROGRESS
genesis 75 #define WSAEADDRINUSE EADDRINUSE
genesis 76 #define WSAENOTSOCK EBADF
genesis 77 #define INVALID_SOCKET (SOCKET)(~0)
genesis 78 #define SOCKET_ERROR -1
genesis 79 typedef u_int SOCKET;
genesis 80 #define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d)
genesis 81 #define strlwr(psz) to_lower(psz)
genesis 82 #define _strlwr(psz) to_lower(psz)
genesis 83 #define MAX_PATH 1024
genesis 84 #define Beep(n1,n2) (0)
genesis 85 inline void Sleep(int64 n)
genesis 86 {
genesis 87
genesis 88 So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/
genesis 89 boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n));
genesis 90 }
bitcoin-asciilife... 91
genesis 92
genesis 93 inline int myclosesocket(SOCKET& hSocket)
genesis 94 {
genesis 95 if (hSocket == INVALID_SOCKET)
genesis 96 return WSAENOTSOCK;
genesis 97 int ret = close(hSocket);
genesis 98 hSocket = INVALID_SOCKET;
genesis 99 return ret;
genesis 100 }
genesis 101 #define closesocket(s) myclosesocket(s)
genesis 102 inline const char* _(const char* psz)
genesis 103 {
genesis 104 return psz;
genesis 105 }
bitcoin-asciilife... 106
genesis 107
genesis 108 extern std::map<std::string, std::string> mapArgs;
genesis 109 extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
genesis 110 extern bool fDebug;
genesis 111 extern bool fPrintToConsole;
genesis 112 extern bool fPrintToDebugger;
asciilifeform_and... 113 extern bool fCanEat;
asciilifeform_add... 114 extern bool fVerifyAll;
genesis 115 extern char pszSetDataDir[MAX_PATH];
genesis 116 extern bool fRequestShutdown;
genesis 117 extern bool fShutdown;
genesis 118 extern bool fDaemon;
genesis 119 extern bool fServer;
asciilifeform_wir... 120 extern bool fWires;
asciilifeform_wir... 121 extern int wireRefresh;
genesis 122 extern bool fCommandLine;
genesis 123 extern std::string strMiscWarning;
genesis 124 extern bool fNoListen;
genesis 125 extern bool fLogTimestamps;
programmable-vers... 126 extern std::string CLIENT_NAME;
mod6_der_high_low_s 127 extern bool fLowS;
mod6_der_high_low_s 128 extern bool fHighS;
genesis 129
genesis 130 void RandAddSeed();
genesis 131 void RandAddSeedPerfmon();
genesis 132 int OutputDebugStringF(const char* pszFormat, ...);
genesis 133 int my_snprintf(char* buffer, size_t limit, const char* format, ...);
genesis 134 std::string strprintf(const std::string &format, ...);
genesis 135 bool error(const std::string &format, ...);
genesis 136 void LogException(std::exception* pex, const char* pszThread);
genesis 137 void PrintException(std::exception* pex, const char* pszThread);
genesis 138 void PrintExceptionContinue(std::exception* pex, const char* pszThread);
genesis 139 void ParseString(const std::string& str, char c, std::vector<std::string>& v);
genesis 140 std::string FormatMoney(int64 n, bool fPlus=false);
genesis 141 bool ParseMoney(const std::string& str, int64& nRet);
genesis 142 bool ParseMoney(const char* pszIn, int64& nRet);
genesis 143 std::vector<unsigned char> ParseHex(const char* psz);
genesis 144 std::vector<unsigned char> ParseHex(const std::string& str);
genesis 145 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
genesis 146 std::string DecodeBase64(const std::string& str);
genesis 147 std::string EncodeBase64(const unsigned char* pch, size_t len);
genesis 148 std::string EncodeBase64(const std::string& str);
genesis 149 void ParseParameters(int argc, char* argv[]);
genesis 150 const char* wxGetTranslation(const char* psz);
genesis 151 bool WildcardMatch(const char* psz, const char* mask);
genesis 152 bool WildcardMatch(const std::string& str, const std::string& mask);
genesis 153 int GetFilesize(FILE* file);
genesis 154 void GetDataDir(char* pszDirRet);
genesis 155 std::string GetConfigFile();
genesis 156 std::string GetPidFile();
genesis 157 void CreatePidFile(std::string pidFile, pid_t pid);
genesis 158 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
genesis 159 std::string GetDefaultDataDir();
genesis 160 std::string GetDataDir();
genesis 161 void ShrinkDebugFile();
genesis 162 int GetRandInt(int nMax);
genesis 163 uint64 GetRand(uint64 nMax);
genesis 164 int64 GetTime();
genesis 165 void SetMockTime(int64 nMockTimeIn);
genesis 166 int64 GetAdjustedTime();
genesis 167 void AddTimeData(unsigned int ip, int64 nTime);
genesis 168 std::string FormatFullVersion();
programmable-vers... 169 std::string FormatSubVersion(const std::string& name, int nClientVersion);
genesis 170
genesis 171
genesis 172
genesis 173
genesis 174
genesis 175
genesis 176
genesis 177
genesis 178
genesis 179
genesis 180
genesis 181
genesis 182
genesis 183
genesis 184 class CCriticalSection
genesis 185 {
genesis 186 protected:
genesis 187 boost::interprocess::interprocess_recursive_mutex mutex;
genesis 188 public:
genesis 189 explicit CCriticalSection() { }
genesis 190 ~CCriticalSection() { }
genesis 191 void Enter(const char* pszName, const char* pszFile, int nLine);
genesis 192 void Leave();
genesis 193 bool TryEnter(const char* pszName, const char* pszFile, int nLine);
genesis 194 };
genesis 195
genesis 196
genesis 197 class CCriticalBlock
genesis 198 {
genesis 199 protected:
genesis 200 CCriticalSection* pcs;
genesis 201
genesis 202 public:
genesis 203 CCriticalBlock(CCriticalSection& csIn, const char* pszName, const char* pszFile, int nLine)
genesis 204 {
genesis 205 pcs = &csIn;
genesis 206 pcs->Enter(pszName, pszFile, nLine);
genesis 207 }
genesis 208
genesis 209 operator bool() const
genesis 210 {
genesis 211 return true;
genesis 212 }
genesis 213
genesis 214 ~CCriticalBlock()
genesis 215 {
genesis 216 pcs->Leave();
genesis 217 }
genesis 218 };
genesis 219
genesis 220 #define CRITICAL_BLOCK(cs) \
genesis 221 if (CCriticalBlock criticalblock = CCriticalBlock(cs, #cs, __FILE__, __LINE__))
genesis 222
genesis 223 #define ENTER_CRITICAL_SECTION(cs) \
genesis 224 (cs).Enter(#cs, __FILE__, __LINE__)
genesis 225
genesis 226 #define LEAVE_CRITICAL_SECTION(cs) \
genesis 227 (cs).Leave()
genesis 228
genesis 229 class CTryCriticalBlock
genesis 230 {
genesis 231 protected:
genesis 232 CCriticalSection* pcs;
genesis 233
genesis 234 public:
genesis 235 CTryCriticalBlock(CCriticalSection& csIn, const char* pszName, const char* pszFile, int nLine)
genesis 236 {
genesis 237 pcs = (csIn.TryEnter(pszName, pszFile, nLine) ? &csIn : NULL);
genesis 238 }
genesis 239
genesis 240 operator bool() const
genesis 241 {
genesis 242 return Entered();
genesis 243 }
genesis 244
genesis 245 ~CTryCriticalBlock()
genesis 246 {
genesis 247 if (pcs)
genesis 248 {
genesis 249 pcs->Leave();
genesis 250 }
genesis 251 }
genesis 252 bool Entered() const { return pcs != NULL; }
genesis 253 };
genesis 254
genesis 255 #define TRY_CRITICAL_BLOCK(cs) \
genesis 256 if (CTryCriticalBlock criticalblock = CTryCriticalBlock(cs, #cs, __FILE__, __LINE__))
genesis 257
genesis 258
genesis 259
genesis 260
genesis 261
genesis 262
genesis 263
genesis 264
genesis 265 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
genesis 266
genesis 267
genesis 268
genesis 269 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
genesis 270
genesis 271
genesis 272
genesis 273
genesis 274
genesis 275 inline std::string i64tostr(int64 n)
genesis 276 {
genesis 277 return strprintf("%"PRI64d, n);
genesis 278 }
genesis 279
genesis 280 inline std::string itostr(int n)
genesis 281 {
genesis 282 return strprintf("%d", n);
genesis 283 }
genesis 284
genesis 285 inline int64 atoi64(const char* psz)
genesis 286 {
genesis 287 return strtoll(psz, NULL, 10);
genesis 288 }
genesis 289
genesis 290 inline int64 atoi64(const std::string& str)
genesis 291 {
genesis 292 return strtoll(str.c_str(), NULL, 10);
genesis 293 }
genesis 294
genesis 295 inline int atoi(const std::string& str)
genesis 296 {
genesis 297 return atoi(str.c_str());
genesis 298 }
genesis 299
genesis 300 inline int roundint(double d)
genesis 301 {
genesis 302 return (int)(d > 0 ? d + 0.5 : d - 0.5);
genesis 303 }
genesis 304
genesis 305 inline int64 roundint64(double d)
genesis 306 {
genesis 307 return (int64)(d > 0 ? d + 0.5 : d - 0.5);
genesis 308 }
genesis 309
genesis 310 inline int64 abs64(int64 n)
genesis 311 {
genesis 312 return (n >= 0 ? n : -n);
genesis 313 }
genesis 314
genesis 315 template<typename T>
genesis 316 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
genesis 317 {
genesis 318 if (itbegin == itend)
genesis 319 return "";
genesis 320 const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
genesis 321 const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
genesis 322 std::string str;
genesis 323 str.reserve((pend-pbegin) * (fSpaces ? 3 : 2));
genesis 324 for (const unsigned char* p = pbegin; p != pend; p++)
genesis 325 str += strprintf((fSpaces && p != pend-1 ? "%02x " : "%02x"), *p);
genesis 326 return str;
genesis 327 }
genesis 328
genesis 329 inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=false)
genesis 330 {
genesis 331 return HexStr(vch.begin(), vch.end(), fSpaces);
genesis 332 }
genesis 333
genesis 334 template<typename T>
genesis 335 std::string HexNumStr(const T itbegin, const T itend, bool f0x=true)
genesis 336 {
genesis 337 if (itbegin == itend)
genesis 338 return "";
genesis 339 const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
genesis 340 const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
genesis 341 std::string str = (f0x ? "0x" : "");
genesis 342 str.reserve(str.size() + (pend-pbegin) * 2);
genesis 343 for (const unsigned char* p = pend-1; p >= pbegin; p--)
genesis 344 str += strprintf("%02x", *p);
genesis 345 return str;
genesis 346 }
genesis 347
genesis 348 inline std::string HexNumStr(const std::vector<unsigned char>& vch, bool f0x=true)
genesis 349 {
genesis 350 return HexNumStr(vch.begin(), vch.end(), f0x);
genesis 351 }
genesis 352
genesis 353 template<typename T>
genesis 354 void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true)
genesis 355 {
genesis 356 printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
genesis 357 }
genesis 358
genesis 359 inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
genesis 360 {
genesis 361 printf(pszFormat, HexStr(vch, fSpaces).c_str());
genesis 362 }
genesis 363
genesis 364 inline int64 GetPerformanceCounter()
genesis 365 {
genesis 366 int64 nCounter = 0;
genesis 367 timeval t;
genesis 368 gettimeofday(&t, NULL);
genesis 369 nCounter = t.tv_sec * 1000000 + t.tv_usec;
genesis 370 return nCounter;
genesis 371 }
genesis 372
genesis 373 inline int64 GetTimeMillis()
genesis 374 {
genesis 375 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
genesis 376 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
genesis 377 }
genesis 378
genesis 379 inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime)
genesis 380 {
genesis 381 time_t n = nTime;
genesis 382 struct tm* ptmTime = gmtime(&n);
genesis 383 char pszTime[200];
genesis 384 strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
genesis 385 return pszTime;
genesis 386 }
genesis 387
genesis 388 template<typename T>
genesis 389 void skipspaces(T& it)
genesis 390 {
genesis 391 while (isspace(*it))
genesis 392 ++it;
genesis 393 }
genesis 394
genesis 395 inline bool IsSwitchChar(char c)
genesis 396 {
genesis 397 return c == '-';
genesis 398 }
genesis 399
genesis 400 inline std::string GetArg(const std::string& strArg, const std::string& strDefault)
genesis 401 {
genesis 402 if (mapArgs.count(strArg))
genesis 403 return mapArgs[strArg];
genesis 404 return strDefault;
genesis 405 }
genesis 406
genesis 407 inline int64 GetArg(const std::string& strArg, int64 nDefault)
genesis 408 {
genesis 409 if (mapArgs.count(strArg))
genesis 410 return atoi64(mapArgs[strArg]);
genesis 411 return nDefault;
genesis 412 }
genesis 413
genesis 414 inline bool GetBoolArg(const std::string& strArg, bool fDefault=false)
genesis 415 {
genesis 416 if (mapArgs.count(strArg))
genesis 417 {
genesis 418 if (mapArgs[strArg].empty())
genesis 419 return true;
genesis 420 return (atoi(mapArgs[strArg]) != 0);
genesis 421 }
genesis 422 return fDefault;
genesis 423 }
genesis 424
genesis 425
genesis 426 * Set an argument if it doesn't already have a value
genesis 427 *
genesis 428 * @param strArg Argument to set (e.g. "-foo")
genesis 429 * @param strValue Value (e.g. "1")
genesis 430 * @return true if argument gets set, false if it already had a value
genesis 431 */
genesis 432 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
genesis 433
genesis 434
genesis 435 * Set a boolean argument if it doesn't already have a value
genesis 436 *
genesis 437 * @param strArg Argument to set (e.g. "-foo")
genesis 438 * @param fValue Value (e.g. false)
genesis 439 * @return true if argument gets set, false if it already had a value
genesis 440 */
genesis 441 bool SoftSetArg(const std::string& strArg, bool fValue);
genesis 442
genesis 443
genesis 444
genesis 445
genesis 446
genesis 447
genesis 448
genesis 449
genesis 450
genesis 451 inline void heapchk()
genesis 452 {
genesis 453 }
genesis 454
genesis 455
genesis 456 #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn) \
genesis 457 { \
genesis 458 static char nLoops; \
genesis 459 if (nLoops <= 0) \
genesis 460 nLoops = GetRand(20) + 1; \
genesis 461 if (nLoops-- > 1) \
genesis 462 { \
genesis 463 ThreadFn; \
genesis 464 return; \
genesis 465 } \
genesis 466 }
genesis 467
genesis 468 #define CATCH_PRINT_EXCEPTION(pszFn) \
genesis 469 catch (std::exception& e) { \
genesis 470 PrintException(&e, (pszFn)); \
genesis 471 } catch (...) { \
genesis 472 PrintException(NULL, (pszFn)); \
genesis 473 }
genesis 474
genesis 475
genesis 476
genesis 477
genesis 478
genesis 479
genesis 480
genesis 481
genesis 482
genesis 483
genesis 484 template<typename T1>
genesis 485 inline uint256 Hash(const T1 pbegin, const T1 pend)
genesis 486 {
genesis 487 static unsigned char pblank[1];
genesis 488 uint256 hash1;
genesis 489 SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
genesis 490 uint256 hash2;
genesis 491 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 492 return hash2;
genesis 493 }
genesis 494
genesis 495 template<typename T1, typename T2>
genesis 496 inline uint256 Hash(const T1 p1begin, const T1 p1end,
genesis 497 const T2 p2begin, const T2 p2end)
genesis 498 {
genesis 499 static unsigned char pblank[1];
genesis 500 uint256 hash1;
genesis 501 SHA256_CTX ctx;
genesis 502 SHA256_Init(&ctx);
genesis 503 SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
genesis 504 SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
genesis 505 SHA256_Final((unsigned char*)&hash1, &ctx);
genesis 506 uint256 hash2;
genesis 507 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 508 return hash2;
genesis 509 }
genesis 510
genesis 511 template<typename T1, typename T2, typename T3>
genesis 512 inline uint256 Hash(const T1 p1begin, const T1 p1end,
genesis 513 const T2 p2begin, const T2 p2end,
genesis 514 const T3 p3begin, const T3 p3end)
genesis 515 {
genesis 516 static unsigned char pblank[1];
genesis 517 uint256 hash1;
genesis 518 SHA256_CTX ctx;
genesis 519 SHA256_Init(&ctx);
genesis 520 SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
genesis 521 SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
genesis 522 SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
genesis 523 SHA256_Final((unsigned char*)&hash1, &ctx);
genesis 524 uint256 hash2;
genesis 525 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 526 return hash2;
genesis 527 }
genesis 528
genesis 529 template<typename T>
genesis 530 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=VERSION)
genesis 531 {
genesis 532
genesis 533
genesis 534
genesis 535 CDataStream ss(nType, nVersion);
genesis 536 ss.reserve(10000);
genesis 537 ss << obj;
genesis 538 return Hash(ss.begin(), ss.end());
genesis 539 }
genesis 540
genesis 541 inline uint160 Hash160(const std::vector<unsigned char>& vch)
genesis 542 {
genesis 543 uint256 hash1;
genesis 544 SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
genesis 545 uint160 hash2;
genesis 546 RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 547 return hash2;
genesis 548 }
genesis 549
genesis 550
genesis 551
genesis 552
genesis 553 template <typename T> class CMedianFilter
genesis 554 {
genesis 555 private:
genesis 556 std::vector<T> vValues;
genesis 557 std::vector<T> vSorted;
genesis 558 int nSize;
genesis 559 public:
genesis 560 CMedianFilter(int size, T initial_value):
genesis 561 nSize(size)
genesis 562 {
genesis 563 vValues.reserve(size);
genesis 564 vValues.push_back(initial_value);
genesis 565 vSorted = vValues;
genesis 566 }
genesis 567
genesis 568 void input(T value)
genesis 569 {
genesis 570 if(vValues.size() == nSize)
genesis 571 {
genesis 572 vValues.erase(vValues.begin());
genesis 573 }
genesis 574 vValues.push_back(value);
genesis 575
genesis 576 vSorted.resize(vValues.size());
genesis 577 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
genesis 578 std::sort(vSorted.begin(), vSorted.end());
genesis 579 }
genesis 580
genesis 581 T median() const
genesis 582 {
genesis 583 int size = vSorted.size();
genesis 584 assert(size>0);
genesis 585 if(size & 1)
genesis 586 {
genesis 587 return vSorted[size/2];
genesis 588 }
genesis 589 else
genesis 590 {
genesis 591 return (vSorted[size/2-1] + vSorted[size/2]) / 2;
genesis 592 }
genesis 593 }
genesis 594 };
genesis 595
genesis 596
genesis 597
genesis 598
genesis 599
genesis 600
genesis 601
genesis 602
genesis 603
genesis 604
genesis 605
genesis 606
genesis 607 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
genesis 608 {
genesis 609 pthread_t hthread = 0;
genesis 610 int ret = pthread_create(&hthread, NULL, (void*(*)(void*))pfn, parg);
genesis 611 if (ret != 0)
genesis 612 {
genesis 613 printf("Error: pthread_create() returned %d\n", ret);
genesis 614 return (pthread_t)0;
genesis 615 }
genesis 616 if (!fWantHandle)
genesis 617 {
genesis 618 pthread_detach(hthread);
genesis 619 return (pthread_t)-1;
genesis 620 }
genesis 621 return hthread;
genesis 622 }
genesis 623
genesis 624 #define THREAD_PRIORITY_LOWEST PRIO_MAX
genesis 625 #define THREAD_PRIORITY_BELOW_NORMAL 2
genesis 626 #define THREAD_PRIORITY_NORMAL 0
genesis 627 #define THREAD_PRIORITY_ABOVE_NORMAL 0
genesis 628
genesis 629 inline void SetThreadPriority(int nPriority)
genesis 630 {
genesis 631
genesis 632
genesis 633 #ifdef PRIO_THREAD
genesis 634 setpriority(PRIO_THREAD, 0, nPriority);
genesis 635 #else
genesis 636 setpriority(PRIO_PROCESS, 0, nPriority);
genesis 637 #endif
genesis 638 }
genesis 639
genesis 640 inline bool TerminateThread(pthread_t hthread, unsigned int nExitCode)
genesis 641 {
genesis 642 return (pthread_cancel(hthread) == 0);
genesis 643 }
genesis 644
genesis 645 inline void ExitThread(size_t nExitCode)
genesis 646 {
genesis 647 pthread_exit((void*)nExitCode);
genesis 648 }
genesis 649
genesis 650
genesis 651
genesis 652 inline bool AffinityBugWorkaround(void(*pfn)(void*))
genesis 653 {
genesis 654 return false;
genesis 655 }
genesis 656
genesis 657 inline uint32_t ByteReverse(uint32_t value)
genesis 658 {
genesis 659 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
genesis 660 return (value<<16) | (value>>16);
genesis 661 }
genesis 662
genesis 663 #endif