src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java
branchhttp-client-branch
changeset 56481 247ed0848e48
parent 56477 46c04919ee5c
child 56501 3a8c9583ceda
--- a/src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java	Tue Apr 24 19:45:20 2018 +0100
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java	Wed Apr 25 19:51:58 2018 +0100
@@ -1198,7 +1198,7 @@
         private final ByteBuffer[] pool = new ByteBuffer[POOL_SIZE];
         private final HttpClientImpl client;
         private final Logger debug;
-        int tail; // no need for volatile: only accessed in SM thread.
+        private int tail, count; // no need for volatile: only accessed in SM thread.
         public SSLDirectBufferSupplier(HttpClientImpl client) {
             this.client = Objects.requireNonNull(client);
             this.debug = client.debug;
@@ -1208,18 +1208,20 @@
         @Override
         public ByteBuffer get() {
             assert client.isSelectorThread();
-            ByteBuffer buf = tail == 0 ? null : pool[--tail];
-            if (buf == null) {
+            assert tail <= POOL_SIZE : "allocate tail is " + tail;
+            ByteBuffer buf;
+            if (tail == 0) {
                 if (debug.on()) {
                     // should not appear more than SocketTube.MAX_BUFFERS
                     debug.log("ByteBuffer.allocateDirect(%d)", Utils.BUFSIZE);
                 }
+                assert count++ < POOL_SIZE : "trying to allocate more than "
+                            + POOL_SIZE + " buffers";
                 buf = ByteBuffer.allocateDirect(Utils.BUFSIZE);
             } else {
-                // if (debug.on()) { // this trace is mostly noise.
-                //    debug.log("ByteBuffer.recycle(%d)", buf.remaining());
-                // }
-                assert buf == pool[tail];
+                assert tail > 0 : "non positive tail value: " + tail;
+                tail--;
+                buf = pool[tail];
                 pool[tail] = null;
             }
             assert buf.isDirect();
@@ -1237,9 +1239,15 @@
             assert client.isSelectorThread();
             assert buffer.isDirect();
             assert !buffer.hasRemaining();
+            assert tail < POOL_SIZE : "recycle tail is " + tail;
+            assert tail >= 0;
             buffer.position(0);
             buffer.limit(buffer.capacity());
-            pool[tail++] = buffer;
+            // don't fail if assertions are off. we have asserted above.
+            if (tail < POOL_SIZE) {
+                pool[tail] = buffer;
+                tail++;
+            }
             assert tail <= POOL_SIZE;
             assert tail > 0;
         }