| 1 |
/* |
| 2 |
* Copyright (C) 2007, 2008 Wayne Meissner |
| 3 |
* |
| 4 |
* This file is part of jffi. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
* |
| 18 |
* |
| 19 |
* Alternatively, you can redistribute it and/or modify it under |
| 20 |
* the terms of the GNU Lesser General Public License as published by |
| 21 |
* the Free Software Foundation, either version 3 of the License, or |
| 22 |
* (at your option) any later version. |
| 23 |
* |
| 24 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 26 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 27 |
* for more details. |
| 28 |
* |
| 29 |
* You should have received a copy of the GNU Lesser General Public License |
| 30 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifndef jffi_jffi_h |
| 34 |
#define jffi_jffi_h |
| 35 |
|
| 36 |
#include <sys/param.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <stdint.h> |
| 39 |
#include <string.h> |
| 40 |
#ifndef _WIN32 |
| 41 |
# include <pthread.h> |
| 42 |
#endif |
| 43 |
#include "endian.h" |
| 44 |
#include <jni.h> |
| 45 |
#include <ffi.h> |
| 46 |
|
| 47 |
#ifdef __cplusplus |
| 48 |
extern "C" { |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifndef roundup |
| 52 |
# define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef _WIN32 |
| 56 |
typedef char* caddr_t; |
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
#ifdef __GNUC__ |
| 61 |
# define likely(x) __builtin_expect((x), 1) |
| 62 |
# define unlikely(x) __builtin_expect((x), 0) |
| 63 |
#else |
| 64 |
# define likely(x) (x) |
| 65 |
# define unlikely(x) (x) |
| 66 |
#endif |
| 67 |
|
| 68 |
/** |
| 69 |
* Convert a C pointer into a java long |
| 70 |
*/ |
| 71 |
static inline jlong |
| 72 |
p2j(void *p) |
| 73 |
{ |
| 74 |
return (jlong)(uintptr_t) p; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Convert a java long into a C pointer |
| 79 |
*/ |
| 80 |
static inline void* |
| 81 |
j2p(jlong j) |
| 82 |
{ |
| 83 |
return (void *)(uintptr_t) j; |
| 84 |
} |
| 85 |
|
| 86 |
#ifndef __cplusplus |
| 87 |
static inline |
| 88 |
jboolean loadClass(JNIEnv* env, const char *name, jclass *classp) |
| 89 |
{ |
| 90 |
jclass tmp = (*env)->FindClass(env, name); |
| 91 |
if (tmp == NULL) { |
| 92 |
return JNI_FALSE; |
| 93 |
} |
| 94 |
*classp = (jclass)(*env)->NewGlobalRef(env, tmp); |
| 95 |
return JNI_TRUE; |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
typedef union FFIValue { |
| 100 |
|
| 101 |
int8_t s8; |
| 102 |
uint8_t u8; |
| 103 |
int16_t s16; |
| 104 |
uint16_t u16; |
| 105 |
int32_t s32; |
| 106 |
uint32_t u32; |
| 107 |
int64_t s64; |
| 108 |
uint64_t u64; |
| 109 |
jint i; |
| 110 |
jlong j; |
| 111 |
long l; |
| 112 |
float f; |
| 113 |
double d; |
| 114 |
void* p; |
| 115 |
ffi_sarg sarg; |
| 116 |
ffi_arg arg; |
| 117 |
} FFIValue; |
| 118 |
|
| 119 |
#ifndef _WIN32 |
| 120 |
typedef struct ThreadData { |
| 121 |
int error; |
| 122 |
int attach_count; |
| 123 |
JavaVM* attached_vm; |
| 124 |
struct FaultData_* fault_data; |
| 125 |
} ThreadData; |
| 126 |
|
| 127 |
extern pthread_key_t jffi_threadDataKey; |
| 128 |
extern ThreadData* jffi_thread_data_init(); |
| 129 |
|
| 130 |
static inline ThreadData* |
| 131 |
thread_data_get() |
| 132 |
{ |
| 133 |
ThreadData* td = (ThreadData *) pthread_getspecific(jffi_threadDataKey); |
| 134 |
return likely(td != NULL) ? td : jffi_thread_data_init(); |
| 135 |
} |
| 136 |
#endif /* !_WIN32 */ |
| 137 |
|
| 138 |
#if BYTE_ORDER == LITTLE_ENDIAN |
| 139 |
# define return_int(retval) return ((retval).i) |
| 140 |
# define ARGPTR(argp, type) (argp) |
| 141 |
#elif BYTE_ORDER == BIG_ENDIAN |
| 142 |
# define return_int(retval) return ((retval).l & 0xFFFFFFFFL) |
| 143 |
# define ARGPTR(argp, type) (((caddr_t) (argp)) + sizeof(*argp) - (type)->size) |
| 144 |
#else |
| 145 |
# error "Unsupported BYTE_ORDER" |
| 146 |
#endif |
| 147 |
|
| 148 |
# define ffi_call0(ctx, fn, retval) do { \ |
| 149 |
FFIValue arg0; \ |
| 150 |
void* ffiValues[] = { &arg0 }; \ |
| 151 |
ffi_call(&(ctx)->cif, FFI_FN((fn)), (retval), ffiValues); \ |
| 152 |
} while (0) |
| 153 |
|
| 154 |
# define ffi_call1(ctx, fn, retval, arg1) do { \ |
| 155 |
void* ffiValues[] = { ARGPTR(&(arg1), (ctx)->cif.arg_types[0]) }; \ |
| 156 |
ffi_call(&(ctx)->cif, FFI_FN((fn)), (retval), ffiValues); \ |
| 157 |
} while (0) |
| 158 |
|
| 159 |
# define ffi_call2(ctx, fn, retval, arg1, arg2) do {\ |
| 160 |
void* ffiValues[] = { \ |
| 161 |
ARGPTR(&arg1, (ctx)->cif.arg_types[0]), \ |
| 162 |
ARGPTR(&arg2, (ctx)->cif.arg_types[1]) \ |
| 163 |
}; \ |
| 164 |
ffi_call(&(ctx)->cif, FFI_FN((fn)), (retval), ffiValues); \ |
| 165 |
} while (0) |
| 166 |
|
| 167 |
# define ffi_call3(ctx, fn, retval, arg1, arg2, arg3) do { \ |
| 168 |
void* ffiValues[] = { \ |
| 169 |
ARGPTR(&arg1, (ctx)->cif.arg_types[0]), \ |
| 170 |
ARGPTR(&arg2, (ctx)->cif.arg_types[1]), \ |
| 171 |
ARGPTR(&arg3, (ctx)->cif.arg_types[2]) \ |
| 172 |
}; \ |
| 173 |
ffi_call(&(ctx)->cif, FFI_FN((fn)), (retval), ffiValues); \ |
| 174 |
} while (0) |
| 175 |
|
| 176 |
# define ffi_call4(ctx, fn, retval, arg1, arg2, arg3, arg4) do { \ |
| 177 |
void* ffiValues[] = { \ |
| 178 |
ARGPTR(&arg1, (ctx)->cif.arg_types[0]), \ |
| 179 |
ARGPTR(&arg2, (ctx)->cif.arg_types[1]), \ |
| 180 |
ARGPTR(&arg3, (ctx)->cif.arg_types[2]), \ |
| 181 |
ARGPTR(&arg4, (ctx)->cif.arg_types[3]) \ |
| 182 |
}; \ |
| 183 |
ffi_call(&(ctx)->cif, FFI_FN((fn)), (retval), ffiValues); \ |
| 184 |
} while (0) |
| 185 |
|
| 186 |
# define ffi_call5(ctx, fn, retval, arg1, arg2, arg3, arg4, arg5) do { \ |
| 187 |
void* ffiValues[] = { \ |
| 188 |
ARGPTR(&arg1, (ctx)->cif.arg_types[0]), \ |
| 189 |
ARGPTR(&arg2, (ctx)->cif.arg_types[1]), \ |
| 190 |
ARGPTR(&arg3, (ctx)->cif.arg_types[2]), \ |
| 191 |
ARGPTR(&arg4, (ctx)->cif.arg_types[3]), \ |
| 192 |
ARGPTR(&arg5, (ctx)->cif.arg_types[4]) \ |
| 193 |
}; \ |
| 194 |
ffi_call(&(ctx)->cif, FFI_FN((fn)), (retval), ffiValues); \ |
| 195 |
} while (0) |
| 196 |
|
| 197 |
# define ffi_call6(ctx, fn, retval, arg1, arg2, arg3, arg4, arg5, arg6) do { \ |
| 198 |
void* ffiValues[] = { \ |
| 199 |
ARGPTR(&arg1, (ctx)->cif.arg_types[0]), \ |
| 200 |
ARGPTR(&arg2, (ctx)->cif.arg_types[1]), \ |
| 201 |
ARGPTR(&arg3, (ctx)->cif.arg_types[2]), \ |
| 202 |
ARGPTR(&arg4, (ctx)->cif.arg_types[3]), \ |
| 203 |
ARGPTR(&arg5, (ctx)->cif.arg_types[4]), \ |
| 204 |
ARGPTR(&arg6, (ctx)->cif.arg_types[5]) \ |
| 205 |
}; \ |
| 206 |
ffi_call(&(ctx)->cif, FFI_FN((fn)), (retval), ffiValues); \ |
| 207 |
} while (0) |
| 208 |
|
| 209 |
|
| 210 |
#if defined(__APPLE__) |
| 211 |
# define debug(fmt, a...) dprintf(STDERR_FILENO, fmt "\n", ##a) |
| 212 |
#else |
| 213 |
# define debug(fmt, a...) do { \ |
| 214 |
char tmp[1024]; \ |
| 215 |
write(STDERR_FILENO, tmp, snprintf(tmp, sizeof(tmp), fmt "\n", ##a)); \ |
| 216 |
} while(0) |
| 217 |
#endif |
| 218 |
|
| 219 |
#ifdef __cplusplus |
| 220 |
} |
| 221 |
#endif |
| 222 |
|
| 223 |
#endif /* jffi_jffi_h */ |
| 224 |
|
| 225 |
|