Coverage Report

Created: 2026-07-23 18:57

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