be/src/util/jni_native_method.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "util/jni_native_method.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <cstdlib> |
23 | | #include <vector> |
24 | | |
25 | | #include "jni.h" |
26 | | #include "util/defer_op.h" |
27 | | |
28 | | namespace doris { |
29 | | |
30 | 222k | jlong JavaNativeMethods::memoryMalloc(JNIEnv* env, jclass clazz, jlong bytes) { |
31 | 222k | return reinterpret_cast<long>(malloc(bytes)); |
32 | 222k | } |
33 | | |
34 | 416k | void JavaNativeMethods::memoryFree(JNIEnv* env, jclass clazz, jlong address) { |
35 | 416k | free(reinterpret_cast<void*>(address)); |
36 | 416k | } |
37 | | |
38 | 104 | jlongArray JavaNativeMethods::memoryMallocBatch(JNIEnv* env, jclass clazz, jintArray sizes) { |
39 | 104 | DCHECK(sizes != nullptr); |
40 | 104 | jsize n = env->GetArrayLength(sizes); |
41 | 104 | DCHECK(n > 0); |
42 | 104 | jint* elems = env->GetIntArrayElements(sizes, nullptr); |
43 | 104 | if (elems == nullptr) { |
44 | 0 | return nullptr; |
45 | 0 | } |
46 | 104 | DEFER({ |
47 | 104 | if (elems != nullptr) { |
48 | 104 | env->ReleaseIntArrayElements(sizes, elems, JNI_ABORT); |
49 | 104 | } |
50 | 104 | }); |
51 | | |
52 | 104 | jlongArray result = env->NewLongArray(n); |
53 | 104 | if (result == nullptr) { |
54 | 0 | return nullptr; |
55 | 0 | } |
56 | | |
57 | 104 | std::vector<void*> allocated; |
58 | 104 | allocated.reserve(n); |
59 | | |
60 | | // sizes are validated on Java side: n > 0 and each size > 0 |
61 | 104 | bool failed = false; |
62 | 450 | for (jsize i = 0; i < n; ++i) { |
63 | 346 | auto sz = static_cast<size_t>(elems[i]); |
64 | 346 | void* p = malloc(sz); |
65 | 346 | if (p == nullptr) { |
66 | 0 | failed = true; |
67 | 0 | break; |
68 | 0 | } |
69 | 346 | allocated.push_back(p); |
70 | 346 | } |
71 | | |
72 | 104 | if (failed) { |
73 | 0 | for (void* p : allocated) { |
74 | 0 | if (p != nullptr) { |
75 | 0 | free(p); |
76 | 0 | } |
77 | 0 | } |
78 | 0 | return nullptr; |
79 | 0 | } |
80 | | |
81 | 104 | std::vector<jlong> addrs(n); |
82 | 450 | for (jsize i = 0; i < n; ++i) { |
83 | 346 | addrs[i] = reinterpret_cast<jlong>(allocated[i]); |
84 | 346 | } |
85 | 104 | env->SetLongArrayRegion(result, 0, n, addrs.data()); |
86 | 104 | return result; |
87 | 104 | } |
88 | | |
89 | 134 | void JavaNativeMethods::memoryFreeBatch(JNIEnv* env, jclass clazz, jlongArray addrs) { |
90 | 134 | if (addrs == nullptr) { |
91 | 0 | return; |
92 | 0 | } |
93 | 134 | jsize n = env->GetArrayLength(addrs); |
94 | 134 | if (n <= 0) { |
95 | 0 | return; |
96 | 0 | } |
97 | 134 | jlong* elems = env->GetLongArrayElements(addrs, nullptr); |
98 | 134 | if (elems == nullptr) { |
99 | 0 | return; |
100 | 0 | } |
101 | 762 | for (jsize i = 0; i < n; ++i) { |
102 | 628 | if (elems[i] != 0) { |
103 | 628 | free(reinterpret_cast<void*>(elems[i])); |
104 | 628 | } |
105 | 628 | } |
106 | 134 | env->ReleaseLongArrayElements(addrs, elems, JNI_ABORT); |
107 | 134 | } |
108 | | |
109 | | } // namespace doris |