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 <cstdlib> |
21 | | #include <vector> |
22 | | |
23 | | #include "jni.h" |
24 | | #include "util/defer_op.h" |
25 | | |
26 | | namespace doris { |
27 | | |
28 | 217k | jlong JavaNativeMethods::memoryMalloc(JNIEnv* env, jclass clazz, jlong bytes) { |
29 | 217k | return reinterpret_cast<long>(malloc(bytes)); |
30 | 217k | } |
31 | | |
32 | 429k | void JavaNativeMethods::memoryFree(JNIEnv* env, jclass clazz, jlong address) { |
33 | 429k | free(reinterpret_cast<void*>(address)); |
34 | 429k | } |
35 | | |
36 | 100 | jlongArray JavaNativeMethods::memoryMallocBatch(JNIEnv* env, jclass clazz, jintArray sizes) { |
37 | 100 | DCHECK(sizes != nullptr); |
38 | 100 | jsize n = env->GetArrayLength(sizes); |
39 | 100 | DCHECK(n > 0); |
40 | 100 | jint* elems = env->GetIntArrayElements(sizes, nullptr); |
41 | 100 | if (elems == nullptr) { |
42 | 0 | return nullptr; |
43 | 0 | } |
44 | 100 | DEFER({ |
45 | 100 | if (elems != nullptr) { |
46 | 100 | env->ReleaseIntArrayElements(sizes, elems, JNI_ABORT); |
47 | 100 | } |
48 | 100 | }); |
49 | | |
50 | 100 | jlongArray result = env->NewLongArray(n); |
51 | 100 | if (result == nullptr) { |
52 | 0 | return nullptr; |
53 | 0 | } |
54 | | |
55 | 100 | std::vector<void*> allocated; |
56 | 100 | allocated.reserve(n); |
57 | | |
58 | | // sizes are validated on Java side: n > 0 and each size > 0 |
59 | 100 | bool failed = false; |
60 | 440 | for (jsize i = 0; i < n; ++i) { |
61 | 340 | auto sz = static_cast<size_t>(elems[i]); |
62 | 340 | void* p = malloc(sz); |
63 | 340 | if (p == nullptr) { |
64 | 0 | failed = true; |
65 | 0 | break; |
66 | 0 | } |
67 | 340 | allocated.push_back(p); |
68 | 340 | } |
69 | | |
70 | 100 | if (failed) { |
71 | 0 | for (void* p : allocated) { |
72 | 0 | if (p != nullptr) { |
73 | 0 | free(p); |
74 | 0 | } |
75 | 0 | } |
76 | 0 | return nullptr; |
77 | 0 | } |
78 | | |
79 | 100 | std::vector<jlong> addrs(n); |
80 | 440 | for (jsize i = 0; i < n; ++i) { |
81 | 340 | addrs[i] = reinterpret_cast<jlong>(allocated[i]); |
82 | 340 | } |
83 | 100 | env->SetLongArrayRegion(result, 0, n, addrs.data()); |
84 | 100 | return result; |
85 | 100 | } |
86 | | |
87 | 130 | void JavaNativeMethods::memoryFreeBatch(JNIEnv* env, jclass clazz, jlongArray addrs) { |
88 | 130 | if (addrs == nullptr) { |
89 | 0 | return; |
90 | 0 | } |
91 | 130 | jsize n = env->GetArrayLength(addrs); |
92 | 130 | if (n <= 0) { |
93 | 0 | return; |
94 | 0 | } |
95 | 130 | jlong* elems = env->GetLongArrayElements(addrs, nullptr); |
96 | 130 | if (elems == nullptr) { |
97 | 0 | return; |
98 | 0 | } |
99 | 752 | for (jsize i = 0; i < n; ++i) { |
100 | 622 | if (elems[i] != 0) { |
101 | 622 | free(reinterpret_cast<void*>(elems[i])); |
102 | 622 | } |
103 | 622 | } |
104 | 130 | env->ReleaseLongArrayElements(addrs, elems, JNI_ABORT); |
105 | 130 | } |
106 | | |
107 | | } // namespace doris |