1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.buffer.api.bytebuffer;
17
18 import io.netty5.buffer.api.AllocationType;
19 import io.netty5.buffer.api.AllocatorControl;
20 import io.netty5.buffer.api.Buffer;
21 import io.netty5.buffer.api.Drop;
22 import io.netty5.buffer.api.MemoryManager;
23 import io.netty5.buffer.api.StandardAllocationTypes;
24 import io.netty5.buffer.api.internal.Statics;
25 import io.netty5.buffer.api.internal.WrappingAllocation;
26
27 import java.nio.ByteBuffer;
28 import java.util.function.Function;
29
30 import static io.netty5.buffer.api.internal.Statics.bbslice;
31 import static io.netty5.buffer.api.internal.Statics.convert;
32
33
34
35
36
37
38
39
40
41 public final class ByteBufferMemoryManager implements MemoryManager {
42 @Override
43 public Buffer allocateShared(AllocatorControl allocatorControl, long size,
44 Function<Drop<Buffer>, Drop<Buffer>> dropDecorator,
45 AllocationType allocationType) {
46 int capacity = Math.toIntExact(size);
47 final ByteBuffer buffer;
48 if (allocationType == StandardAllocationTypes.OFF_HEAP) {
49 buffer = ByteBuffer.allocateDirect(capacity);
50 } else if (allocationType == StandardAllocationTypes.ON_HEAP) {
51 buffer = ByteBuffer.allocate(capacity);
52 } else if (allocationType instanceof WrappingAllocation) {
53 buffer = ByteBuffer.wrap(((WrappingAllocation) allocationType).getArray());
54 } else {
55 throw new IllegalArgumentException("Unknown allocation type: " + allocationType);
56 }
57 return createBuffer(buffer, allocatorControl, dropDecorator.apply(drop()));
58 }
59
60 @Override
61 public Buffer allocateConstChild(Buffer readOnlyConstParent) {
62 NioBuffer buf = (NioBuffer) readOnlyConstParent;
63 return buf.newConstChild();
64 }
65
66 private static Drop<Buffer> drop() {
67 return Statics.NO_OP_DROP;
68 }
69
70 @Override
71 public Object unwrapRecoverableMemory(Buffer buf) {
72 return ((NioBuffer) buf).recoverable();
73 }
74
75 @Override
76 public Buffer recoverMemory(AllocatorControl allocatorControl, Object recoverableMemory, Drop<Buffer> drop) {
77 ByteBuffer memory = (ByteBuffer) recoverableMemory;
78 return createBuffer(memory, allocatorControl, drop);
79 }
80
81 private static NioBuffer createBuffer(ByteBuffer memory, AllocatorControl allocatorControl, Drop<Buffer> drop) {
82 Drop<NioBuffer> concreteDrop = convert(drop);
83 NioBuffer nioBuffer = new NioBuffer(memory, memory, allocatorControl, concreteDrop);
84 concreteDrop.attach(nioBuffer);
85 return nioBuffer;
86 }
87
88 @Override
89 public Object sliceMemory(Object memory, int offset, int length) {
90 var buffer = (ByteBuffer) memory;
91 return bbslice(buffer, offset, length);
92 }
93
94 @Override
95 public void clearMemory(Object memory) {
96 ByteBuffer buffer = (ByteBuffer) memory;
97 Statics.setMemory(buffer, buffer.capacity(), (byte) 0);
98 }
99
100 @Override
101 public String implementationName() {
102 return "ByteBuffer";
103 }
104 }