1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::sys::jsize;
use log::error;
use std::ptr::NonNull;
use crate::objects::release_mode::ReleaseMode;
use crate::sys::{jboolean, jbyte, jchar, jdouble, jfloat, jint, jlong, jshort};
use crate::{errors::*, objects::JObject, sys, JNIEnv};
pub trait TypeArray {
fn get(env: &JNIEnv, obj: JObject, is_copy: &mut jboolean) -> Result<*mut Self>;
fn release(env: &JNIEnv, obj: JObject, ptr: NonNull<Self>, mode: i32) -> Result<()>;
}
macro_rules! type_array {
( $jni_type:ty, $jni_get:tt, $jni_release:tt ) => {
impl TypeArray for $jni_type {
fn get(env: &JNIEnv, obj: JObject, is_copy: &mut jboolean) -> Result<*mut Self> {
let internal = env.get_native_interface();
let res = jni_unchecked!(internal, $jni_get, *obj, is_copy);
Ok(res)
}
fn release(env: &JNIEnv, obj: JObject, ptr: NonNull<Self>, mode: i32) -> Result<()> {
let internal = env.get_native_interface();
jni_unchecked!(internal, $jni_release, *obj, ptr.as_ptr(), mode as i32);
Ok(())
}
}
};
}
type_array!(jint, GetIntArrayElements, ReleaseIntArrayElements);
type_array!(jlong, GetLongArrayElements, ReleaseLongArrayElements);
type_array!(jbyte, GetByteArrayElements, ReleaseByteArrayElements);
type_array!(
jboolean,
GetBooleanArrayElements,
ReleaseBooleanArrayElements
);
type_array!(jchar, GetCharArrayElements, ReleaseCharArrayElements);
type_array!(jshort, GetShortArrayElements, ReleaseShortArrayElements);
type_array!(jfloat, GetFloatArrayElements, ReleaseFloatArrayElements);
type_array!(jdouble, GetDoubleArrayElements, ReleaseDoubleArrayElements);
pub struct AutoArray<'a: 'b, 'b, T: TypeArray> {
obj: JObject<'a>,
ptr: NonNull<T>,
mode: ReleaseMode,
is_copy: bool,
env: &'b JNIEnv<'a>,
}
impl<'a, 'b, T: TypeArray> AutoArray<'a, 'b, T> {
pub(crate) fn new(env: &'b JNIEnv<'a>, obj: JObject<'a>, mode: ReleaseMode) -> Result<Self> {
let mut is_copy: jboolean = 0xff;
Ok(AutoArray {
obj,
ptr: {
let ptr = T::get(env, obj, &mut is_copy)?;
NonNull::new(ptr).ok_or(Error::NullPtr("Non-null ptr expected"))?
},
mode,
is_copy: is_copy == sys::JNI_TRUE,
env,
})
}
pub fn as_ptr(&self) -> *mut T {
self.ptr.as_ptr()
}
pub fn commit(&self) -> Result<()> {
self.release_array_elements(sys::JNI_COMMIT)
}
fn release_array_elements(&self, mode: i32) -> Result<()> {
T::release(self.env, self.obj, self.ptr, mode)
}
pub fn discard(&mut self) {
self.mode = ReleaseMode::NoCopyBack;
}
pub fn is_copy(&self) -> bool {
self.is_copy
}
pub fn size(&self) -> Result<jsize> {
self.env.get_array_length(*self.obj)
}
}
impl<'a, 'b, T: TypeArray> Drop for AutoArray<'a, 'b, T> {
fn drop(&mut self) {
let res = self.release_array_elements(self.mode as i32);
match res {
Ok(()) => {}
Err(e) => error!("error releasing array: {:#?}", e),
}
}
}
impl<'a, T: TypeArray> From<&'a AutoArray<'a, '_, T>> for *mut T {
fn from(other: &'a AutoArray<T>) -> *mut T {
other.as_ptr()
}
}