2014-04-20 20:14:48 -06:00
|
|
|
/* Copyright (c) 2008-2014, Avian Contributors
|
2008-02-19 11:06:52 -07:00
|
|
|
|
|
|
|
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. */
|
|
|
|
|
2007-08-27 07:46:17 -06:00
|
|
|
#ifndef JNI_UTIL
|
|
|
|
#define JNI_UTIL
|
|
|
|
|
2008-11-11 08:20:49 -07:00
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
2009-08-26 18:26:44 -06:00
|
|
|
#include "string.h"
|
2008-11-11 08:20:49 -07:00
|
|
|
|
2013-02-19 22:56:05 -07:00
|
|
|
#include <avian/util/runtime-array.h>
|
2013-02-10 17:51:59 -07:00
|
|
|
|
2007-10-26 18:20:37 -06:00
|
|
|
#undef JNIEXPORT
|
2009-10-27 09:34:46 -06:00
|
|
|
|
2009-08-26 18:26:44 -06:00
|
|
|
#if (defined __MINGW32__) || (defined _MSC_VER)
|
|
|
|
# define PLATFORM_WINDOWS
|
|
|
|
# define PATH_SEPARATOR ';'
|
2009-10-27 10:35:26 -06:00
|
|
|
# define JNIEXPORT __declspec(dllexport)
|
2009-10-27 09:34:46 -06:00
|
|
|
#else // not (defined __MINGW32__) || (defined _MSC_VER)
|
2009-08-26 18:26:44 -06:00
|
|
|
# define PLATFORM_POSIX
|
|
|
|
# define PATH_SEPARATOR ':'
|
2011-09-22 16:55:45 -06:00
|
|
|
# define JNIEXPORT __attribute__ ((visibility("default"))) \
|
|
|
|
__attribute__ ((used))
|
2009-10-27 09:34:46 -06:00
|
|
|
#endif // not (defined __MINGW32__) || (defined _MSC_VER)
|
2007-10-26 18:20:37 -06:00
|
|
|
|
2009-08-26 18:26:44 -06:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
|
|
|
# define UNUSED
|
|
|
|
|
|
|
|
typedef char int8_t;
|
|
|
|
typedef unsigned char uint8_t;
|
|
|
|
typedef short int16_t;
|
|
|
|
typedef unsigned short uint16_t;
|
|
|
|
typedef int int32_t;
|
|
|
|
typedef unsigned int uint32_t;
|
|
|
|
typedef __int64 int64_t;
|
|
|
|
typedef unsigned __int64 uint64_t;
|
|
|
|
|
|
|
|
# define INT32_MAX 2147483647
|
|
|
|
|
|
|
|
# define not !
|
|
|
|
# define or ||
|
|
|
|
# define and &&
|
|
|
|
# define xor ^
|
|
|
|
|
|
|
|
# ifdef _M_IX86
|
|
|
|
# define ARCH_x86_32
|
|
|
|
# elif defined _M_X64
|
|
|
|
# define ARCH_x86_64
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#else // not _MSC_VER
|
|
|
|
|
|
|
|
# define UNUSED __attribute__((unused))
|
|
|
|
|
|
|
|
# include "stdint.h"
|
|
|
|
# include "errno.h"
|
|
|
|
|
|
|
|
# ifdef __i386__
|
|
|
|
# define ARCH_x86_32
|
|
|
|
# elif defined __x86_64__
|
|
|
|
# define ARCH_x86_64
|
|
|
|
# elif defined __arm__
|
|
|
|
# define ARCH_arm
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif // not _MSC_VER
|
2008-06-16 11:45:23 -06:00
|
|
|
|
2007-08-27 07:46:17 -06:00
|
|
|
inline void
|
2008-11-11 08:20:49 -07:00
|
|
|
throwNew(JNIEnv* e, const char* class_, const char* message, ...)
|
2007-08-27 07:46:17 -06:00
|
|
|
{
|
|
|
|
jclass c = e->FindClass(class_);
|
|
|
|
if (c) {
|
2008-11-11 08:20:49 -07:00
|
|
|
if (message) {
|
|
|
|
static const unsigned BufferSize = 256;
|
|
|
|
char buffer[BufferSize];
|
|
|
|
|
|
|
|
va_list list;
|
|
|
|
va_start(list, message);
|
2009-08-26 18:26:44 -06:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
vsnprintf_s(buffer, BufferSize - 1, _TRUNCATE, message, list);
|
|
|
|
#else
|
2008-11-11 08:20:49 -07:00
|
|
|
vsnprintf(buffer, BufferSize - 1, message, list);
|
2009-08-26 18:26:44 -06:00
|
|
|
#endif
|
2008-11-11 08:20:49 -07:00
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
e->ThrowNew(c, buffer);
|
|
|
|
} else {
|
|
|
|
e->ThrowNew(c, 0);
|
|
|
|
}
|
2007-08-27 07:46:17 -06:00
|
|
|
e->DeleteLocalRef(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 18:26:44 -06:00
|
|
|
inline void
|
|
|
|
throwNewErrno(JNIEnv* e, const char* class_)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
const unsigned size = 128;
|
|
|
|
char buffer[size];
|
|
|
|
strerror_s(buffer, size, errno);
|
|
|
|
throwNew(e, class_, buffer);
|
|
|
|
#else
|
|
|
|
throwNew(e, class_, strerror(errno));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-11-11 08:20:49 -07:00
|
|
|
inline void*
|
|
|
|
allocate(JNIEnv* e, unsigned size)
|
|
|
|
{
|
|
|
|
void* p = malloc(size);
|
|
|
|
if (p == 0) {
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2009-08-26 18:26:44 -06:00
|
|
|
|
2007-08-27 07:46:17 -06:00
|
|
|
#endif//JNI_UTIL
|