Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 1b65bf9

Browse files
committed
Make the incubating buffers exposable as ByteBuf
Motivation: This makes it possible to use the new buffer API in Netty as is. Modification: Make the MemSegBuffer implementation class implement AsByteBuf and ReferenceCounted. The produced ByteBuf instance delegates all calls to the underlying Buffer instance as faithfully as possible. One area where the two deviates, is that it's not possible to create non-retained duplicates and slices with the new buffer API. Result: It is now possible to use the new buffer API on both client and server side. The Echo* examples demonstrate this, and the EchoIT proves it with a test. The API is used more directly on the client side, since the server-side allocator in Netty does not know how to allocate buffers with the incubating API.
1 parent f14a779 commit 1b65bf9

File tree

14 files changed

+2023
-1
lines changed

14 files changed

+2023
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@
394394
<version>${netty.build.version}</version>
395395
<scope>test</scope>
396396
</dependency>
397+
<dependency>
398+
<groupId>io.netty</groupId>
399+
<artifactId>netty-handler</artifactId>
400+
<version>${netty.version}</version>
401+
<scope>test</scope>
402+
</dependency>
397403
<dependency>
398404
<groupId>org.openjdk.jmh</groupId>
399405
<artifactId>jmh-core</artifactId>

src/main/java/io/netty/buffer/api/BufferHolder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,9 @@ protected final Buffer getBuf() {
190190
protected final Buffer getBufVolatile() {
191191
return (Buffer) BUF.getVolatile(this);
192192
}
193+
194+
@Override
195+
public boolean isAccessible() {
196+
return buf.isAccessible();
197+
}
193198
}

src/main/java/io/netty/buffer/api/Rc.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,13 @@ default boolean isInstanceOf(Class<?> cls) {
8787
* @return The number of borrows, if any, of this object.
8888
*/
8989
int countBorrows();
90+
91+
/**
92+
* Check if this object is accessible.
93+
*
94+
* @return {@code true} if this object is still valid and can be accessed,
95+
* otherwise {@code false} if, for instance, this object has been dropped/deallocated,
96+
* or been {@linkplain #send() sent} elsewhere.
97+
*/
98+
boolean isAccessible();
9099
}

src/main/java/io/netty/buffer/api/RcSupport.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public int countBorrows() {
102102
return Math.max(acquires, 0);
103103
}
104104

105+
@Override
106+
public boolean isAccessible() {
107+
return acquires >= 0;
108+
}
109+
105110
/**
106111
* Prepare this instance for ownsership transfer. This method is called from {@link #send()} in the sending thread.
107112
* This method should put this Rc in a deactivated state where it is no longer accessible from the currently owning
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.netty.buffer.api.adaptor;
17+
18+
import io.netty.buffer.ByteBufConvertible;
19+
import io.netty.util.ReferenceCounted;
20+
21+
/**
22+
* Interfaces that are required for an object to stand-in for a {@link io.netty.buffer.ByteBuf} in Netty.
23+
*/
24+
public interface BufferIntegratable extends ByteBufConvertible, ReferenceCounted {
25+
}

0 commit comments

Comments
 (0)