2008-02-19 18:06:52 +00:00
|
|
|
/* 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. */
|
|
|
|
|
2007-08-27 13:46:17 +00:00
|
|
|
#ifndef JNI_UTIL
|
|
|
|
#define JNI_UTIL
|
|
|
|
|
2008-11-11 15:20:49 +00:00
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
2007-10-27 00:20:37 +00:00
|
|
|
#undef JNIEXPORT
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
# define JNIEXPORT __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
# define JNIEXPORT __attribute__ ((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2008-06-16 17:45:23 +00:00
|
|
|
#define UNUSED __attribute__((unused))
|
|
|
|
|
2007-08-27 13:46:17 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline void
|
2008-11-11 15:20:49 +00:00
|
|
|
throwNew(JNIEnv* e, const char* class_, const char* message, ...)
|
2007-08-27 13:46:17 +00:00
|
|
|
{
|
|
|
|
jclass c = e->FindClass(class_);
|
|
|
|
if (c) {
|
2008-11-11 15:20:49 +00:00
|
|
|
if (message) {
|
|
|
|
static const unsigned BufferSize = 256;
|
|
|
|
char buffer[BufferSize];
|
|
|
|
|
|
|
|
va_list list;
|
|
|
|
va_start(list, message);
|
|
|
|
vsnprintf(buffer, BufferSize - 1, message, list);
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
e->ThrowNew(c, buffer);
|
|
|
|
} else {
|
|
|
|
e->ThrowNew(c, 0);
|
|
|
|
}
|
2007-08-27 13:46:17 +00:00
|
|
|
e->DeleteLocalRef(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-11 15:20:49 +00:00
|
|
|
inline void*
|
|
|
|
allocate(JNIEnv* e, unsigned size)
|
|
|
|
{
|
|
|
|
void* p = malloc(size);
|
|
|
|
if (p == 0) {
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-08-27 13:46:17 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif//JNI_UTIL
|