]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / memory / memory-netty / src / main / java / org / apache / arrow / memory / NettyAllocationManager.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.memory;
19
20 import io.netty.buffer.PooledByteBufAllocatorL;
21 import io.netty.buffer.UnsafeDirectLittleEndian;
22 import io.netty.util.internal.PlatformDependent;
23
24 /**
25 * The default implementation of {@link AllocationManager}. The implementation is responsible for managing when memory
26 * is allocated and returned to the Netty-based PooledByteBufAllocatorL.
27 */
28 public class NettyAllocationManager extends AllocationManager {
29
30 public static final AllocationManager.Factory FACTORY = new AllocationManager.Factory() {
31
32 @Override
33 public AllocationManager create(BufferAllocator accountingAllocator, long size) {
34 return new NettyAllocationManager(accountingAllocator, size);
35 }
36
37 @Override
38 public ArrowBuf empty() {
39 return EMPTY_BUFFER;
40 }
41 };
42
43 /**
44 * The default cut-off value for switching allocation strategies.
45 * If the request size is not greater than the cut-off value, we will allocate memory by
46 * {@link PooledByteBufAllocatorL} APIs,
47 * otherwise, we will use {@link PlatformDependent} APIs.
48 */
49 public static final int DEFAULT_ALLOCATION_CUTOFF_VALUE = Integer.MAX_VALUE;
50
51 private static final PooledByteBufAllocatorL INNER_ALLOCATOR = new PooledByteBufAllocatorL();
52 static final UnsafeDirectLittleEndian EMPTY = INNER_ALLOCATOR.empty;
53 static final ArrowBuf EMPTY_BUFFER = new ArrowBuf(ReferenceManager.NO_OP,
54 null,
55 0,
56 NettyAllocationManager.EMPTY.memoryAddress());
57 static final long CHUNK_SIZE = INNER_ALLOCATOR.getChunkSize();
58
59 private final long allocatedSize;
60 private final UnsafeDirectLittleEndian memoryChunk;
61 private final long allocatedAddress;
62
63 /**
64 * The cut-off value for switching allocation strategies.
65 */
66 private final int allocationCutOffValue;
67
68 NettyAllocationManager(BufferAllocator accountingAllocator, long requestedSize, int allocationCutOffValue) {
69 super(accountingAllocator);
70 this.allocationCutOffValue = allocationCutOffValue;
71
72 if (requestedSize > allocationCutOffValue) {
73 this.memoryChunk = null;
74 this.allocatedAddress = PlatformDependent.allocateMemory(requestedSize);
75 this.allocatedSize = requestedSize;
76 } else {
77 this.memoryChunk = INNER_ALLOCATOR.allocate(requestedSize);
78 this.allocatedAddress = memoryChunk.memoryAddress();
79 this.allocatedSize = memoryChunk.capacity();
80 }
81 }
82
83 NettyAllocationManager(BufferAllocator accountingAllocator, long requestedSize) {
84 this(accountingAllocator, requestedSize, DEFAULT_ALLOCATION_CUTOFF_VALUE);
85 }
86
87 /**
88 * Get the underlying memory chunk managed by this AllocationManager.
89 * @return the underlying memory chunk if the request size is not greater than the
90 * {@link NettyAllocationManager#allocationCutOffValue}, or null otherwise.
91 *
92 * @deprecated this method will be removed in a future release.
93 */
94 @Deprecated
95 UnsafeDirectLittleEndian getMemoryChunk() {
96 return memoryChunk;
97 }
98
99 @Override
100 protected long memoryAddress() {
101 return allocatedAddress;
102 }
103
104 @Override
105 protected void release0() {
106 if (memoryChunk == null) {
107 PlatformDependent.freeMemory(allocatedAddress);
108 } else {
109 memoryChunk.release();
110 }
111 }
112
113 /**
114 * Returns the underlying memory chunk size managed.
115 *
116 * <p>NettyAllocationManager rounds requested size up to the next power of two.
117 */
118 @Override
119 public long getSize() {
120 return allocatedSize;
121 }
122
123 }