Coverage Report

Created: 2026-06-05 04:37

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