|
| 1 | +/* |
| 2 | + * Copyright 2021 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.buffer.api; |
| 17 | + |
| 18 | +import java.util.ServiceLoader; |
| 19 | +import java.util.function.Supplier; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +/** |
| 23 | + * The MemoryManagers interface is the handle through which {@link BufferAllocator buffer allocators} access the low |
| 24 | + * level memory management APIs. |
| 25 | + * <p> |
| 26 | + * This is hidden behind this interface in order to make allocation and pool agnostic and reusable across buffer and |
| 27 | + * memory implementations. |
| 28 | + */ |
| 29 | +public interface MemoryManagers { |
| 30 | + /** |
| 31 | + * Get the default, or currently configured, memory managers instance. |
| 32 | + * @return A MemoryManagers instance. |
| 33 | + */ |
| 34 | + static MemoryManagers getManagers() { |
| 35 | + return MemoryManagersOverride.getManagers(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Temporarily override the default configured memory managers instance. |
| 40 | + * <p> |
| 41 | + * Calls to {@link #getManagers()} from within the given supplier will get the given managers instance. |
| 42 | + * |
| 43 | + * @param managers Override the default configured managers instance with this instance. |
| 44 | + * @param supplier The supplier function to be called while the override is in place. |
| 45 | + * @param <T> The result type from the supplier. |
| 46 | + * @return The result from the supplier. |
| 47 | + */ |
| 48 | + static <T> T using(MemoryManagers managers, Supplier<T> supplier) { |
| 49 | + return MemoryManagersOverride.using(managers, supplier); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get a lazy-loading stream of all available memory managers. |
| 54 | + * |
| 55 | + * @return A stream of providers of memory managers instances. |
| 56 | + */ |
| 57 | + static Stream<ServiceLoader.Provider<MemoryManagers>> getAllManagers() { |
| 58 | + var loader = ServiceLoader.load(MemoryManagers.class); |
| 59 | + return loader.stream(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get a {@link MemoryManager} instance that is suitable for allocating on-heap {@link Buffer} instances. |
| 64 | + * |
| 65 | + * @return An on-heap {@link MemoryManager}. |
| 66 | + */ |
| 67 | + MemoryManager getHeapMemoryManager(); |
| 68 | + |
| 69 | + /** |
| 70 | + * Get a {@link MemoryManager} instance that is suitable for allocating off-heap {@link Buffer} instances. |
| 71 | + * |
| 72 | + * @return An off-heap {@link MemoryManager}. |
| 73 | + */ |
| 74 | + MemoryManager getNativeMemoryManager(); |
| 75 | +} |
0 commit comments