1 /* 2 * Copyright 2025 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.util.internal; 17 18 import java.nio.ByteBuffer; 19 20 /** 21 * Encapsulates a direct {@link ByteBuffer} and its mechanism for immediate deallocation, if any. 22 */ 23 public interface CleanableDirectBuffer { 24 /** 25 * Get the buffer instance. 26 * <p> 27 * Note: the buffer must not be accessed after the {@link #clean()} method has been called. 28 * 29 * @return The {@link ByteBuffer} instance. 30 */ 31 ByteBuffer buffer(); 32 33 /** 34 * Deallocate the buffer. This method can only be called once per instance, 35 * and all usages of the buffer must have ceased before this method is called, 36 * and the buffer must not be accessed again after this method has been called. 37 */ 38 void clean(); 39 40 /** 41 * @return {@code true} if the {@linkplain #memoryAddress() native memory address} is available, 42 * otherwise {@code false}. 43 */ 44 default boolean hasMemoryAddress() { 45 return false; 46 } 47 48 /** 49 * Get the native memory address, but only if {@link #hasMemoryAddress()} returns true, 50 * otherwise this may return an unspecified value or throw an exception. 51 * @return The native memory address of this buffer, if available. 52 */ 53 default long memoryAddress() { 54 return 0; 55 } 56 }