forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjni_util.h
More file actions
172 lines (136 loc) · 6.12 KB
/
Copy pathjni_util.h
File metadata and controls
172 lines (136 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#pragma once
#include <jni.h>
#include "arrow/array.h"
#include "arrow/io/api.h"
#include "arrow/ipc/api.h"
#include "arrow/memory_pool.h"
#include "arrow/result.h"
#include "arrow/type.h"
namespace arrow {
namespace dataset {
namespace jni {
JNIEnv* GetEnvOrAttach(JavaVM* vm);
Status CheckException(JNIEnv* env);
jclass CreateGlobalClassReference(JNIEnv* env, const char* class_name);
arrow::Result<jmethodID> GetMethodID(JNIEnv* env, jclass this_class, const char* name,
const char* sig);
arrow::Result<jmethodID> GetStaticMethodID(JNIEnv* env, jclass this_class,
const char* name, const char* sig);
std::string JStringToCString(JNIEnv* env, jstring string);
std::vector<std::string> ToStringVector(JNIEnv* env, jobjectArray& str_array);
arrow::Result<jbyteArray> ToSchemaByteArray(JNIEnv* env,
std::shared_ptr<arrow::Schema> schema);
arrow::Result<std::shared_ptr<arrow::Schema>> FromSchemaByteArray(JNIEnv* env,
jbyteArray schemaBytes);
/// \brief Export arrow::RecordBatch for Java (or other JVM languages) use.
/// The exported batch is subject to C data interface specification and can be
/// imported from Java side using provided JNI utilities.
arrow::Status ExportRecordBatch(JNIEnv* env, const std::shared_ptr<RecordBatch>& batch,
jlong struct_array);
/// \brief Import arrow::RecordBatch from JVM language side. The input data should
/// ideally be exported from specific JNI utilities from JVM language side and should
/// conform to C data interface specification.
arrow::Result<std::shared_ptr<RecordBatch>> ImportRecordBatch(
JNIEnv* env, const std::shared_ptr<Schema>& schema, jlong struct_array);
/// \brief Create a new shared_ptr on heap from shared_ptr t to prevent
/// the managed object from being garbage-collected.
///
/// \return address of the newly created shared pointer
template <typename T>
jlong CreateNativeRef(std::shared_ptr<T> t) {
std::shared_ptr<T>* retained_ptr = new std::shared_ptr<T>(t);
return reinterpret_cast<jlong>(retained_ptr);
}
/// \brief Get the shared_ptr that was derived via function CreateNativeRef.
///
/// \param[in] ref address of the shared_ptr
/// \return the shared_ptr object
template <typename T>
std::shared_ptr<T> RetrieveNativeInstance(jlong ref) {
std::shared_ptr<T>* retrieved_ptr = reinterpret_cast<std::shared_ptr<T>*>(ref);
return *retrieved_ptr;
}
/// \brief Destroy a shared_ptr using its memory address.
///
/// \param[in] ref address of the shared_ptr
template <typename T>
void ReleaseNativeRef(jlong ref) {
std::shared_ptr<T>* retrieved_ptr = reinterpret_cast<std::shared_ptr<T>*>(ref);
delete retrieved_ptr;
}
// Indicate an exception thrown during calling Java method via JNI.
// Not thread safe.
class JavaErrorDetail : public StatusDetail {
public:
JavaErrorDetail(JavaVM* vm, jthrowable cause);
virtual ~JavaErrorDetail();
const char* type_id() const override;
std::string ToString() const override;
jthrowable GetCause() const;
private:
JavaVM* vm_;
jthrowable cause_;
};
/// Listener to act on reservations/unreservations from ReservationListenableMemoryPool.
///
/// Note the memory pool will call this listener only on block-level memory
/// reservation/unreservation is granted. So the invocation parameter "size" is always
/// multiple of block size (by default, 512k) specified in memory pool.
class ReservationListener {
public:
virtual ~ReservationListener() = default;
virtual arrow::Status OnReservation(int64_t size) = 0;
virtual arrow::Status OnRelease(int64_t size) = 0;
protected:
ReservationListener() = default;
};
/// A memory pool implementation for pre-reserving memory blocks from a
/// customizable listener. This will typically be used when memory allocations
/// have to be subject to another "virtual" resource manager, which just tracks or
/// limits number of bytes of application's overall memory usage. The underlying
/// memory pool will still be responsible for actual malloc/free operations.
class ReservationListenableMemoryPool : public arrow::MemoryPool {
public:
/// \brief Constructor.
///
/// \param[in] pool the underlying memory pool
/// \param[in] listener a listener for block-level reservations/releases.
/// \param[in] block_size size of each block to reserve from the listener
explicit ReservationListenableMemoryPool(MemoryPool* pool,
std::shared_ptr<ReservationListener> listener,
int64_t block_size = 512 * 1024);
~ReservationListenableMemoryPool();
using MemoryPool::Allocate;
using MemoryPool::Free;
using MemoryPool::Reallocate;
arrow::Status Allocate(int64_t size, int64_t alignment, uint8_t** out) override;
arrow::Status Reallocate(int64_t old_size, int64_t new_size, int64_t alignment,
uint8_t** ptr) override;
void Free(uint8_t* buffer, int64_t size, int64_t alignment) override;
int64_t bytes_allocated() const override;
int64_t max_memory() const override;
std::string backend_name() const override;
std::shared_ptr<ReservationListener> get_listener();
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace jni
} // namespace dataset
} // namespace arrow