corda/src/common.h

330 lines
5.7 KiB
C
Raw Normal View History

/* Copyright (c) 2008, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#ifndef COMMON_H
#define COMMON_H
2007-06-05 00:28:52 +00:00
#include "stdint.h"
2007-06-18 04:25:42 +00:00
#include "stdlib.h"
#include "stdarg.h"
2007-09-25 23:53:11 +00:00
#include "stddef.h"
#include "string.h"
#include "stdio.h"
2007-06-29 02:58:48 +00:00
#include "types.h"
2007-08-13 14:06:31 +00:00
#include "math.h"
2007-06-05 00:28:52 +00:00
#undef JNIEXPORT
#ifdef __MINGW32__
# define JNIEXPORT __declspec(dllexport)
# define PATH_SEPARATOR ';'
#else
# define JNIEXPORT __attribute__ ((visibility("default")))
# define PATH_SEPARATOR ':'
#endif
2009-08-06 17:52:36 +00:00
#if (defined __i386__) || (defined __POWERPC__) || (defined __arm__)
# define LD "ld"
# if (defined __MINGW32__) && __GNUC__ == 4
2009-08-07 22:28:47 +00:00
# define LLD "I64d"
# else
# define LLD "lld"
# endif
# ifdef __APPLE__
# define ULD "lu"
# define LX "lx"
# else
# define LX "x"
# define ULD "u"
# endif
#elif defined __x86_64__
# define LD "ld"
# define LX "lx"
# ifdef __MINGW32__
# define LLD "I64d"
# define ULD "I64x"
# else
# define LLD "ld"
# define ULD "lu"
# endif
#else
2007-09-21 14:16:43 +00:00
# error "Unsupported architecture"
#endif
#ifdef __MINGW32__
# define SO_PREFIX ""
#else
# define SO_PREFIX "lib"
#endif
#ifdef __APPLE__
2007-09-21 14:16:43 +00:00
# define SO_SUFFIX ".jnilib"
#elif defined __MINGW32__
2007-10-23 01:00:57 +00:00
# define SO_SUFFIX ".dll"
#else
2007-09-21 14:16:43 +00:00
# define SO_SUFFIX ".so"
#endif
#define NO_RETURN __attribute__((noreturn))
2007-06-20 21:27:22 +00:00
#define LIKELY(v) __builtin_expect((v) != 0, true)
2007-06-20 21:27:22 +00:00
#define UNLIKELY(v) __builtin_expect((v) != 0, false)
2007-06-05 00:28:52 +00:00
#define MACRO_XY(X, Y) X##Y
#define MACRO_MakeNameXY(FX, LINE) MACRO_XY(FX, LINE)
#define MAKE_NAME(FX) MACRO_MakeNameXY(FX, __LINE__)
#define UNUSED __attribute__((unused))
2007-06-20 17:42:13 +00:00
inline void* operator new(size_t, void* p) throw() { return p; }
2007-06-20 04:26:36 +00:00
namespace vm {
const unsigned BytesPerWord = sizeof(uintptr_t);
const unsigned BitsPerWord = BytesPerWord * 8;
const uintptr_t PointerMask
= ((~static_cast<uintptr_t>(0)) / BytesPerWord) * BytesPerWord;
2007-06-22 20:55:11 +00:00
const unsigned LikelyPageSizeInBytes = 4 * 1024;
2007-06-20 04:26:36 +00:00
2007-06-20 17:42:13 +00:00
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
avg(unsigned a, unsigned b)
{
return (a + b) / 2;
}
inline unsigned
pad(unsigned n, unsigned alignment)
{
return (n + (alignment - 1)) & ~(alignment - 1);
}
2007-06-20 17:42:13 +00:00
inline unsigned
pad(unsigned n)
{
return pad(n, BytesPerWord);
2007-06-20 17:42:13 +00:00
}
inline unsigned
ceiling(unsigned n, unsigned d)
2007-06-20 17:42:13 +00:00
{
return (n + d - 1) / d;
2007-06-20 17:42:13 +00:00
}
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)
{
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;
}
inline void
markBit(uintptr_t* map, unsigned i)
{
map[wordOf(i)] |= static_cast<uintptr_t>(1) << bitOf(i);
}
inline void
clearBit(uintptr_t* map, unsigned i)
{
map[wordOf(i)] &= ~(static_cast<uintptr_t>(1) << bitOf(i));
}
inline unsigned
getBit(uintptr_t* map, unsigned i)
{
2007-10-12 14:26:36 +00:00
return (map[wordOf(i)] & (static_cast<uintptr_t>(1) << bitOf(i)))
>> bitOf(i);
}
inline void
clearBits(uintptr_t* map, unsigned bitsPerRecord, unsigned index)
{
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
clearBit(map, i);
}
}
inline void
setBits(uintptr_t* map, unsigned bitsPerRecord, int index, unsigned v)
{
for (int i = index + bitsPerRecord - 1; i >= index; --i) {
if (v & 1) markBit(map, i); else clearBit(map, i);
v >>= 1;
}
}
inline unsigned
getBits(uintptr_t* map, unsigned bitsPerRecord, unsigned index)
{
unsigned v = 0;
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
v <<= 1;
v |= getBit(map, i);
}
return v;
}
2007-06-20 17:42:13 +00:00
template <class T>
inline T&
cast(void* p, unsigned offset)
2007-06-20 17:42:13 +00:00
{
return *reinterpret_cast<T*>(static_cast<uint8_t*>(p) + offset);
}
template <class T>
inline T*
mask(T* p)
{
return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p) & PointerMask);
}
2007-09-17 00:13:36 +00:00
inline uint32_t
hash(const char* s)
{
uint32_t h = 0;
for (unsigned i = 0; s[i]; ++i) {
h = (h * 31) + s[i];
}
return h;
}
inline uint32_t
hash(const uint8_t* s, unsigned length)
{
uint32_t h = 0;
for (unsigned i = 0; i < length; ++i) {
h = (h * 31) + s[i];
}
return h;
}
inline uint32_t
hash(const int8_t* s, unsigned length)
{
return hash(reinterpret_cast<const uint8_t*>(s), length);
}
inline uint32_t
hash(const uint16_t* s, unsigned length)
{
uint32_t h = 0;
for (unsigned i = 0; i < length; ++i) {
h = (h * 31) + s[i];
}
return h;
}
inline uint32_t
floatToBits(float f)
{
uint32_t bits; memcpy(&bits, &f, 4);
return bits;
}
inline uint64_t
doubleToBits(double d)
{
uint64_t bits; memcpy(&bits, &d, 8);
return bits;
}
inline double
bitsToDouble(uint64_t bits)
{
double d; memcpy(&d, &bits, 8);
return d;
}
inline float
bitsToFloat(uint32_t bits)
{
float f; memcpy(&f, &bits, 4);
return f;
}
inline int
difference(void* a, void* b)
{
return reinterpret_cast<intptr_t>(a) - reinterpret_cast<intptr_t>(b);
}
2008-11-23 23:58:01 +00:00
template <class T>
inline void*
voidPointer(T function)
{
void* p;
memcpy(&p, &function, sizeof(void*));
return p;
}
inline void
replace(char a, char b, char* c)
{
for (; *c; ++c) if (*c == a) *c = b;
}
class Machine;
class Thread;
struct Object { };
typedef Object* object;
} // namespace vm
2007-06-20 04:26:36 +00:00
#endif//COMMON_H