1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
71 byte[] bytes = str.getBytes(charset);
72
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 }