Skip to content

Commit dc81351

Browse files
committed
Merge branch 'release/0.2.1'
2 parents c7f6f67 + 0ca48eb commit dc81351

17 files changed

Lines changed: 332 additions & 130 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
LArray
22
===
3-
A library for managing large off-heap arrays that can hold more than 2G (2^31) entries in Java and Scala.
3+
A library for managing large off-heap arrays that can hold more than 2G (2^31) entries in Java and Scala. Notably LArray is *disposable* by calling `LArray.free` or you can let GC automatically release the memory. LArray also can be used to create an `mmap` (memory-mapped file) whose size is more than 2GB.
44

55
## Features
66
* LArray can create arrays with more than 2G(2^31) entries.
@@ -94,6 +94,7 @@ A standard JVM, (e.g. Oracle JVM (standard JVM, HotSpotVM) or OpenJDK) must be u
9494

9595

9696
## History
97+
* November 11, 2013 version 0.2.1 - Use orgnization name `org.xerial.larray`. Add LBuffer.view.
9798
* November 11, 2013 version 0.2 - Extracted pure-java modules (larray-buffer.jar and larray-mmap.jar) from larray.jar (for Scala).
9899
* August 28, 2013 version 0.1.2 - improved memory layout
99100
* August 28, 2013 version 0.1.1 (for Scala 2.10.2)
@@ -105,15 +106,15 @@ A standard JVM, (e.g. Oracle JVM (standard JVM, HotSpotVM) or OpenJDK) must be u
105106
Add the following sbt dependency to your project settings:
106107

107108
```scala
108-
libraryDependencies += "org.xerial" % "larray" % "0.2"
109+
libraryDependencies += "org.xerial.larray" % "larray" % "0.2.1"
109110
```
110111

111112
* Using snapshot versions:
112113

113114
```scala
114115
resolvers += "Sonatype shapshot repo" at "https://oss.sonatype.org/content/repositories/snapshots/"
115116

116-
libraryDependencies += "org.xerial" % "larray" % "0.2-SNAPSHOT"
117+
libraryDependencies += "org.xerial.larray" % "larray" % "0.2.2-SNAPSHOT"
117118
```
118119
### Example
119120

@@ -137,16 +138,16 @@ l2.free
137138
l2(0) // The result of accessing released LArray is undefined
138139
```
139140

140-
For more examples, see [xerial/larray/example/LArrayExample.scala](larray-scala/src/main/scala/xerial/larray/example/LArrayExample.scala)
141+
For more examples, see [xerial/larray/example/LArrayExample.scala](larray/src/main/scala/xerial/larray/example/LArrayExample.scala)
141142

142143
## Usage (Java)
143144

144145
Add the following dependency to your pom.xml (Maven):
145146
```xml
146147
<dependency>
147-
<groupId>org.xerial</groupId>
148+
<groupId>org.xerial.larray</groupId>
148149
<artifactId>larray</artifactId>
149-
<version>0.2</version>
150+
<version>0.2.1</version>
150151
</dependency>
151152
```
152153

@@ -165,11 +166,11 @@ int e0 = l.apply(0L); // Get l[0L]
165166
// release
166167
l.free();
167168
```
168-
For more examples, see [xerial/larray/example/LArrayJavaExample.scala](larray-scala/src/main/scala/xerial/larray/example/LArrayJavaExample.java)
169+
For more examples, see [xerial/larray/example/LArrayJavaExample.scala](larray/src/main/scala/xerial/larray/example/LArrayJavaExample.java)
169170

170171
## Scaladoc
171172

172-
* [LArray Scala API](https://oss.sonatype.org/service/local/repositories/releases/archive/org/xerial/larray/0.2/larray-0.2-javadoc.jar/!/index.html#xerial.larray.package)
173+
* [LArray Scala API](https://oss.sonatype.org/service/local/repositories/releases/archive/org/xerial/larray/larray/0.2.1/larray-0.2.1-javadoc.jar/!/index.html#xerial.larray.package)
173174

174175
## For developers
175176

larray-buffer/src/main/java/xerial/larray/buffer/BufferConfig.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

larray-buffer/src/main/java/xerial/larray/buffer/OffHeapMemoryAllocator.java renamed to larray-buffer/src/main/java/xerial/larray/buffer/DefaultMemoryAllocator.java

Lines changed: 19 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,33 @@
11
package xerial.larray.buffer;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
3+
4+
import org.xerial.util.log.Logger;
55

66
import java.lang.ref.ReferenceQueue;
77
import java.util.Map;
88
import java.util.concurrent.ConcurrentHashMap;
99
import java.util.concurrent.atomic.AtomicLong;
1010

11-
import static xerial.larray.buffer.UnsafeUtil.unsafe;
12-
13-
14-
/**
15-
* Stores |(memory size:long)| data ... |
16-
*/
17-
class OffHeapMemory implements Memory {
18-
19-
private final long _data;
20-
21-
public static long HEADER_SIZE = 8L;
22-
23-
/**
24-
* Create an empty memory
25-
*/
26-
public OffHeapMemory() {
27-
this._data = 0L;
28-
}
29-
30-
public OffHeapMemory(long address) {
31-
if(address != 0L)
32-
this._data = address + HEADER_SIZE;
33-
else
34-
this._data = 0L;
35-
}
36-
37-
public OffHeapMemory(long address, long size) {
38-
if(address != 0L) {
39-
this._data = address + HEADER_SIZE;
40-
unsafe.putLong(address, size);
41-
}
42-
else {
43-
this._data = 0L;
44-
}
45-
}
46-
47-
public long headerAddress() {
48-
return _data - HEADER_SIZE;
49-
}
50-
public long size() {
51-
return (_data == 0) ? 0L : unsafe.getLong(headerAddress()) + HEADER_SIZE;
52-
}
53-
54-
public long address() {
55-
return _data;
56-
}
57-
58-
public long dataSize() {
59-
return (_data == 0) ? 0L : unsafe.getLong(headerAddress());
60-
}
61-
62-
public MemoryReference toRef(ReferenceQueue<Memory> queue) {
63-
return new OffHeapMemoryReference(this, queue);
64-
}
65-
66-
public void release() {
67-
if(_data != 0)
68-
UnsafeUtil.unsafe.freeMemory(headerAddress());
69-
}
70-
}
71-
72-
class OffHeapMemoryReference extends MemoryReference {
73-
74-
/**
75-
* Create a phantom reference
76-
* @param m the allocated memory
77-
* @param queue the reference queue to which GCed reference of the Memory will be inserted
78-
*/
79-
public OffHeapMemoryReference(Memory m, ReferenceQueue<Memory> queue) {
80-
super(m, queue);
81-
}
82-
83-
public Memory toMemory() {
84-
if(address != 0)
85-
return new OffHeapMemory(address);
86-
else
87-
return new OffHeapMemory();
88-
}
89-
90-
public String name() { return "off-heap"; }
91-
92-
}
93-
9411

9512

9613
/**
97-
* Allocating off-heap memory
14+
* A default implementation of MemoryAllocator that allocates off-heap memory and releases allocated memories in a background thread.
9815
*
9916
* @author Taro L. Saito
10017
*/
101-
public class OffHeapMemoryAllocator implements MemoryAllocator {
18+
public class DefaultMemoryAllocator implements MemoryAllocator {
19+
20+
private Logger logger = Logger.getLogger(DefaultMemoryAllocator.class);
10221

103-
private Logger logger = LoggerFactory.getLogger(OffHeapMemoryAllocator.class);
10422

10523
// Table from address -> MemoryReference
10624
private Map<Long, MemoryReference> allocatedMemoryReferences = new ConcurrentHashMap<Long, MemoryReference>();
10725
private ReferenceQueue<Memory> queue = new ReferenceQueue<Memory>();
10826

10927
{
28+
// Enable ANSI Color
29+
logger.enableColor(true);
30+
11031
// Start OffHeapMemory collector that releases the allocated memory when the corresponding Memory object is collected by GC.
11132
Thread collector = new Thread(new Runnable() {
11233
@Override
@@ -136,15 +57,19 @@ public void run() {
13657
*/
13758
public long allocatedSize() { return totalAllocatedSize.get(); }
13859

60+
/**
61+
* Allocate a memory of the specified byte length. The allocated memory must be released via `release`
62+
* as in malloc() in C/C++.
63+
* @param size byte length of the memory
64+
* @return allocated memory information
65+
*/
13966
public Memory allocate(long size) {
14067
if(size == 0L)
141-
return new OffHeapMemory();
68+
return new OffHeapMemory();
14269

14370
// Allocate memory of the given size + HEADER space
14471
long memorySize = size + OffHeapMemory.HEADER_SIZE;
145-
long address = unsafe.allocateMemory(memorySize);
146-
if(logger.isTraceEnabled())
147-
logger.trace(String.format("Allocated memory address:%x, size:%,d", address, size));
72+
long address = UnsafeUtil.unsafe.allocateMemory(memorySize);
14873
Memory m = new OffHeapMemory(address, size);
14974
register(m);
15075
return m;
@@ -183,10 +108,9 @@ public void release(Memory m) {
183108
synchronized(this) {
184109
long address = m.headerAddress();
185110
if(allocatedMemoryReferences.containsKey(address)) {
186-
long size = m.size();
187111
if(logger.isTraceEnabled())
188-
logger.trace(String.format("Released memory address:%x, size:%,d", address, size));
189-
totalAllocatedSize.getAndAdd(-size);
112+
logger.trace(String.format("Released memory address:%x, size:%,d", address, m.dataSize()));
113+
totalAllocatedSize.getAndAdd(-m.size());
190114
allocatedMemoryReferences.remove(address);
191115
m.release();
192116
}

larray-buffer/src/main/java/xerial/larray/buffer/LBuffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class LBuffer extends LBufferAPI {
1414
* @param size byte size of the array
1515
*/
1616
public LBuffer(long size) {
17-
super(BufferConfig.allocator.allocate(size));
17+
super(LBufferConfig.allocator.allocate(size));
1818
}
1919

2020

0 commit comments

Comments
 (0)