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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.memory
;
20 import io
.netty
.buffer
.PooledByteBufAllocatorL
;
21 import io
.netty
.buffer
.UnsafeDirectLittleEndian
;
22 import io
.netty
.util
.internal
.PlatformDependent
;
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.
28 public class NettyAllocationManager
extends AllocationManager
{
30 public static final AllocationManager
.Factory FACTORY
= new AllocationManager
.Factory() {
33 public AllocationManager
create(BufferAllocator accountingAllocator
, long size
) {
34 return new NettyAllocationManager(accountingAllocator
, size
);
38 public ArrowBuf
empty() {
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.
49 public static final int DEFAULT_ALLOCATION_CUTOFF_VALUE
= Integer
.MAX_VALUE
;
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
,
56 NettyAllocationManager
.EMPTY
.memoryAddress());
57 static final long CHUNK_SIZE
= INNER_ALLOCATOR
.getChunkSize();
59 private final long allocatedSize
;
60 private final UnsafeDirectLittleEndian memoryChunk
;
61 private final long allocatedAddress
;
64 * The cut-off value for switching allocation strategies.
66 private final int allocationCutOffValue
;
68 NettyAllocationManager(BufferAllocator accountingAllocator
, long requestedSize
, int allocationCutOffValue
) {
69 super(accountingAllocator
);
70 this.allocationCutOffValue
= allocationCutOffValue
;
72 if (requestedSize
> allocationCutOffValue
) {
73 this.memoryChunk
= null;
74 this.allocatedAddress
= PlatformDependent
.allocateMemory(requestedSize
);
75 this.allocatedSize
= requestedSize
;
77 this.memoryChunk
= INNER_ALLOCATOR
.allocate(requestedSize
);
78 this.allocatedAddress
= memoryChunk
.memoryAddress();
79 this.allocatedSize
= memoryChunk
.capacity();
83 NettyAllocationManager(BufferAllocator accountingAllocator
, long requestedSize
) {
84 this(accountingAllocator
, requestedSize
, DEFAULT_ALLOCATION_CUTOFF_VALUE
);
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.
92 * @deprecated this method will be removed in a future release.
95 UnsafeDirectLittleEndian
getMemoryChunk() {
100 protected long memoryAddress() {
101 return allocatedAddress
;
105 protected void release0() {
106 if (memoryChunk
== null) {
107 PlatformDependent
.freeMemory(allocatedAddress
);
109 memoryChunk
.release();
114 * Returns the underlying memory chunk size managed.
116 * <p>NettyAllocationManager rounds requested size up to the next power of two.
119 public long getSize() {
120 return allocatedSize
;