Bill commented on a change in pull request #5525:
URL: https://github.com/apache/geode/pull/5525#discussion_r491018765



##########
File path: 
geode-core/src/main/java/org/apache/geode/internal/net/BufferPool.java
##########
@@ -295,19 +302,48 @@ void releaseBuffer(BufferPool.BufferType type, ByteBuffer 
buffer) {
   /**
    * Releases a previously acquired buffer.
    */
-  private void releaseBuffer(ByteBuffer bb, boolean send) {
-    if (bb.isDirect()) {
-      BBSoftReference bbRef = new BBSoftReference(bb, send);
-      if (bb.capacity() <= SMALL_BUFFER_SIZE) {
+  private void releaseBuffer(ByteBuffer buffer, boolean send) {
+    if (buffer.isDirect()) {
+      buffer = getPoolableBuffer(buffer);
+      BBSoftReference bbRef = new BBSoftReference(buffer, send);
+      if (buffer.capacity() <= SMALL_BUFFER_SIZE) {
         bufferSmallQueue.offer(bbRef);
-      } else if (bb.capacity() <= MEDIUM_BUFFER_SIZE) {
+      } else if (buffer.capacity() <= MEDIUM_BUFFER_SIZE) {
         bufferMiddleQueue.offer(bbRef);
       } else {
         bufferLargeQueue.offer(bbRef);
       }
     } else {
-      updateBufferStats(-bb.capacity(), send, false);
+      updateBufferStats(-buffer.capacity(), send, false);
+    }
+  }
+
+  /**
+   * If we hand out a buffer that is larger than the requested size we create a
+   * "slice" of the buffer having the requested capacity and hand that out 
instead.
+   * When we put the buffer back in the pool we need to find the original, 
non-sliced,
+   * buffer. This is held in DirectBuffer in its "attachment" field, which is 
a public
+   * method, though DirectBuffer is package-private.
+   */
+  @VisibleForTesting
+  public ByteBuffer getPoolableBuffer(ByteBuffer buffer) {

Review comment:
       That’s fine. But I did want to run it by you.
   
   Subclassing from a byte buffer class would entail making large (lots of 
methods), new classes huh?
   
   The alternative would be to return something that is not a byte buffer, but 
rather has-a byte buffer. In that case things that call our `BufferPool` 
methods would get one of those (non-byte-buffer) things, and that would perturb 
lots of calling code.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to