Author: mturk
Date: Fri Sep 4 09:26:54 2009
New Revision: 811319
URL: http://svn.apache.org/viewvc?rev=811319&view=rev
Log:
Add mapped regions vector
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java?rev=811319&r1=811318&r2=811319&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java
Fri Sep 4 09:26:54 2009
@@ -16,6 +16,8 @@
package org.apache.commons.runtime;
import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Vector;
import org.apache.commons.runtime.io.File;
import org.apache.commons.runtime.io.Status;
import org.apache.commons.runtime.exception.ClosedDescriptorException;
@@ -30,7 +32,7 @@
*
* @since Runtime 1.0
*/
-public final class SharedMemory
+public final class SharedMemory implements java.io.Closeable
{
/*
@@ -38,6 +40,13 @@
*/
private Descriptor shm;
+ /*
+ * List of all mapped regions to this mapped file.
+ * When the MemoryMappedFile is close all Pointer
+ * objects are invalidated
+ */
+ private Vector<Pointer> mappedRegions = new Vector<Pointer>(2);
+
private SharedMemory()
{
// No Instance
@@ -188,12 +197,36 @@
public void close()
throws IOException, SecurityException, ClosedDescriptorException
{
+ Throwable ce = null;
+ for (Enumeration<Pointer> e = mappedRegions.elements();
e.hasMoreElements();) {
+ try {
+ Pointer ptr = e.nextElement();
+ /* Check if we already unmap the given Pointer.
+ * Closing Pointers directly is bad prectice because
+ * the references Vector will continue to hold the
+ * link to this Pointer.
+ */
+ if (!ptr.isNull()) {
+ ptr.free();
+ }
+ } catch (Throwable te) {
+ if (ce == null)
+ ce = te;
+ }
+ }
+ mappedRegions.clear();
if (shm.valid()) {
close0(shm);
}
else {
throw new ClosedDescriptorException();
}
+ if (ce != null) {
+ /* One of the pointers has thrown an Exception.
+ * Rethrow as IOException
+ */
+ throw new IOException(ce.getMessage());
+ }
}
private static native long size0(Descriptor shm)
@@ -227,7 +260,9 @@
throws IOException, ClosedDescriptorException
{
if (shm.valid()) {
- return map0(shm);
+ Pointer p = map0(shm);
+ mappedRegions.add(p);
+ return p;
}
else {
throw new ClosedDescriptorException();
@@ -251,7 +286,9 @@
throws IOException, IndexOutOfBoundsException,
ClosedDescriptorException
{
if (shm.valid()) {
- return map1(shm, offset, size);
+ Pointer p = map1(shm, offset, size);
+ mappedRegions.add(p);
+ return p;
}
else {
throw new ClosedDescriptorException();