]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferAllocator.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / memory / memory-core / src / main / java / org / apache / arrow / memory / BufferAllocator.java
CommitLineData
1d09f67e
TL
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
18package org.apache.arrow.memory;
19
20import java.util.Collection;
21
22import org.apache.arrow.memory.rounding.DefaultRoundingPolicy;
23import org.apache.arrow.memory.rounding.RoundingPolicy;
24
25/**
26 * Wrapper class to deal with byte buffer allocation. Ensures users only use designated methods.
27 */
28public interface BufferAllocator extends AutoCloseable {
29
30 /**
31 * Allocate a new or reused buffer of the provided size. Note that the buffer may technically
32 * be larger than the
33 * requested size for rounding purposes. However, the buffer's capacity will be set to the
34 * configured size.
35 *
36 * @param size The size in bytes.
37 * @return a new ArrowBuf, or null if the request can't be satisfied
38 * @throws OutOfMemoryException if buffer cannot be allocated
39 */
40 ArrowBuf buffer(long size);
41
42 /**
43 * Allocate a new or reused buffer of the provided size. Note that the buffer may technically
44 * be larger than the
45 * requested size for rounding purposes. However, the buffer's capacity will be set to the
46 * configured size.
47 *
48 * @param size The size in bytes.
49 * @param manager A buffer manager to manage reallocation.
50 * @return a new ArrowBuf, or null if the request can't be satisfied
51 * @throws OutOfMemoryException if buffer cannot be allocated
52 */
53 ArrowBuf buffer(long size, BufferManager manager);
54
55 /**
56 * Get the root allocator of this allocator. If this allocator is already a root, return
57 * this directly.
58 *
59 * @return The root allocator
60 */
61 BufferAllocator getRoot();
62
63 /**
64 * Create a new child allocator.
65 *
66 * @param name the name of the allocator.
67 * @param initReservation the initial space reservation (obtained from this allocator)
68 * @param maxAllocation maximum amount of space the new allocator can allocate
69 * @return the new allocator, or null if it can't be created
70 */
71 BufferAllocator newChildAllocator(String name, long initReservation, long maxAllocation);
72
73 /**
74 * Create a new child allocator.
75 *
76 * @param name the name of the allocator.
77 * @param listener allocation listener for the newly created child
78 * @param initReservation the initial space reservation (obtained from this allocator)
79 * @param maxAllocation maximum amount of space the new allocator can allocate
80 * @return the new allocator, or null if it can't be created
81 */
82 BufferAllocator newChildAllocator(
83 String name,
84 AllocationListener listener,
85 long initReservation,
86 long maxAllocation);
87
88 /**
89 * Close and release all buffers generated from this buffer pool.
90 *
91 * <p>When assertions are on, complains if there are any outstanding buffers; to avoid
92 * that, release all buffers before the allocator is closed.</p>
93 */
94 @Override
95 void close();
96
97 /**
98 * Returns the amount of memory currently allocated from this allocator.
99 *
100 * @return the amount of memory currently allocated
101 */
102 long getAllocatedMemory();
103
104 /**
105 * Return the current maximum limit this allocator imposes.
106 *
107 * @return Limit in number of bytes.
108 */
109 long getLimit();
110
111 /**
112 * Return the initial reservation.
113 *
114 * @return reservation in bytes.
115 */
116 long getInitReservation();
117
118 /**
119 * Set the maximum amount of memory this allocator is allowed to allocate.
120 *
121 * @param newLimit The new Limit to apply to allocations
122 */
123 void setLimit(long newLimit);
124
125 /**
126 * Returns the peak amount of memory allocated from this allocator.
127 *
128 * @return the peak amount of memory allocated
129 */
130 long getPeakMemoryAllocation();
131
132 /**
133 * Returns the amount of memory that can probably be allocated at this moment
134 * without exceeding this or any parents allocation maximum.
135 *
136 * @return Headroom in bytes
137 */
138 long getHeadroom();
139
140 /**
141 * Forcibly allocate bytes. Returns whether the allocation fit within limits.
142 *
143 * @param size to increase
144 * @return Whether the allocation fit within limits.
145 */
146 boolean forceAllocate(long size);
147
148
149 /**
150 * Release bytes from this allocator.
151 *
152 * @param size to release
153 */
154 void releaseBytes(long size);
155
156 /**
157 * Returns the allocation listener used by this allocator.
158 *
159 * @return the {@link AllocationListener} instance. Or {@link AllocationListener#NOOP} by default if no listener
160 * is configured when this allocator was created.
161 */
162 AllocationListener getListener();
163
164 /**
165 * Returns the parent allocator.
166 *
167 * @return parent allocator
168 */
169 BufferAllocator getParentAllocator();
170
171 /**
172 * Returns the set of child allocators.
173 *
174 * @return set of child allocators
175 */
176 Collection<BufferAllocator> getChildAllocators();
177
178 /**
179 * Create an allocation reservation. A reservation is a way of building up
180 * a request for a buffer whose size is not known in advance. See
181 *
182 * @return the newly created reservation
183 * @see AllocationReservation
184 */
185 AllocationReservation newReservation();
186
187 /**
188 * Get a reference to the empty buffer associated with this allocator. Empty buffers are
189 * special because we don't
190 * worry about them leaking or managing reference counts on them since they don't actually
191 * point to any memory.
192 *
193 * @return the empty buffer
194 */
195 ArrowBuf getEmpty();
196
197 /**
198 * Return the name of this allocator. This is a human readable name that can help debugging.
199 * Typically provides
200 * coordinates about where this allocator was created
201 *
202 * @return the name of the allocator
203 */
204 String getName();
205
206 /**
207 * Return whether or not this allocator (or one if its parents) is over its limits. In the case
208 * that an allocator is
209 * over its limit, all consumers of that allocator should aggressively try to address the
210 * overlimit situation.
211 *
212 * @return whether or not this allocator (or one if its parents) is over its limits
213 */
214 boolean isOverLimit();
215
216 /**
217 * Return a verbose string describing this allocator. If in DEBUG mode, this will also include
218 * relevant stacktraces
219 * and historical logs for underlying objects
220 *
221 * @return A very verbose description of the allocator hierarchy.
222 */
223 String toVerboseString();
224
225 /**
226 * Asserts (using java assertions) that the provided allocator is currently open. If assertions
227 * are disabled, this is
228 * a no-op.
229 */
230 void assertOpen();
231
232 /**
233 * Gets the rounding policy of the allocator.
234 */
235 default RoundingPolicy getRoundingPolicy() {
236 return DefaultRoundingPolicy.DEFAULT_ROUNDING_POLICY;
237 }
238}