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
use log::debug;
use crate::sys::jsize;
use crate::wrapper::objects::ReleaseMode;
use crate::{errors::*, objects::JObject, JNIEnv};
use std::os::raw::c_void;
use std::ptr::NonNull;
pub struct AutoPrimitiveArray<'a: 'b, 'b> {
obj: JObject<'a>,
ptr: NonNull<c_void>,
mode: ReleaseMode,
is_copy: bool,
env: &'b JNIEnv<'a>,
}
impl<'a, 'b> AutoPrimitiveArray<'a, 'b> {
pub(crate) fn new(
env: &'b JNIEnv<'a>,
obj: JObject<'a>,
ptr: *mut c_void,
mode: ReleaseMode,
is_copy: bool,
) -> Result<Self> {
Ok(AutoPrimitiveArray {
obj,
ptr: NonNull::new(ptr).ok_or(Error::NullPtr("Non-null ptr expected"))?,
mode,
is_copy,
env,
})
}
pub fn as_ptr(&self) -> *mut c_void {
self.ptr.as_ptr()
}
fn release_primitive_array_critical(&mut self, mode: i32) -> Result<()> {
jni_unchecked!(
self.env.get_native_interface(),
ReleasePrimitiveArrayCritical,
*self.obj,
self.ptr.as_mut(),
mode
);
Ok(())
}
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> Drop for AutoPrimitiveArray<'a, 'b> {
fn drop(&mut self) {
let res = self.release_primitive_array_critical(self.mode as i32);
match res {
Ok(()) => {}
Err(e) => debug!("error releasing primitive array: {:#?}", e),
}
}
}
impl<'a> From<&'a AutoPrimitiveArray<'a, '_>> for *mut c_void {
fn from(other: &'a AutoPrimitiveArray) -> *mut c_void {
other.as_ptr()
}
}