Permalink
Browse files

Code cleanup and add comments

  • Loading branch information...
1 parent 5635aeb commit a598e033d6de6f8245c5e97358f3be17ea06cb29 @xerial committed Dec 10, 2013
View
@@ -4,7 +4,7 @@ all: larray
include Makefile.common
-SRC:=src/main/scala
+SRC:=larray-mmap/src/main/scala
LARRAY_OUT:=$(TARGET)/larray-$(os_arch)
LARRAY_SRC_DIR:=$(SRC)/xerial/larray/impl
@@ -14,7 +14,6 @@ LARRAY_OBJ:=$(LARRAY_OUT)/LArrayNative.o
VERSION:=$(shell perl -npe "s/version in ThisBuild\s+:=\s+\"(.*)\"/\1/" version.sbt | sed -e "/^$$/d")
-
CFLAGS:=$(CFLAGS) -I$(LARRAY_SRC_DIR)
LARRAY_HEADER:=$(SRC)/xerial/larray/impl/LArrayNative.h
@@ -28,17 +27,8 @@ $(LARRAY_HEADER): $(SRC)/xerial/larray/impl/LArrayNative.java $(TARGET)/classes
@mkdir -p $(TARGET)/classes
$(JAVAH) -classpath $(TARGET)/classes -o $@ xerial.larray.impl.LArrayNative
-bytecode: src/main/resources/xerial/larray/LArrayNativeLoader.bytecode
-
-src/main/resources/xerial/larray/LArrayNativeLoader.bytecode: src/main/resources/xerial/larray/LArrayNativeLoader.java
- @mkdir -p $(TARGET)/temp
- $(JAVAC) -source 1.5 -target 1.5 -d $(TARGET)/temp $<
- cp $(TARGET)/temp/xerial/larray/LArrayNativeLoader.class $@
-VERSION_FILE:=src/main/resources/xerial/larray/VERSION
-$(VERSION_FILE):
- echo "version=$(VERSION)" > $@
$(LARRAY_OUT)/%.o : $(LARRAY_SRC_DIR)/%.c
@@ -55,12 +45,12 @@ clean-native:
clean:
rm -rf $(TARGET)
-NATIVE_DIR:=src/main/resources/xerial/larray/native/$(OS_NAME)/$(OS_ARCH)
+NATIVE_DIR:=larray-mmap/src/main/resources/xerial/larray/native/$(OS_NAME)/$(OS_ARCH)
NATIVE_TARGET_DIR:=$(TARGET)/classes/xerial/native/$(OS_NAME)/$(OS_ARCH)
NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME)
native: osinfo $(NATIVE_DLL)
-larray: native $(VERSION_FILE) $(TARGET)/larray-$(VERSION).jar
+larray: native $(TARGET)/larray-$(VERSION).jar
SBT:=./sbt
View
@@ -1,5 +1,5 @@
-TARGET:=target
-SRC:=src/main/scala
+TARGET:=larray-mmap/target
+SRC:=larray-mmap/src/main/scala
ifndef JAVA_HOME
$(error Set JAVA_HOME environment variable)
@@ -1,6 +1,7 @@
package xerial.larray.buffer;
/**
+ * Allocated memory information
* @author Taro L. Saito
*/
interface Memory {
@@ -6,8 +6,22 @@
*/
public interface MemoryAllocator {
+ /**
+ * Allocate a memory of the specified byte length. The allocated memory must be released via `release`
+ * as in malloc() in C/C++.
+ * @param size byte length of the memory
+ * @return allocated memory information
+ */
Memory allocate(long size);
+
+ /**
+ * Release the memory allocated by allocate(size).
+ */
void release(Memory m);
+
+ /**
+ * Release the memory allocated by allocate(size).
+ */
void release(MemoryReference ref);
}
@@ -2,6 +2,7 @@
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
+import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
@@ -39,9 +40,18 @@ public long dataSize() {
}
+/**
+ * Phantom reference to the allocated memory that will be queued to the ReferenceQueue upon GC time
+ */
class MemoryReference extends PhantomReference<Memory> {
public final Long address;
+
+ /**
+ * Create a phantom reference
+ * @param m the allocated memory
+ * @param queue the reference queue to which GCed reference of the Memory will be put
+ */
public MemoryReference(Memory m, ReferenceQueue<Memory> queue) {
super(m, queue);
this.address = m.address();
@@ -73,7 +83,7 @@ public void run() {
}
}));
- // Start OffHeapMemory collector that releases the allocated memories when MemoryReference (phantom reference) is collected by GC.
+ // Start OffHeapMemory collector that releases the allocated memory when the corresponding Memory object is collected by GC.
Thread collector = new Thread(new Runnable() {
@Override
public void run() {
@@ -95,6 +105,9 @@ public void run() {
private AtomicLong totalAllocatedSize = new AtomicLong(0L);
+ /**
+ * Get the total amount of allocated memory
+ */
public long allocatedSize() { return totalAllocatedSize.get(); }
public Memory allocate(long size) {
@@ -119,6 +132,20 @@ public void release(MemoryReference ref) {
}
}
+ /**
+ * Release all memory addresses taken by this allocator.
+ * Be careful in using this method, since all of the memory addresses become invalid.
+ */
+ public void releaseAll() {
+ synchronized(this) {
+ Collection<MemoryReference> refSet = allocatedMemoryReferences.values();
+ for(MemoryReference ref : refSet) {
+ release(ref);
+ }
+ }
+ }
+
+
public void release(Memory m) {
long address = m.address();
if(allocatedMemoryReferences.containsKey(address)) {
@@ -1,59 +0,0 @@
-/*--------------------------------------------------------------------------
- * Copyright 2011 Taro L. Saito
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *--------------------------------------------------------------------------*/
-//--------------------------------------
-// XerialJ
-//
-// SnappyNativeLoader.java
-// Since: 2011/07/04 12:10:28
-//
-// $URL$
-// $Author$
-//--------------------------------------
-package xerial.larray;
-
-import java.util.HashMap;
-
-public class LArrayNativeLoader
-{
- private static HashMap<String, Boolean> loadedLibFiles = new HashMap<String, Boolean>();
- private static HashMap<String, Boolean> loadedLib = new HashMap<String, Boolean>();
-
- public static synchronized void load(String lib) {
- if (loadedLibFiles.containsKey(lib) && loadedLibFiles.get(lib) == true)
- return;
-
- try {
- System.load(lib);
- loadedLibFiles.put(lib, true);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static synchronized void loadLibrary(String libname) {
- if (loadedLib.containsKey(libname) && loadedLib.get(libname) == true)
- return;
-
- try {
- System.loadLibrary(libname);
- loadedLib.put(libname, true);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-}
@@ -144,7 +144,7 @@ object LArrayLoader {
private def extractLibraryFile(libFolderForCurrentOS:String, libraryFileName:String, targetFolder:String) : File = {
val nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName
val suffix = UUID.randomUUID().toString
- val extractedLibFileName = s"larray-${getVersion}-${suffix}.lib"
+ val extractedLibFileName = s"larray-${suffix}.lib"
val extractedLibFile = new File(targetFolder, extractedLibFileName)
@@ -222,30 +222,4 @@ object LArrayLoader {
}
- /**
- * Get the LArray version by reading VERSION file embedded in jar.
- * This version data is used as a suffix of a dll file extracted from the
- * jar.
- *
- * @return the version string
- */
- def getVersion : String = {
- val versionFile = this.getClass.getResource("/xerial/larray/VERSION")
- var version = "unknown"
- try {
- if (versionFile != null) {
- val versionData = new Properties()
- versionData.load(versionFile.openStream())
- version = versionData.getProperty("version", version)
- if (version.equals("unknown"))
- version = versionData.getProperty("VERSION", version)
- version = version.trim().replaceAll("[^0-9\\.]", "")
- }
- }
- catch {
- case e:IOException => System.err.println(e)
- }
- version
- }
-
}
@@ -323,6 +323,8 @@ object LArray {
}
finally
fin.close
+
+
}
@@ -22,7 +22,6 @@
package xerial.larray
-import impl.LArrayNative
import xerial.core.log.Logger
import java.lang.ref.{PhantomReference, ReferenceQueue}
import collection.mutable
@@ -44,7 +43,6 @@ class MemoryReference(m:Memory, queue:ReferenceQueue[Memory], val address:Long)
def name : String = "off-heap"
def release {
- val size = m.size
unsafe.freeMemory(m.headerAddress)
}
@@ -245,7 +243,7 @@ class DefaultAllocator(allocatedMemoryReferences: mutable.Map[Long, MemoryRefere
ref.release
ref match {
case r:MemoryReference =>
- totalAllocatedSize.getAndAdd(- size)
+ totalAllocatedSize.getAndAdd(- m.size)
}
ref.clear()
allocatedMemoryReferences.remove(m.address)
@@ -15,7 +15,6 @@
*--------------------------------------------------------------------------*/
package xerial
-import larray.impl.LArrayNative
import reflect.ClassTag
/**

0 comments on commit a598e03

Please sign in to comment.