src/share/classes/com/sun/corba/se/impl/transport/ByteBufferPoolImpl.java

Thu, 31 Aug 2017 18:10:36 +0800

author
aoqi
date
Thu, 31 Aug 2017 18:10:36 +0800
changeset 748
6845b95cba6b
parent 158
91006f157c46
parent 0
7ef37b2cdcad
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.corba.se.impl.transport;
aoqi@0 27
aoqi@0 28 import java.nio.ByteBuffer;
aoqi@0 29 import java.util.ArrayList;
aoqi@0 30
aoqi@0 31 import com.sun.corba.se.spi.orb.ORB;
aoqi@0 32
aoqi@0 33 import com.sun.corba.se.pept.transport.ByteBufferPool;
aoqi@0 34
aoqi@0 35 /**
aoqi@0 36 * @author Charlie Hunt
aoqi@0 37 */
aoqi@0 38
aoqi@0 39 public class ByteBufferPoolImpl implements ByteBufferPool
aoqi@0 40 {
aoqi@0 41 private ORB itsOrb;
aoqi@0 42 private int itsByteBufferSize;
aoqi@0 43 private ArrayList itsPool;
aoqi@0 44 private int itsObjectCounter = 0;
aoqi@0 45 private boolean debug;
aoqi@0 46
aoqi@0 47 // Construct a ByteBufferPool for a pool of NIO ByteBuffers
aoqi@0 48 // of ORB fragment size.
aoqi@0 49 public ByteBufferPoolImpl(ORB theORB)
aoqi@0 50 {
aoqi@0 51 itsByteBufferSize = theORB.getORBData().getGIOPFragmentSize();
aoqi@0 52 itsPool = new ArrayList();
aoqi@0 53 itsOrb = theORB;
aoqi@0 54 debug = theORB.transportDebugFlag;
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 /*
aoqi@0 58 * Locations where ByteBuffers are gotten from the pool:
aoqi@0 59 * 1. ContactInfoBase.createMessageMediator()
aoqi@0 60 * 2. ByteBufferWithInfo.growBuffer()
aoqi@0 61 * 3. ByteBufferWithInfo(ORB, BufferManagerWrite) - constructor
aoqi@0 62 */
aoqi@0 63
aoqi@0 64 // If the requested ByteBuffer size is less than or equal to
aoqi@0 65 // the ORB fragment size, and we have not disabled use of
aoqi@0 66 // direct byte buffers (normally for debugging purposes)
aoqi@0 67 // then get a DirectByteBuffer from the
aoqi@0 68 // pool if there is one, if there is not one in the pool,
aoqi@0 69 // then allocate a a DirectByteBuffer of ORB fragment size.
aoqi@0 70 //
aoqi@0 71 // If the request ByteBuffer size is greater than the ORB fragment
aoqi@0 72 // size, allocate a new non-direct ByteBuffer.
aoqi@0 73 public ByteBuffer getByteBuffer(int theAskSize)
aoqi@0 74 {
aoqi@0 75 ByteBuffer abb = null;
aoqi@0 76
aoqi@0 77 if ((theAskSize <= itsByteBufferSize) &&
aoqi@0 78 !itsOrb.getORBData().disableDirectByteBufferUse())
aoqi@0 79 {
aoqi@0 80 // check if there's one in the pool, if not allocate one.
aoqi@0 81 int poolSize;
aoqi@0 82 synchronized (itsPool)
aoqi@0 83 {
aoqi@0 84 poolSize = itsPool.size();
aoqi@0 85 if (poolSize > 0)
aoqi@0 86 {
aoqi@0 87 abb = (ByteBuffer)itsPool.remove(poolSize - 1);
aoqi@0 88
aoqi@0 89 // clear ByteBuffer before returning it
aoqi@0 90 abb.clear();
aoqi@0 91 }
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 // NOTE: Moved the 'else' part of the above if statement
aoqi@0 95 // outside the synchronized block since it is likely
aoqi@0 96 // less expensive to check poolSize than to allocate a
aoqi@0 97 // DirectByteBuffer in the synchronized block.
aoqi@0 98 if (poolSize <= 0)
aoqi@0 99 {
aoqi@0 100 abb = ByteBuffer.allocateDirect(itsByteBufferSize);
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 // increment the number of ByteBuffers gotten from pool
aoqi@0 104 // IMPORTANT: Since this counter is used only for information
aoqi@0 105 // purposes, it does not use synchronized access.
aoqi@0 106 itsObjectCounter++;
aoqi@0 107 }
aoqi@0 108 else
aoqi@0 109 {
aoqi@0 110 // Requested ByteBuffer size larger than the pool manages.
aoqi@0 111 // Just allocate a non-direct ByteBuffer
aoqi@0 112 abb = ByteBuffer.allocate(theAskSize);
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 return abb;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118
aoqi@0 119 /*
aoqi@0 120 * Locations where ByteBuffers are released to the pool:
aoqi@0 121 * 1. ByteBufferWithInfo.growBuffer()
aoqi@0 122 * 2. BufferManagerWriteCollect.sendMessage()
aoqi@0 123 * 3. CDROutputStream_1_0.close()
aoqi@0 124 * 4. CDRInputStream_1_0.close()
aoqi@0 125 * 5. BufferManagerReadStream.underflow()
aoqi@0 126 * 6. BufferManagerWrite.close()
aoqi@0 127 * 7. BufferManagerRead.close()
aoqi@0 128 * 8. CorbaMessageMediatorImpl.releaseByteBufferToPool()
aoqi@0 129 */
aoqi@0 130
aoqi@0 131 // If the ByteBuffer is a DirectByteBuffer, add it to the pool.
aoqi@0 132 // Otherwise, set its reference to null since it's not kept in
aoqi@0 133 // the pool and caller is saying he/she is done with it.
aoqi@0 134 // NOTE: The size of the ByteBuffer is not checked with the
aoqi@0 135 // this pool's ByteBuffer size since only DirectByteBuffers
aoqi@0 136 // ever allocated. Hence, only DirectByteBuffer are checked
aoqi@0 137 // here. An additional check could be added here for that though.
aoqi@0 138 public void releaseByteBuffer(ByteBuffer thebb)
aoqi@0 139 {
aoqi@0 140 if (thebb.isDirect())
aoqi@0 141 {
aoqi@0 142 synchronized (itsPool)
aoqi@0 143 {
aoqi@0 144 // use with debug to determine if byteBuffer is already
aoqi@0 145 // in the pool.
aoqi@0 146 boolean refInPool = false;
aoqi@0 147 int bbAddr = 0;
aoqi@0 148
aoqi@0 149 if (debug)
aoqi@0 150 {
aoqi@0 151 // Check to make sure we don't have 'thebb' reference
aoqi@0 152 // already in the pool before adding it.
aoqi@0 153
aoqi@0 154 for (int i = 0; i < itsPool.size() && refInPool == false; i++)
aoqi@0 155 {
aoqi@0 156 ByteBuffer tmpbb = (ByteBuffer)itsPool.get(i);
aoqi@0 157 if (thebb == tmpbb)
aoqi@0 158 {
aoqi@0 159 refInPool = true;
aoqi@0 160 bbAddr = System.identityHashCode(thebb);
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 // NOTE: The else part of this if will only get called
aoqi@0 167 // if debug = true and refInPool = true, see logic above.
aoqi@0 168 if (refInPool == false || debug == false)
aoqi@0 169 {
aoqi@0 170 // add ByteBuffer back to the pool
aoqi@0 171 itsPool.add(thebb);
aoqi@0 172 }
aoqi@0 173 else // otherwise, log a stack trace with duplicate message
aoqi@0 174 {
aoqi@0 175 String threadName = Thread.currentThread().getName();
aoqi@0 176 Throwable t =
aoqi@0 177 new Throwable(threadName +
aoqi@0 178 ": Duplicate ByteBuffer reference (" +
aoqi@0 179 bbAddr + ")");
aoqi@0 180 t.printStackTrace(System.out);
aoqi@0 181 }
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 // decrement the count of ByteBuffers released
aoqi@0 185 // IMPORTANT: Since this counter is used only for information
aoqi@0 186 // purposes, it does not use synchronized access.
aoqi@0 187 itsObjectCounter--;
aoqi@0 188 }
aoqi@0 189 else
aoqi@0 190 {
aoqi@0 191 // ByteBuffer not pooled nor needed
aoqi@0 192 thebb = null;
aoqi@0 193 }
aoqi@0 194 }
aoqi@0 195
aoqi@0 196
aoqi@0 197 // Get a count of the outstanding allocated DirectByteBuffers.
aoqi@0 198 // (Those allocated and have not been returned to the pool).
aoqi@0 199 // IMPORTANT: Since this counter is used only for information
aoqi@0 200 // purposes, it does not use synchronized access.
aoqi@0 201 public int activeCount()
aoqi@0 202 {
aoqi@0 203 return itsObjectCounter;
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 // End of file.

mercurial