| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
/* == HEADER-SNIP-LOCATION == */ |
| 12 |
/** |
| 13 |
* hawtjni.h |
| 14 |
* |
| 15 |
* This file contains the global macro declarations for a hawtjni based |
| 16 |
* library. |
| 17 |
* |
| 18 |
*/ |
| 19 |
#ifndef INC_HAWTJNI_H |
| 20 |
#define INC_HAWTJNI_H |
| 21 |
#define _MULTI_THREADED |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include "config.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "jni.h" |
| 28 |
#include <stdint.h> |
| 29 |
|
| 30 |
|
| 31 |
#ifdef __cplusplus |
| 32 |
extern "C" { |
| 33 |
#endif |
| 34 |
|
| 35 |
extern int IS_JNI_1_2; |
| 36 |
|
| 37 |
#ifndef JNI64 |
| 38 |
#if defined(_LP64) |
| 39 |
#define JNI64 |
| 40 |
#endif |
| 41 |
#endif |
| 42 |
|
| 43 |
/* 64 bit support */ |
| 44 |
#ifndef JNI64 |
| 45 |
|
| 46 |
/* int/long defines */ |
| 47 |
#define GetIntLongField GetIntField |
| 48 |
#define SetIntLongField SetIntField |
| 49 |
#define GetIntLongArrayElements GetIntArrayElements |
| 50 |
#define ReleaseIntLongArrayElements ReleaseIntArrayElements |
| 51 |
#define GetIntLongArrayRegion GetIntArrayRegion |
| 52 |
#define SetIntLongArrayRegion SetIntArrayRegion |
| 53 |
#define NewIntLongArray NewIntArray |
| 54 |
#define CallStaticIntLongMethod CallStaticIntMethod |
| 55 |
#define CallIntLongMethod CallIntMethod |
| 56 |
#define CallStaticIntLongMethodV CallStaticIntMethodV |
| 57 |
#define CallIntLongMethodV CallIntMethodV |
| 58 |
#define jintLongArray jintArray |
| 59 |
#define jintLong jint |
| 60 |
#define I_J "I" |
| 61 |
#define I_JArray "[I" |
| 62 |
|
| 63 |
/* float/double defines */ |
| 64 |
#define GetFloatDoubleField GetFloatField |
| 65 |
#define SetFloatDoubleField SetFloatField |
| 66 |
#define GetFloatDoubleArrayElements GetFloatArrayElements |
| 67 |
#define ReleaseFloatDoubleArrayElements ReleaseFloatArrayElements |
| 68 |
#define GetFloatDoubleArrayRegion GetFloatArrayRegion |
| 69 |
#define jfloatDoubleArray jfloatArray |
| 70 |
#define jfloatDouble jfloat |
| 71 |
#define F_D "F" |
| 72 |
#define F_DArray "[F" |
| 73 |
|
| 74 |
#else |
| 75 |
|
| 76 |
/* int/long defines */ |
| 77 |
#define GetIntLongField GetLongField |
| 78 |
#define SetIntLongField SetLongField |
| 79 |
#define GetIntLongArrayElements GetLongArrayElements |
| 80 |
#define ReleaseIntLongArrayElements ReleaseLongArrayElements |
| 81 |
#define GetIntLongArrayRegion GetLongArrayRegion |
| 82 |
#define SetIntLongArrayRegion SetLongArrayRegion |
| 83 |
#define NewIntLongArray NewLongArray |
| 84 |
#define CallStaticIntLongMethod CallStaticLongMethod |
| 85 |
#define CallIntLongMethod CallLongMethod |
| 86 |
#define CallStaticIntLongMethodV CallStaticLongMethodV |
| 87 |
#define CallIntLongMethodV CallLongMethodV |
| 88 |
#define jintLongArray jlongArray |
| 89 |
#define jintLong jlong |
| 90 |
#define I_J "J" |
| 91 |
#define I_JArray "[J" |
| 92 |
|
| 93 |
/* float/double defines */ |
| 94 |
#define GetFloatDoubleField GetDoubleField |
| 95 |
#define SetFloatDoubleField SetDoubleField |
| 96 |
#define GetFloatDoubleArrayElements GetDoubleArrayElements |
| 97 |
#define ReleaseFloatDoubleArrayElements ReleaseDoubleArrayElements |
| 98 |
#define GetFloatDoubleArrayRegion GetDoubleArrayRegion |
| 99 |
#define jfloatDoubleArray jdoubleArray |
| 100 |
#define jfloatDouble jdouble |
| 101 |
#define F_D "D" |
| 102 |
#define F_DArray "[D" |
| 103 |
|
| 104 |
#endif |
| 105 |
|
| 106 |
#ifdef __APPLE__ |
| 107 |
#define CALLING_CONVENTION |
| 108 |
#define LOAD_FUNCTION(var, name) \ |
| 109 |
static int initialized = 0; \ |
| 110 |
static void *var = NULL; \ |
| 111 |
if (!initialized) { \ |
| 112 |
CFBundleRef bundle = CFBundleGetBundleWithIdentifier(CFSTR(name##_LIB)); \ |
| 113 |
if (bundle) var = CFBundleGetFunctionPointerForName(bundle, CFSTR(#name)); \ |
| 114 |
initialized = 1; \ |
| 115 |
} |
| 116 |
#elif defined (_WIN32) || defined (_WIN32_WCE) |
| 117 |
#define CALLING_CONVENTION CALLBACK |
| 118 |
#define LOAD_FUNCTION(var, name) \ |
| 119 |
static int initialized = 0; \ |
| 120 |
static FARPROC var = NULL; \ |
| 121 |
if (!initialized) { \ |
| 122 |
HMODULE hm = LoadLibrary(name##_LIB); \ |
| 123 |
if (hm) var = GetProcAddress(hm, #name); \ |
| 124 |
initialized = 1; \ |
| 125 |
} |
| 126 |
#else |
| 127 |
#define CALLING_CONVENTION |
| 128 |
#define LOAD_FUNCTION(var, name) \ |
| 129 |
static int initialized = 0; \ |
| 130 |
static void *var = NULL; \ |
| 131 |
if (!initialized) { \ |
| 132 |
void* handle = dlopen(name##_LIB, RTLD_LAZY); \ |
| 133 |
if (handle) var = dlsym(handle, #name); \ |
| 134 |
initialized = 1; \ |
| 135 |
} |
| 136 |
#endif |
| 137 |
|
| 138 |
#ifdef JNI_VERSION_1_2 |
| 139 |
extern JavaVM *JVM; |
| 140 |
jint hawtjni_attach_thread(JNIEnv **env, const char *thread_name); |
| 141 |
jint hawtjni_detach_thread(); |
| 142 |
#endif |
| 143 |
|
| 144 |
void throwOutOfMemory(JNIEnv *env); |
| 145 |
|
| 146 |
#define CHECK_NULL_VOID(ptr) \ |
| 147 |
if ((ptr) == NULL) { \ |
| 148 |
throwOutOfMemory(env); \ |
| 149 |
return; \ |
| 150 |
} |
| 151 |
|
| 152 |
#define CHECK_NULL(ptr) \ |
| 153 |
if ((ptr) == NULL) { \ |
| 154 |
throwOutOfMemory(env); \ |
| 155 |
return 0; \ |
| 156 |
} |
| 157 |
|
| 158 |
#ifndef JNI64 |
| 159 |
void ** hawtjni_malloc_pointer_array(JNIEnv *env, jlongArray array); |
| 160 |
void hawtjni_free_pointer_array(JNIEnv *env, jlongArray array, void **elems, jint mode); |
| 161 |
#else |
| 162 |
#ifdef __cplusplus |
| 163 |
#define hawtjni_malloc_pointer_array(env, array) ( (void **)(intptr_t)env->GetLongArrayElements(array, NULL) ) |
| 164 |
#define hawtjni_free_pointer_array(env, array, elems, mode) ( env->ReleaseLongArrayElements(array, (jlong*)elems, mode) ) |
| 165 |
#else |
| 166 |
#define hawtjni_malloc_pointer_array(env, source) ( (void **)(intptr_t)(*env)->GetLongArrayElements(env, source, NULL) ) |
| 167 |
#define hawtjni_free_pointer_array(env, array, elems, mode) ( (*env)->ReleaseLongArrayElements(env, array, (jlong*)elems, mode) ) |
| 168 |
#endif |
| 169 |
#endif /* JNI64 */ |
| 170 |
|
| 171 |
#ifdef __GNUC__ |
| 172 |
#define hawtjni_w_barrier() __sync_synchronize() |
| 173 |
#elif defined(SOLARIS2) && SOLARIS2 >= 10 |
| 174 |
#include <mbarrier.h> |
| 175 |
#define hawtjni_w_barrier() __machine_w_barrier() |
| 176 |
#elif defined(__APPLE__) |
| 177 |
#include <libkern/OSAtomic.h> |
| 178 |
#define hawtjni_w_barrier() OSMemoryBarrier() |
| 179 |
#elif defined(_WIN32) || defined(_WIN64) |
| 180 |
#include <intrin.h> |
| 181 |
#define hawtjni_w_barrier() _mm_sfence(); _WriteBarrier() |
| 182 |
#else |
| 183 |
#pragma message ( "Don't know how to do a memory barrier on this platform" ) |
| 184 |
#define hawtjni_w_barrier() |
| 185 |
#endif |
| 186 |
|
| 187 |
void hawtjni_atomic_set(jlong *target, jlong value); |
| 188 |
jlong hawtjni_atomic_get(jlong *target); |
| 189 |
|
| 190 |
#ifdef __cplusplus |
| 191 |
} |
| 192 |
#endif |
| 193 |
|
| 194 |
#endif /* ifndef INC_HAWTJNI_H */ |