View Javadoc
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.netty5.buffer.api.pool;
17  
18  import io.netty5.buffer.api.AllocationType;
19  import io.netty5.buffer.api.Buffer;
20  import io.netty5.buffer.api.Drop;
21  import io.netty5.buffer.api.MemoryManager;
22  import io.netty5.buffer.api.internal.DropCaptor;
23  
24  import static io.netty5.buffer.api.internal.Statics.standardDrop;
25  
26  @SuppressWarnings("unchecked")
27  class UnpooledUntetheredMemory implements UntetheredMemory {
28      private final MemoryManager manager;
29      private final Buffer buffer;
30      private final DropCaptor<Buffer> dropCaptor;
31  
32      UnpooledUntetheredMemory(PooledBufferAllocator allocator, MemoryManager manager,
33                               AllocationType allocationType, int size) {
34          this.manager = manager;
35          dropCaptor = new DropCaptor<>();
36          buffer = manager.allocateShared(allocator.getPooledAllocatorControl(), size, dropCaptor, allocationType);
37      }
38  
39      @Override
40      public <Memory> Memory memory() {
41          return (Memory) manager.unwrapRecoverableMemory(buffer);
42      }
43  
44      @Override
45      public <BufferType extends Buffer> Drop<BufferType> drop() {
46          return (Drop<BufferType>) standardDrop(manager).apply(dropCaptor.getDrop());
47      }
48  }