mirror of
https://github.com/corda/corda.git
synced 2025-06-19 07:38:22 +00:00
snapshot
This commit is contained in:
77
src/common.h
77
src/common.h
@ -15,6 +15,8 @@
|
||||
#define MACRO_MakeNameXY(FX, LINE) MACRO_XY(FX, LINE)
|
||||
#define MAKE_NAME(FX) MACRO_MakeNameXY(FX, __LINE__)
|
||||
|
||||
inline void* operator new(size_t, void* p) throw() { return p; }
|
||||
|
||||
namespace vm {
|
||||
|
||||
typedef void* object;
|
||||
@ -24,6 +26,81 @@ const unsigned BitsPerWord = BytesPerWord * 8;
|
||||
|
||||
const unsigned LikelyPageSize = 4 * 1024;
|
||||
|
||||
inline unsigned
|
||||
max(unsigned a, unsigned b)
|
||||
{
|
||||
return (a > b ? a : b);
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
min(unsigned a, unsigned b)
|
||||
{
|
||||
return (a < b ? a : b);
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
pad(unsigned n)
|
||||
{
|
||||
unsigned extra = n % BytesPerWord;
|
||||
return (extra ? n + BytesPerWord - extra : n);
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
divide(unsigned n, unsigned d)
|
||||
{
|
||||
if (n and d > n) return 1;
|
||||
return (n / d) + (n % d ? 1 : 0);
|
||||
}
|
||||
|
||||
inline bool
|
||||
powerOfTwo(unsigned n)
|
||||
{
|
||||
for (; n > 2; n >>= 1) if (n & 1) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
nextPowerOfTwo(unsigned n)
|
||||
{
|
||||
unsigned r = 1;
|
||||
while (r < n) r <<= 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
log(unsigned n)
|
||||
{
|
||||
if (n < 3) return 1;
|
||||
unsigned r = 0;
|
||||
for (unsigned i = 1; i < n; ++r) i <<= 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
wordOf(unsigned i)
|
||||
{
|
||||
return i / BitsPerWord;
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
bitOf(unsigned i)
|
||||
{
|
||||
return i % BitsPerWord;
|
||||
}
|
||||
|
||||
inline unsigned
|
||||
indexOf(unsigned word, unsigned bit)
|
||||
{
|
||||
return (word * BitsPerWord) + bit;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T&
|
||||
cast(object p, unsigned offset)
|
||||
{
|
||||
return *reinterpret_cast<T*>(static_cast<uint8_t*>(p) + offset);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif//COMMON_H
|
||||
|
Reference in New Issue
Block a user