2007-06-03 01:56:57 +00:00
|
|
|
#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"
|
2007-06-07 00:30:16 +00:00
|
|
|
#include "stdarg.h"
|
2007-09-25 23:53:11 +00:00
|
|
|
#include "stddef.h"
|
2007-06-07 00:30:16 +00:00
|
|
|
#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
|
|
|
|
2007-11-01 20:00:22 +00:00
|
|
|
#undef JNIEXPORT
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
# define JNIEXPORT __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
# define JNIEXPORT __attribute__ ((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2007-07-06 01:06:06 +00:00
|
|
|
#ifdef __i386__
|
2007-08-19 19:45:51 +00:00
|
|
|
# define LD "d"
|
2007-09-19 16:26:08 +00:00
|
|
|
#ifdef __APPLE__
|
2007-09-19 16:22:19 +00:00
|
|
|
# define ULD "lu"
|
2007-09-19 16:26:08 +00:00
|
|
|
#else
|
|
|
|
# define ULD "u"
|
|
|
|
#endif
|
2007-08-19 19:45:51 +00:00
|
|
|
# define LLD "lld"
|
2007-07-06 01:06:06 +00:00
|
|
|
#elif defined __x86_64__
|
2007-08-19 19:45:51 +00:00
|
|
|
# define LD "ld"
|
2007-09-19 16:22:19 +00:00
|
|
|
# define ULD "lu"
|
2007-08-19 19:45:51 +00:00
|
|
|
# define LLD "ld"
|
2007-09-19 16:22:19 +00:00
|
|
|
#else
|
2007-09-21 14:16:43 +00:00
|
|
|
# error "Unsupported architecture"
|
2007-07-06 01:06:06 +00:00
|
|
|
#endif
|
|
|
|
|
2007-10-24 16:24:02 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
# define SO_PREFIX ""
|
|
|
|
#else
|
|
|
|
# define SO_PREFIX "lib"
|
|
|
|
#endif
|
|
|
|
|
2007-09-20 16:13:41 +00:00
|
|
|
#ifdef __APPLE__
|
2007-09-21 14:16:43 +00:00
|
|
|
# define SO_SUFFIX ".jnilib"
|
2007-10-24 16:24:02 +00:00
|
|
|
#elif defined __MINGW32__
|
2007-10-23 01:00:57 +00:00
|
|
|
# define SO_SUFFIX ".dll"
|
2007-09-20 16:13:41 +00:00
|
|
|
#else
|
2007-09-21 14:16:43 +00:00
|
|
|
# define SO_SUFFIX ".so"
|
2007-09-20 16:13:41 +00:00
|
|
|
#endif
|
|
|
|
|
2007-06-03 01:56:57 +00:00
|
|
|
#define NO_RETURN __attribute__((noreturn))
|
2007-06-20 21:27:22 +00:00
|
|
|
|
2007-06-08 00:23:12 +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-03 01:56:57 +00:00
|
|
|
|
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__)
|
|
|
|
|
2007-08-19 19:45:51 +00:00
|
|
|
#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;
|
|
|
|
|
2007-07-01 21:34:22 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-06-22 23:29:15 +00:00
|
|
|
inline unsigned
|
|
|
|
avg(unsigned a, unsigned b)
|
|
|
|
{
|
|
|
|
return (a + b) / 2;
|
|
|
|
}
|
|
|
|
|
2007-06-20 17:42:13 +00:00
|
|
|
inline unsigned
|
|
|
|
pad(unsigned n)
|
|
|
|
{
|
2007-09-20 00:11:19 +00:00
|
|
|
return (n + (BytesPerWord - 1)) & ~(BytesPerWord - 1);
|
2007-06-20 17:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned
|
2007-07-26 00:48:28 +00:00
|
|
|
ceiling(unsigned n, unsigned d)
|
2007-06-20 17:42:13 +00:00
|
|
|
{
|
2007-07-26 00:48:28 +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;
|
|
|
|
}
|
|
|
|
|
2007-10-12 02:52:16 +00:00
|
|
|
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);
|
2007-10-12 02:52:16 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 17:42:13 +00:00
|
|
|
template <class T>
|
|
|
|
inline T&
|
2007-09-07 00:21:52 +00:00
|
|
|
cast(void* p, unsigned offset)
|
2007-06-20 17:42:13 +00:00
|
|
|
{
|
|
|
|
return *reinterpret_cast<T*>(static_cast<uint8_t*>(p) + offset);
|
|
|
|
}
|
|
|
|
|
2007-07-01 21:34:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-10-15 19:12:38 +00:00
|
|
|
inline uint32_t
|
|
|
|
floatToBits(float f)
|
|
|
|
{
|
2007-10-25 20:26:51 +00:00
|
|
|
uint32_t bits; memcpy(&bits, &f, 4);
|
2007-10-15 19:12:38 +00:00
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64_t
|
|
|
|
doubleToBits(double d)
|
|
|
|
{
|
2007-10-25 20:26:51 +00:00
|
|
|
uint64_t bits; memcpy(&bits, &d, 8);
|
2007-10-15 19:12:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-11-25 23:00:55 +00:00
|
|
|
inline intptr_t
|
|
|
|
difference(void* a, void* b)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<intptr_t>(a) - reinterpret_cast<intptr_t>(b);
|
|
|
|
}
|
|
|
|
|
2007-09-24 01:39:03 +00:00
|
|
|
class Machine;
|
|
|
|
class Thread;
|
|
|
|
|
|
|
|
struct Object { };
|
|
|
|
|
|
|
|
typedef Object* object;
|
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
} // namespace vm
|
2007-06-20 04:26:36 +00:00
|
|
|
|
2007-06-03 01:56:57 +00:00
|
|
|
#endif//COMMON_H
|