9 #if defined(HAVE_CONFIG_H)
27 #include <sys/resource.h>
29 #include <sys/types.h>
32 #include <boost/filesystem/path.hpp>
33 #include <boost/thread.hpp>
38 static const int64_t
COIN = 100000000;
39 static const int64_t
CENT = 1000000;
41 #define BEGIN(a) ((char*)&(a))
42 #define END(a) ((char*)&((&(a))[1]))
43 #define UBEGIN(a) ((unsigned char*)&(a))
44 #define UEND(a) ((unsigned char*)&((&(a))[1]))
45 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
48 #define PAIRTYPE(t1, t2) std::pair<t1, t2>
51 template <
size_t nBytes,
typename T>
60 u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
65 #define MSG_DONTWAIT 0
75 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
76 #define MSG_NOSIGNAL 0
84 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
85 boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
86 #elif defined(HAVE_WORKING_BOOST_SLEEP)
87 boost::this_thread::sleep(boost::posix_time::milliseconds(n));
90 #error missing boost sleep implementation
96 extern std::map<std::string, std::string>
mapArgs;
97 extern std::map<std::string, std::vector<std::string> >
mapMultiArgs;
116 #define strprintf tfm::format
117 #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
122 #define MAKE_ERROR_AND_LOG_FUNC(n) \
124 template<TINYFORMAT_ARGTYPES(n)> \
125 static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
127 if(!LogAcceptCategory(category)) return 0; \
128 return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
131 template<TINYFORMAT_ARGTYPES(n)> \
132 static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
134 LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
150 LogPrintStr(std::string(
"ERROR: ") + format +
"\n");
155 void LogException(std::exception* pex,
const char* pszThread);
157 std::string
FormatMoney(int64_t n,
bool fPlus=
false);
158 bool ParseMoney(
const std::string& str, int64_t& nRet);
159 bool ParseMoney(
const char* pszIn, int64_t& nRet);
161 std::vector<unsigned char>
ParseHex(
const char* psz);
162 std::vector<unsigned char>
ParseHex(
const std::string& str);
163 bool IsHex(
const std::string& str);
164 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid = NULL);
166 std::string
EncodeBase64(
const unsigned char* pch,
size_t len);
168 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid = NULL);
170 std::string
EncodeBase32(
const unsigned char* pch,
size_t len);
174 bool WildcardMatch(
const std::string& str,
const std::string& mask);
179 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
182 const boost::filesystem::path &
GetDataDir(
bool fNetSpecific =
true);
186 void CreatePidFile(
const boost::filesystem::path &path, pid_t pid);
188 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
190 boost::filesystem::path GetSpecialFolderPath(
int nFolder,
bool fCreate =
true);
195 uint64_t
GetRand(uint64_t nMax);
202 std::string
FormatSubVersion(
const std::string& name,
int nClientVersion,
const std::vector<std::string>& comments);
229 return strtoll(psz, NULL, 10);
233 inline int64_t
atoi64(
const std::string& str)
236 return _atoi64(str.c_str());
238 return strtoll(str.c_str(), NULL, 10);
242 inline int atoi(
const std::string& str)
244 return atoi(str.c_str());
249 return (
int)(d > 0 ? d + 0.5 : d - 0.5);
254 return (int64_t)(d > 0 ? d + 0.5 : d - 0.5);
259 return (n >= 0 ? n : -n);
263 std::string
HexStr(
const T itbegin,
const T itend,
bool fSpaces=
false)
266 static const char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
267 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
268 rv.reserve((itend-itbegin)*3);
269 for(T it = itbegin; it < itend; ++it)
271 unsigned char val = (
unsigned char)(*it);
272 if(fSpaces && it != itbegin)
274 rv.push_back(hexmap[val>>4]);
275 rv.push_back(hexmap[val&15]);
282 inline std::string
HexStr(
const T& vch,
bool fSpaces=
false)
284 return HexStr(vch.begin(), vch.end(), fSpaces);
288 void PrintHex(
const T pbegin,
const T pend,
const char* pszFormat=
"%s",
bool fSpaces=
true)
293 inline void PrintHex(
const std::vector<unsigned char>& vch,
const char* pszFormat=
"%s",
bool fSpaces=
true)
300 int64_t nCounter = 0;
302 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
305 gettimeofday(&t, NULL);
306 nCounter = (int64_t) t.tv_sec * 1000000 + t.tv_usec;
313 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
314 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
319 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
320 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
335 return c ==
'-' || c ==
'/';
348 std::string
GetArg(
const std::string& strArg,
const std::string& strDefault);
357 int64_t
GetArg(
const std::string& strArg, int64_t nDefault);
366 bool GetBoolArg(
const std::string& strArg,
bool fDefault);
375 bool SoftSetArg(
const std::string& strArg,
const std::string& strValue);
413 template <
typename T>
416 if (b.size() == 0)
return a.size() == 0;
417 size_t accumulator = a.size() ^ b.size();
418 for (
size_t i = 0; i < a.size(); i++)
419 accumulator |= a[i] ^ b[i%b.size()];
420 return accumulator == 0;
436 vValues.reserve(size);
437 vValues.push_back(initial_value);
443 if(vValues.size() ==
nSize)
445 vValues.erase(vValues.begin());
447 vValues.push_back(value);
449 vSorted.resize(vValues.size());
450 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
451 std::sort(vSorted.begin(), vSorted.end());
456 int size = vSorted.size();
460 return vSorted[size/2];
464 return (vSorted[size/2-1] + vSorted[size/2]) / 2;
470 return vValues.size();
490 #define THREAD_PRIORITY_LOWEST PRIO_MAX
491 #define THREAD_PRIORITY_BELOW_NORMAL 2
492 #define THREAD_PRIORITY_NORMAL 0
493 #define THREAD_PRIORITY_ABOVE_NORMAL (-2)
500 setpriority(PRIO_THREAD, 0, nPriority);
502 setpriority(PRIO_PROCESS, 0, nPriority);
511 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
512 return (value<<16) | (value>>16);
522 template <
typename Callable>
void LoopForever(
const char* name, Callable func, int64_t msecs)
524 std::string s =
strprintf(
"bitcoin-%s", name);
535 catch (boost::thread_interrupted)
540 catch (std::exception& e) {
550 template <
typename Callable>
void TraceThread(
const char* name, Callable func)
552 std::string s =
strprintf(
"bitcoin-%s", name);
560 catch (boost::thread_interrupted)
562 LogPrintf(
"%s thread interrupt\n", name);
565 catch (std::exception& e) {
std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
uint64_t GetRand(uint64_t nMax)
static uint32_t insecure_rand(void)
void PrintHex(const T pbegin, const T pend, const char *pszFormat="%s", bool fSpaces=true)
void MilliSleep(int64_t n)
void LogException(std::exception *pex, const char *pszThread)
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid=NULL)
void SetMockTime(int64_t nMockTimeIn)
int64_t GetAdjustedTime()
void RandAddSeedPerfmon()
std::string i64tostr(int64_t n)
void RenameThread(const char *name)
void ReadConfigFile(std::map< std::string, std::string > &mapSettingsRet, std::map< std::string, std::vector< std::string > > &mapMultiSettingsRet)
std::string SanitizeString(const std::string &str)
std::map< std::string, std::vector< std::string > > mapMultiArgs
std::string FormatFullVersion()
void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
std::map< std::string, std::string > mapArgs
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
std::vector< unsigned char > ParseHex(const char *psz)
const boost::filesystem::path & GetDataDir(bool fNetSpecific=true)
static int LogPrint(const char *category, const char *format)
std::string itostr(int n)
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
int64_t atoi64(const char *psz)
void TraceThread(const char *name, Callable func)
bool ParseMoney(const std::string &str, int64_t &nRet)
static bool error(const char *format)
void ParseParameters(int argc, const char *const argv[])
bool IsHex(const std::string &str)
std::string EncodeBase64(const unsigned char *pch, size_t len)
#define MAKE_ERROR_AND_LOG_FUNC(n)
void SetThreadPriority(int nPriority)
void PrintExceptionContinue(std::exception *pex, const char *pszThread)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid=NULL)
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
std::string EncodeBase32(const unsigned char *pch, size_t len)
int64_t GetPerformanceCounter()
static const int64_t COIN
std::string strMiscWarning
bool IsSwitchChar(char c)
uint32_t insecure_rand_Rz
MWC RNG of George Marsaglia This is intended to be fast.
std::string FormatMoney(int64_t n, bool fPlus=false)
bool TryCreateDirectory(const boost::filesystem::path &p)
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
uint32_t ByteReverse(uint32_t value)
void FileCommit(FILE *fileout)
boost::filesystem::path GetConfigFile()
void seed_insecure_rand(bool fDeterministic=false)
Seed insecure_rand using the random pool.
boost::filesystem::path GetTempPath()
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
bool WildcardMatch(const char *psz, const char *mask)
bool LogAcceptCategory(const char *category)
static const int64_t CENT
int RaiseFileDescriptorLimit(int nMinFD)
int64_t roundint64(double d)
boost::filesystem::path GetPidFile()
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
bool TruncateFile(FILE *file, unsigned int length)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
int LogPrintStr(const std::string &str)
volatile bool fReopenDebugLog
void LoopForever(const char *name, Callable func, int64_t msecs)
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
boost::filesystem::path GetDefaultDataDir()
uint32_t insecure_rand_Rw
int atoi(const std::string &str)
void runCommand(std::string strCommand)
void AddTimeData(const CNetAddr &ip, int64_t nTime)