KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > io > impl > SynchronizedMemoryManager


1 // $Id: MemoryManager.java 1304 2007-06-02 13:26:34Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream.io.impl;
23
24 import java.lang.ref.SoftReference JavaDoc;
25 import java.nio.ByteBuffer JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import org.xsocket.DataConverter;
32
33
34 /**
35  * a Memory Manager implementation
36  *
37  * @author grro@xsocket.org
38  */

39 class SynchronizedMemoryManager implements IMemoryManager {
40     
41     private static final Logger JavaDoc LOG = Logger.getLogger(SynchronizedMemoryManager.class.getName());
42     
43         
44     private List JavaDoc<SoftReference JavaDoc<ByteBuffer JavaDoc>> memoryBuffer = new ArrayList JavaDoc<SoftReference JavaDoc<ByteBuffer JavaDoc>>();
45
46     private boolean useDirectMemory = false;
47     private int preallocationSize = 65536;
48         
49     
50     /**
51      * constructor
52      *
53      * @param preallocationSize the preallocation size
54      * @param useDirectMemory true, if direct memory should be used
55      */

56     SynchronizedMemoryManager(int preallocationSize, boolean useDirectMemory) {
57         this.preallocationSize = preallocationSize;
58         this.useDirectMemory = useDirectMemory;
59     }
60     
61     
62     
63     /**
64      * return the free memory size
65      *
66      * @return the free memory size
67      */

68     public final synchronized int getFreeBufferSize() {
69         int size = 0;
70         for (SoftReference JavaDoc<ByteBuffer JavaDoc> bufferRef: memoryBuffer) {
71             ByteBuffer JavaDoc buffer = bufferRef.get();
72             if (buffer != null) {
73                 size += buffer.remaining();
74             }
75         }
76         return size;
77     }
78             
79     
80     /**
81      * recycle free memory
82      *
83      * @param buffer the buffer to recycle
84      */

85     public void recycleMemory(ByteBuffer JavaDoc buffer, int minSize) {
86         int remaining = buffer.remaining();
87         if (remaining >= minSize) {
88             memoryBuffer.add(new SoftReference JavaDoc<ByteBuffer JavaDoc>(buffer.slice()));
89         }
90     }
91     
92     public void preallocate(int minSize) {
93         
94     }
95
96             
97     /**
98      * aquire free memory
99      *
100      * @param minSize the min size of the aquired memory
101      */

102     public final synchronized ByteBuffer JavaDoc acquireMemory(int minSize) {
103         ByteBuffer JavaDoc buffer = null;
104                     
105         if (!memoryBuffer.isEmpty()) {
106             SoftReference JavaDoc<ByteBuffer JavaDoc> freeBuffer = memoryBuffer.remove(0);
107             buffer = freeBuffer.get();
108
109             if (buffer != null) {
110                 // size sufficient?
111
if (buffer.limit() < minSize) {
112                     buffer = null;
113                 }
114             }
115         }
116                     
117                 
118         if (buffer == null) {
119             int size = getPreallocationSize();
120             if (getPreallocationSize() < minSize) {
121                 size = minSize * 4;
122             }
123                 
124             
125             buffer = newBuffer(size);
126         }
127                     
128         return buffer;
129     }
130     
131     
132     /**
133      * return the preallocation size
134      *
135      * @return the preallocation size
136      */

137     public int getPreallocationSize() {
138         return preallocationSize;
139     }
140
141     /**
142      * set the preallocation size
143      * @param preallocationSize the preallocation size
144      */

145     public void setPreallocationSize(int preallocationSize) {
146         this.preallocationSize = preallocationSize;
147     }
148     
149     
150     
151
152     
153     
154     private final ByteBuffer JavaDoc newBuffer(int size) {
155         if (useDirectMemory) {
156             if (LOG.isLoggable(Level.FINE)) {
157                 LOG.fine("allocating " + DataConverter.toFormatedBytesSize(size) + " direct memory");
158             }
159
160             return ByteBuffer.allocateDirect(size);
161
162         } else {
163             if (LOG.isLoggable(Level.FINE)) {
164                 LOG.fine("allocating " + DataConverter.toFormatedBytesSize(size) + " heap memory");
165             }
166
167             return ByteBuffer.allocate(size);
168         }
169     }
170
171
172 }
Popular Tags