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;
17  
18  import io.netty5.buffer.api.internal.Statics;
19  import io.netty5.buffer.api.internal.WrappingAllocation;
20  
21  import java.nio.charset.Charset;
22  import java.util.function.Supplier;
23  
24  import static io.netty5.buffer.api.internal.Statics.allocatorClosedException;
25  import static io.netty5.buffer.api.internal.Statics.standardDrop;
26  
27  class ManagedBufferAllocator implements BufferAllocator, AllocatorControl {
28      private final MemoryManager manager;
29      private final AllocationType allocationType;
30      private volatile boolean closed;
31  
32      ManagedBufferAllocator(MemoryManager manager, boolean direct) {
33          this.manager = manager;
34          allocationType = direct? StandardAllocationTypes.OFF_HEAP : StandardAllocationTypes.ON_HEAP;
35      }
36  
37      @Override
38      public boolean isPooling() {
39          return false;
40      }
41  
42      @Override
43      public AllocationType getAllocationType() {
44          return allocationType;
45      }
46  
47      @Override
48      public Buffer allocate(int size) {
49          if (closed) {
50              throw allocatorClosedException();
51          }
52          Statics.assertValidBufferSize(size);
53          return manager.allocateShared(this, size, standardDrop(manager), allocationType);
54      }
55  
56      @Override
57      public Supplier<Buffer> constBufferSupplier(byte[] bytes) {
58          if (closed) {
59              throw allocatorClosedException();
60          }
61          Buffer constantBuffer = manager.allocateShared(
62                  this, bytes.length, standardDrop(manager), allocationType);
63          constantBuffer.writeBytes(bytes).makeReadOnly();
64          return () -> manager.allocateConstChild(constantBuffer);
65      }
66  
67      @Override
68      public Buffer copyOf(String str, Charset charset) {
69          if (!allocationType.isDirect()) {
70              // For on-heap buffers we can optimise a bit, and allocate with just one copy operation.
71              byte[] bytes = str.getBytes(charset);
72              // We use a wrapping allocation type, because the byte array is guaranteed by String to be un-aliased.
73              WrappingAllocation allocation = new WrappingAllocation(bytes);
74              Buffer buffer = manager.allocateShared(this, bytes.length, standardDrop(manager), allocation);
75              return buffer.writerOffset(bytes.length);
76          }
77          return BufferAllocator.super.copyOf(str, charset);
78      }
79  
80      @Override
81      public void close() {
82          closed = true;
83      }
84  
85      @Override
86      public BufferAllocator getAllocator() {
87          return this;
88      }
89  }