95709828bf
Motivation: We need a new implementation of our new API that supports Java 11, since that is what Netty 5 will most likely baseline on. We also need an implementation that does not rely on Unsafe. This leaves us with ByteBuffer as the underlying currency of memory. Modification: - Add a NioBuffer implementation and associated supporting classes. - The entry-point for this is a new MemoryManagers API, which is used to pick the implementation and provide the on-/off-heap MemoryManager implementations. - Add a mechanism to configure/override which MemoryManagers implementation to use. - The MemoryManagers implementations are service-loadable, so new ones can be discovered at runtime. - The existing MemorySegment based implementation also get a MemoryManagers implementation. - Expand the BufferTest to include all combinations of all implementations. We now run 360.000 tests in BufferTest. - Some common infrastructure, like ArcDrop, is moved to its own package. - Add a module-info.java to control the service loading, and the visibility in the various packages. - Some pom.xml file updates to support our now module based project. Result: We have an implementation that should work on Java 11, but we currently don't build or test on 11. More work needs to happen before that is a reality.
75 lines
2.6 KiB
Java
75 lines
2.6 KiB
Java
/*
|
|
* Copyright 2020 The Netty Project
|
|
*
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
* with the License. You may obtain a copy of the License at:
|
|
*
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
package io.netty.buffer.api.memseg;
|
|
|
|
import io.netty.buffer.api.AllocatorControl;
|
|
import io.netty.buffer.api.Buffer;
|
|
import io.netty.buffer.api.Drop;
|
|
import io.netty.buffer.api.MemoryManager;
|
|
import io.netty.buffer.api.memseg.MemSegBuffer.RecoverableMemory;
|
|
import jdk.incubator.foreign.MemorySegment;
|
|
|
|
import java.lang.ref.Cleaner;
|
|
|
|
import static io.netty.buffer.api.internal.Statics.convert;
|
|
|
|
public abstract class AbstractMemorySegmentManager implements MemoryManager {
|
|
@Override
|
|
public abstract boolean isNative();
|
|
|
|
@Override
|
|
public Buffer allocateConfined(AllocatorControl allocatorControl, long size, Drop<Buffer> drop, Cleaner cleaner) {
|
|
var segment = createSegment(size);
|
|
if (cleaner != null) {
|
|
segment = segment.registerCleaner(cleaner);
|
|
}
|
|
return new MemSegBuffer(segment, segment, convert(drop), allocatorControl);
|
|
}
|
|
|
|
@Override
|
|
public Buffer allocateShared(AllocatorControl allocatorControl, long size, Drop<Buffer> drop, Cleaner cleaner) {
|
|
var segment = createSegment(size).share();
|
|
if (cleaner != null) {
|
|
segment = segment.registerCleaner(cleaner);
|
|
}
|
|
return new MemSegBuffer(segment, segment, convert(drop), allocatorControl);
|
|
}
|
|
|
|
protected abstract MemorySegment createSegment(long size);
|
|
|
|
@Override
|
|
public Drop<Buffer> drop() {
|
|
return convert(MemSegBuffer.SEGMENT_CLOSE);
|
|
}
|
|
|
|
@Override
|
|
public Object unwrapRecoverableMemory(Buffer buf) {
|
|
var b = (MemSegBuffer) buf;
|
|
return b.recoverableMemory();
|
|
}
|
|
|
|
@Override
|
|
public int capacityOfRecoverableMemory(Object memory) {
|
|
return ((RecoverableMemory) memory).capacity();
|
|
}
|
|
|
|
@Override
|
|
public Buffer recoverMemory(AllocatorControl allocatorControl, Object recoverableMemory, Drop<Buffer> drop) {
|
|
var recovery = (RecoverableMemory) recoverableMemory; // TODO get rid of RecoverableMemory
|
|
return recovery.recover(convert(drop));
|
|
}
|
|
}
|