KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: IoSocketDispatcherPool.java 1301 2007-06-01 15:52:16Z 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
25 import java.nio.ByteBuffer JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29
30
31 /**
32  * Single thread memory manager
33  *
34  *
35  * @author grro@xsocket.org
36  */

37 final class UnsynchronizedMemoryManager implements IMemoryManager {
38         
39     private static final Logger JavaDoc LOG = Logger.getLogger(UnsynchronizedMemoryManager.class.getName());
40     
41     private ByteBuffer JavaDoc freeBuffer = null;
42     
43     private boolean useDirectMemory = false;
44     private int preallocationSize = 65536;
45     
46     
47     /**
48      * constructor
49      *
50      * @p
51      * @param useDirectMemory true, if direct memory should be used
52      */

53     UnsynchronizedMemoryManager(int preallocationSize, boolean useDirectMemory) {
54         this.preallocationSize = preallocationSize;
55         this.useDirectMemory = useDirectMemory;
56     }
57     
58     /**
59      * {@inheritDoc}
60      */

61     public int getCurrentPreallocationBufferSize() {
62         ByteBuffer JavaDoc b = freeBuffer;
63         if (b == null) {
64             return 0;
65         } else {
66             return b.remaining();
67         }
68     }
69             
70
71     /**
72      * {@inheritDoc}
73      */

74     public void recycleMemory(ByteBuffer JavaDoc buffer, int minSize) {
75         int remaining = buffer.remaining();
76         if (remaining >= minSize) {
77             freeBuffer = buffer;
78         }
79     }
80         
81     
82     /**
83      * {@inheritDoc}
84      */

85     public final ByteBuffer JavaDoc acquireMemory(int minSize) {
86         preallocate(minSize);
87
88         ByteBuffer JavaDoc buffer = freeBuffer;
89         freeBuffer = null;
90
91         return buffer;
92     }
93     
94
95     /**
96      * {@inheritDoc}
97      */

98     public void preallocate(int minSize) {
99
100         // sufficient size?
101
if (freeBuffer != null) {
102             if (freeBuffer.remaining() >= minSize) {
103                 return;
104             }
105         }
106             
107         // no, allocate new
108
freeBuffer = newBuffer(preallocationSize);
109     }
110     
111         
112     /**
113      * {@inheritDoc}
114      */

115     private ByteBuffer JavaDoc newBuffer(int preallocationSize) {
116         if (LOG.isLoggable(Level.FINE)) {
117             LOG.fine("allocate new physical memory (size: " + preallocationSize + ") by thread " + Thread.currentThread().getName());
118         }
119     
120         if (useDirectMemory) {
121             return ByteBuffer.allocateDirect(preallocationSize);
122         } else {
123             return ByteBuffer.allocate(preallocationSize);
124         }
125     }
126
127     /**
128      * {@inheritDoc}
129      */

130     public int getFreeBufferSize() {
131         if (freeBuffer != null) {
132             return freeBuffer.remaining();
133         } else {
134             return 0;
135         }
136     }
137 }
138
Popular Tags