Coverage Report

Created: 2026-03-16 08:10

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