-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testsuite: Add test for
sun.misc.Unsafe
- Loading branch information
1 parent
94ebbda
commit 53b643f
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
import java.lang.reflect.*; | ||
import sun.misc.Unsafe; | ||
|
||
class OffHeapArray { | ||
private final static int BYTE = 1; | ||
private long size; | ||
private long address; | ||
|
||
public OffHeapArray(long size) throws NoSuchFieldException, IllegalAccessException { | ||
this.size = size; | ||
address = getUnsafe().allocateMemory(size * BYTE); | ||
} | ||
|
||
private Unsafe getUnsafe() throws IllegalAccessException, NoSuchFieldException { | ||
Field f = Unsafe.class.getDeclaredField("theUnsafe"); | ||
f.setAccessible(true); | ||
return (Unsafe) f.get(null); | ||
} | ||
|
||
public void set(long i, byte value) throws NoSuchFieldException, IllegalAccessException { | ||
getUnsafe().putByte(address + i * BYTE, value); | ||
} | ||
|
||
public int get(long idx) throws NoSuchFieldException, IllegalAccessException { | ||
return getUnsafe().getByte(address + idx * BYTE); | ||
} | ||
|
||
public long size() { | ||
return size; | ||
} | ||
|
||
public void freeMemory() throws NoSuchFieldException, IllegalAccessException { | ||
getUnsafe().freeMemory(address); | ||
} | ||
} | ||
|
||
public class HelloWorld { | ||
public static void main(String[] args) throws Exception { | ||
OffHeapArray bigarray = new OffHeapArray(64*8*1024*1024); | ||
for (int i = 0; i < 131072; i++) bigarray.set(i * 4096 + 16, (byte) 1); | ||
System.out.println("Hello, World!"); | ||
} | ||
} | ||
// Source: https://dmoj.ca/src/5961758 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: JAVA8 | ||
time: 5 | ||
memory: 65535 | ||
source: HelloWorld.java | ||
expect: IR | ||
feedback: ca.dmoj.java.UnsafeDisallowedException |