]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeMemoryPool.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / dataset / src / main / java / org / apache / arrow / dataset / jni / NativeMemoryPool.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * 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, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.arrow.dataset.jni;
19
20 /**
21 * C++ memory pool(arrow::MemoryPool)'s Java mapped instance.
22 */
23 public class NativeMemoryPool implements AutoCloseable {
24 private final long nativeInstanceId;
25
26 static {
27 JniLoader.get().ensureLoaded();
28 }
29
30 private NativeMemoryPool(long nativeInstanceId) {
31 this.nativeInstanceId = nativeInstanceId;
32 }
33
34 /**
35 * Get the default memory pool. This will return arrow::default_memory_pool() directly.
36 */
37 public static NativeMemoryPool getDefault() {
38 return new NativeMemoryPool(getDefaultMemoryPool());
39 }
40
41 /**
42 * Create a listenable memory pool (see also: arrow::ReservationListenableMemoryPool) with
43 * a specific listener. All buffers created from the memory pool should take enough reservation
44 * from the listener in advance.
45 */
46 public static NativeMemoryPool createListenable(ReservationListener listener) {
47 return new NativeMemoryPool(createListenableMemoryPool(listener));
48 }
49
50 /**
51 * Return native instance ID of this memory pool.
52 */
53 public long getNativeInstanceId() {
54 return nativeInstanceId;
55 }
56
57 /**
58 * Get current allocated bytes.
59 */
60 public long getBytesAllocated() {
61 return bytesAllocated(nativeInstanceId);
62 }
63
64 @Override
65 public void close() throws Exception {
66 releaseMemoryPool(nativeInstanceId);
67 }
68
69 private static native long getDefaultMemoryPool();
70
71 private static native long createListenableMemoryPool(ReservationListener listener);
72
73 private static native void releaseMemoryPool(long id);
74
75 private static native long bytesAllocated(long id);
76 }