001: /*
002: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.spi;
022:
023: import java.nio.ByteBuffer;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import org.xsocket.DataConverter;
028:
029: /**
030: * Single thread memory manager
031: *
032: *
033: * @author grro@xsocket.org
034: */
035: final class UnsynchronizedMemoryManager extends AbstractMemoryManager {
036:
037: private static final Logger LOG = Logger
038: .getLogger(UnsynchronizedMemoryManager.class.getName());
039:
040: private ByteBuffer freeBuffer = null;
041:
042: /**
043: * constructor
044: *
045: * @param allocationSize the buffer to allocate
046: * @param preallocate true, if buffer should be preallocated
047: * @param minPreallocatedBufferSize the minimal buffer size
048: * @param useDirectMemory true, if direct memory should be used
049: */
050: private UnsynchronizedMemoryManager(int preallocationSize,
051: boolean preallocate, int minPreallocatedBufferSize,
052: boolean useDirectMemory) {
053: super (preallocationSize, preallocate,
054: minPreallocatedBufferSize, useDirectMemory);
055: }
056:
057: public static UnsynchronizedMemoryManager createPreallocatedMemoryManager(
058: int preallocationSize, int minBufferSze,
059: boolean useDirectMemory) {
060: return new UnsynchronizedMemoryManager(preallocationSize, true,
061: minBufferSze, useDirectMemory);
062: }
063:
064: public static UnsynchronizedMemoryManager createNonPreallocatedMemoryManager(
065: boolean useDirectMemory) {
066: return new UnsynchronizedMemoryManager(0, false, 1,
067: useDirectMemory);
068: }
069:
070: /**
071: * {@inheritDoc}
072: */
073: public int getCurrentSizePreallocatedBuffer() {
074: if (freeBuffer != null) {
075: return freeBuffer.remaining();
076: } else {
077: return 0;
078: }
079: }
080:
081: /**
082: * {@inheritDoc}
083: */
084: public void recycleMemory(ByteBuffer buffer) {
085:
086: // preallocate mode?
087: if (isPreallocationMode()) {
088: if (buffer.remaining() >= getPreallocatedMinBufferSize()) {
089: if (LOG.isLoggable(Level.FINE)) {
090: LOG.fine("recycling "
091: + DataConverter.toFormatedBytesSize(buffer
092: .remaining()));
093: }
094: freeBuffer = buffer;
095: }
096: }
097: }
098:
099: /**
100: * {@inheritDoc}
101: */
102: public ByteBuffer acquireMemoryStandardSizeOrPreallocated(
103: int standardSize) {
104: if (isPreallocationMode()) {
105: preallocate();
106: } else {
107: freeBuffer = newBuffer(standardSize);
108: }
109:
110: ByteBuffer buffer = freeBuffer;
111: freeBuffer = null;
112:
113: return buffer;
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: public void preallocate() {
120: if (isPreallocationMode()) {
121:
122: // sufficient size?
123: if (freeBuffer != null) {
124: if (freeBuffer.remaining() >= getPreallocatedMinBufferSize()) {
125: return;
126: }
127: }
128:
129: // no, allocate new
130: freeBuffer = newBuffer(gettPreallocationBufferSize());
131: }
132: }
133:
134: }
|