KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > datagram > MemoryManager


1 // $Id: MemoryManager.java 910 2007-02-12 16:56:19Z 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.datagram;
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 MemoryManager {
40     
41     private static final Logger JavaDoc LOG = Logger.getLogger(MemoryManager.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 = 4096;
48         
49     
50     /**
51      * constructor
52      *
53      * @param preallocationSize the preallocation size
54      * @param useDirectMemory true, if direct memory should be used
55      */

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

67     public final synchronized int getFreeBufferSize() {
68         int size = 0;
69         for (SoftReference JavaDoc<ByteBuffer JavaDoc> bufferRef: memoryBuffer) {
70             ByteBuffer JavaDoc buffer = bufferRef.get();
71             if (buffer != null) {
72                 size += buffer.remaining();
73             }
74         }
75         return size;
76     }
77             
78     
79
80     private void recycleMemory(ByteBuffer JavaDoc buffer) {
81         if (buffer.hasRemaining()) {
82             memoryBuffer.add(new SoftReference JavaDoc<ByteBuffer JavaDoc>(buffer.slice()));
83         }
84     }
85
86             
87     /**
88      * aquires free memory
89      *
90      * @param minSize the min size of the aquired memory
91      */

92     public final synchronized ByteBuffer JavaDoc acquireMemory(int size) {
93         ByteBuffer JavaDoc buffer = null;
94         
95         if (!memoryBuffer.isEmpty()) {
96             SoftReference JavaDoc<ByteBuffer JavaDoc> freeBuffer = memoryBuffer.remove(0);
97             buffer = freeBuffer.get();
98
99             if (buffer != null) {
100                 // size sufficient?
101
if (buffer.limit() < size) {
102                     buffer = null;
103                 }
104             }
105         }
106                     
107                 
108         if (buffer == null) {
109             int allocationSize = getPreallocationSize();
110             if (getPreallocationSize() < allocationSize) {
111                 allocationSize = size * 4;
112             }
113                             
114             buffer = newBuffer(size);
115         }
116         
117         int savedLimit = buffer.limit();
118         
119         buffer.limit(size);
120         ByteBuffer JavaDoc result = buffer.slice();
121         
122         buffer.position(size);
123         buffer.limit(savedLimit);
124         ByteBuffer JavaDoc remaining = buffer.slice();
125         recycleMemory(remaining);
126         
127         return result;
128     }
129     
130     
131     /**
132      * return the preallocation size
133      *
134      * @return the preallocation size
135      */

136     int getPreallocationSize() {
137         return preallocationSize;
138     }
139     
140     
141     
142     private final ByteBuffer JavaDoc newBuffer(int size) {
143         if (useDirectMemory) {
144             if (LOG.isLoggable(Level.FINE)) {
145                 LOG.fine("allocating " + DataConverter.toFormatedBytesSize(size) + " direct memory");
146             }
147
148             return ByteBuffer.allocateDirect(size);
149
150         } else {
151             if (LOG.isLoggable(Level.FINE)) {
152                 LOG.fine("allocating " + DataConverter.toFormatedBytesSize(size) + " heap memory");
153             }
154
155             return ByteBuffer.allocate(size);
156         }
157     }
158 }
Popular Tags