Is there a sample for JDK1.4's undocumented sun.misc.Unsafe class?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Davanum_Srinivas
Posted On:   Saturday, June 16, 2001 04:13 PM

Is there a sample for JDK1.4's undocumented sun.misc.Unsafe class? This class supposedly allows java code to access native memory directly from Java (without needing to write C++ JNI code).

Re: Is there a sample for JDK1.4's undocumented sun.misc.Unsafe class?

Posted By:   Davanum_Srinivas  
Posted On:   Saturday, June 16, 2001 04:15 PM

The java.sun.com's BugParade BUGID:4420961 has some sample code.

import java.io.*;
import sun.misc.*;
import java.security.*;

class reallocmem001 {

public static void main (String args[]) {
System.exit(95 + run(args, System.out));
}

public static int run (String args[], PrintStream out) {
boolean testFailed = false;
long address;
Unsafe unsafe = (Unsafe)
AccessController.doPrivileged(new Unsafe.GetUnsafeAction());

// Allocate memory
try {
address = unsafe.allocateMemory(1);
} catch(Throwable t) {
out.println("# FAILURE 1.");
out.println(" Cannot allocate memory.");
out.println(" Exception: " + t);
return ( (t instanceof OutOfMemoryError) ||
(t instanceof IllegalArgumentException)
) ? 0 : 2;
}
unsafe.putByte(address, Byte.MAX_VALUE);

// Reallocate memory
try {
address = unsafe.reallocateMemory(address, 2);
if (address == 0) {
out.println("# FAILURE 2.");
out.println(" Address of reallocated memory is 0.");
testFailed = true;
}
} catch(Exception e) {
out.println("# FAILURE 3.");
out.println(" Cannot reallocate memory");
return 2; // Not possibe to continue
}

// Check value put in the memory before reallocation
byte b = unsafe.getByte(address);
if (b != Byte.MAX_VALUE) {
out.println("# FAILURE 4.");
out.println(" " + Byte.MAX_VALUE + " was put in the "
+ "memory before reallocation and " + b
+ " was read.");
testFailed = true;
}

// Try to reallocate memory with size 0
try {
address = unsafe.reallocateMemory(address, 0);
if (address != 0) {
out.println("# FAILURE 5.");
out.println(" Memory was reallocated with size 0, "
+ "but address is " + address + " (should be 0).");
testFailed = true;
}
} catch(Exception e) {
out.println("# FAILURE 6.");
out.println(" Unexpected exception " + e);
testFailed = true;
}

// Try to reallocate memory with address 0
// (an allocation should be performed) and then
// check if memory was allocated
try {
address = unsafe.reallocateMemory(0, 1);
if (address == 0) {
out.println("# FAILURE 7.");
out.println(" Memory was reallocated with address 0 "
+ "and size 1 (byte), but reallocated "
+ "address is " + address + ".");
testFailed = true;
}
} catch(Exception e) {
out.println("# FAILURE 8.");
out.println(" Unexpected exception " + e);
testFailed = true;
}

// Check if memory allocated
try {
unsafe.putByte(address, Byte.MAX_VALUE);
b = unsafe.getByte(address);
if (b != Byte.MAX_VALUE) {
out.println("# FAILURE 9.");
out.println(" " + Byte.MAX_VALUE + " was put in the "
+ "memory and " + b + " was read.");
testFailed = true;
}
} catch (Exception e) {
out.println("# FAILURE 10.");
out.println(" Unexpected exception " + e);
testFailed = true;
}

// Check if VM throws OutOfMemoryError if the
// reallocation is refused by the system
long size = 1;
try {
for (int i = 0; i < 64; i++) {
address = unsafe.reallocateMemory(address, size);
if (address == 0) {
out.println("# FAILURE 11.");
out.println(" Address of reallocated memory is 0 "
+ "when size = " + size);
testFailed = true;
}
size <<= 1;
}
unsafe.freeMemory(address);
} catch (Throwable t) {
if (!((t instanceof OutOfMemoryError) ||
(t instanceof IllegalArgumentException)
)
) {
out.println("# Unexpected exception: " + t);
testFailed = true;
}
}

if (testFailed)
out.println("# TEST FAILED.");
return (testFailed ? 2 : 0);
}
}
About | Sitemap | Contact