Skip to content
Snippets Groups Projects
Commit 28984710 authored by Florian Angerer's avatar Florian Angerer
Browse files

Refined assertion in 'NativeDataAccess' and introduced empty array marker

address.
parent 01198b9c
No related branches found
No related tags found
No related merge requests found
...@@ -96,6 +96,8 @@ public final class NativeDataAccess { ...@@ -96,6 +96,8 @@ public final class NativeDataAccess {
private static final boolean TRACE_MIRROR_ALLOCATION_SITES = false; private static final boolean TRACE_MIRROR_ALLOCATION_SITES = false;
private static final long EMPTY_DATA_ADDRESS = 0xDEADL;
public static boolean isNativeMirror(Object o) { public static boolean isNativeMirror(Object o) {
return o instanceof NativeMirror; return o instanceof NativeMirror;
} }
...@@ -132,9 +134,16 @@ public final class NativeDataAccess { ...@@ -132,9 +134,16 @@ public final class NativeDataAccess {
@TruffleBoundary @TruffleBoundary
void allocateNative(Object source, int len, int elementBase, int elementSize) { void allocateNative(Object source, int len, int elementBase, int elementSize) {
assert dataAddress == 0; assert dataAddress == 0;
dataAddress = UnsafeAdapter.UNSAFE.allocateMemory(len * elementSize); if (len != 0) {
UnsafeAdapter.UNSAFE.copyMemory(source, elementBase, null, dataAddress, len * elementSize); dataAddress = UnsafeAdapter.UNSAFE.allocateMemory(len * elementSize);
UnsafeAdapter.UNSAFE.copyMemory(source, elementBase, null, dataAddress, len * elementSize);
} else {
dataAddress = EMPTY_DATA_ADDRESS;
}
this.length = len; this.length = len;
// ensure that marker address is not used
assert this.length == 0 || dataAddress != EMPTY_DATA_ADDRESS;
} }
@TruffleBoundary @TruffleBoundary
...@@ -146,6 +155,9 @@ public final class NativeDataAccess { ...@@ -146,6 +155,9 @@ public final class NativeDataAccess {
UnsafeAdapter.UNSAFE.putByte(dataAddress + bytes.length, (byte) 0); // C strings UnsafeAdapter.UNSAFE.putByte(dataAddress + bytes.length, (byte) 0); // C strings
// terminator // terminator
this.length = bytes.length + 1; this.length = bytes.length + 1;
// ensure that marker address is not used
assert this.length == 0 || dataAddress != EMPTY_DATA_ADDRESS;
} }
@Override @Override
...@@ -153,7 +165,9 @@ public final class NativeDataAccess { ...@@ -153,7 +165,9 @@ public final class NativeDataAccess {
super.finalize(); super.finalize();
nativeMirrors.remove(id); nativeMirrors.remove(id);
// System.out.println(String.format("gc'ing %16x", id)); // System.out.println(String.format("gc'ing %16x", id));
if (dataAddress != 0) { if (dataAddress == EMPTY_DATA_ADDRESS) {
assert (dataAddress = 0xbadbad) != 0;
} else if (dataAddress != 0) {
// System.out.println(String.format("freeing data at %16x", dataAddress)); // System.out.println(String.format("freeing data at %16x", dataAddress));
UnsafeAdapter.UNSAFE.freeMemory(dataAddress); UnsafeAdapter.UNSAFE.freeMemory(dataAddress);
assert (dataAddress = 0xbadbad) != 0; assert (dataAddress = 0xbadbad) != 0;
...@@ -493,7 +507,7 @@ public final class NativeDataAccess { ...@@ -493,7 +507,7 @@ public final class NativeDataAccess {
} else { } else {
NativeMirror mirror = (NativeMirror) vector.getNativeMirror(); NativeMirror mirror = (NativeMirror) vector.getNativeMirror();
long address = mirror.dataAddress; long address = mirror.dataAddress;
assert address != 0; assert address != 0 || address == EMPTY_DATA_ADDRESS;
int length = 0; int length = 0;
while (length < mirror.length && UnsafeAdapter.UNSAFE.getByte(address + length) != 0) { while (length < mirror.length && UnsafeAdapter.UNSAFE.getByte(address + length) != 0) {
length++; length++;
...@@ -507,7 +521,7 @@ public final class NativeDataAccess { ...@@ -507,7 +521,7 @@ public final class NativeDataAccess {
static long allocateNativeContents(RLogicalVector vector, byte[] data, int length) { static long allocateNativeContents(RLogicalVector vector, byte[] data, int length) {
NativeMirror mirror = (NativeMirror) vector.getNativeMirror(); NativeMirror mirror = (NativeMirror) vector.getNativeMirror();
assert mirror != null; assert mirror != null;
assert mirror.dataAddress == 0 ^ data == null; assert mirror.dataAddress == 0 ^ (data == null || mirror.dataAddress == EMPTY_DATA_ADDRESS);
if (mirror.dataAddress == 0) { if (mirror.dataAddress == 0) {
int[] intArray = new int[data.length]; int[] intArray = new int[data.length];
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
...@@ -522,7 +536,7 @@ public final class NativeDataAccess { ...@@ -522,7 +536,7 @@ public final class NativeDataAccess {
static long allocateNativeContents(RIntVector vector, int[] data, int length) { static long allocateNativeContents(RIntVector vector, int[] data, int length) {
NativeMirror mirror = (NativeMirror) vector.getNativeMirror(); NativeMirror mirror = (NativeMirror) vector.getNativeMirror();
assert mirror != null; assert mirror != null;
assert mirror.dataAddress == 0 ^ data == null; assert mirror.dataAddress == 0 ^ (data == null || mirror.dataAddress == EMPTY_DATA_ADDRESS);
if (mirror.dataAddress == 0) { if (mirror.dataAddress == 0) {
noIntNative.invalidate(); noIntNative.invalidate();
mirror.allocateNative(data, length, Unsafe.ARRAY_INT_BASE_OFFSET, Unsafe.ARRAY_INT_INDEX_SCALE); mirror.allocateNative(data, length, Unsafe.ARRAY_INT_BASE_OFFSET, Unsafe.ARRAY_INT_INDEX_SCALE);
...@@ -533,7 +547,7 @@ public final class NativeDataAccess { ...@@ -533,7 +547,7 @@ public final class NativeDataAccess {
static long allocateNativeContents(RRawVector vector, byte[] data, int length) { static long allocateNativeContents(RRawVector vector, byte[] data, int length) {
NativeMirror mirror = (NativeMirror) vector.getNativeMirror(); NativeMirror mirror = (NativeMirror) vector.getNativeMirror();
assert mirror != null; assert mirror != null;
assert mirror.dataAddress == 0 ^ data == null; assert mirror.dataAddress == 0 ^ (data == null || mirror.dataAddress == EMPTY_DATA_ADDRESS);
if (mirror.dataAddress == 0) { if (mirror.dataAddress == 0) {
noRawNative.invalidate(); noRawNative.invalidate();
mirror.allocateNative(data, length, Unsafe.ARRAY_BYTE_BASE_OFFSET, Unsafe.ARRAY_BYTE_INDEX_SCALE); mirror.allocateNative(data, length, Unsafe.ARRAY_BYTE_BASE_OFFSET, Unsafe.ARRAY_BYTE_INDEX_SCALE);
...@@ -544,7 +558,7 @@ public final class NativeDataAccess { ...@@ -544,7 +558,7 @@ public final class NativeDataAccess {
static long allocateNativeContents(RDoubleVector vector, double[] data, int length) { static long allocateNativeContents(RDoubleVector vector, double[] data, int length) {
NativeMirror mirror = (NativeMirror) vector.getNativeMirror(); NativeMirror mirror = (NativeMirror) vector.getNativeMirror();
assert mirror != null; assert mirror != null;
assert mirror.dataAddress == 0 ^ data == null; assert mirror.dataAddress == 0 ^ (data == null || mirror.dataAddress == EMPTY_DATA_ADDRESS);
if (mirror.dataAddress == 0) { if (mirror.dataAddress == 0) {
noDoubleNative.invalidate(); noDoubleNative.invalidate();
mirror.allocateNative(data, length, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, Unsafe.ARRAY_DOUBLE_INDEX_SCALE); mirror.allocateNative(data, length, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, Unsafe.ARRAY_DOUBLE_INDEX_SCALE);
...@@ -555,7 +569,7 @@ public final class NativeDataAccess { ...@@ -555,7 +569,7 @@ public final class NativeDataAccess {
static long allocateNativeContents(RComplexVector vector, double[] data, int length) { static long allocateNativeContents(RComplexVector vector, double[] data, int length) {
NativeMirror mirror = (NativeMirror) vector.getNativeMirror(); NativeMirror mirror = (NativeMirror) vector.getNativeMirror();
assert mirror != null; assert mirror != null;
assert mirror.dataAddress == 0 ^ data == null; assert mirror.dataAddress == 0 ^ (data == null || mirror.dataAddress == EMPTY_DATA_ADDRESS);
if (mirror.dataAddress == 0) { if (mirror.dataAddress == 0) {
noComplexNative.invalidate(); noComplexNative.invalidate();
mirror.allocateNative(data, length, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, Unsafe.ARRAY_DOUBLE_INDEX_SCALE * 2); mirror.allocateNative(data, length, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, Unsafe.ARRAY_DOUBLE_INDEX_SCALE * 2);
...@@ -566,7 +580,7 @@ public final class NativeDataAccess { ...@@ -566,7 +580,7 @@ public final class NativeDataAccess {
static long allocateNativeContents(CharSXPWrapper vector, String contents) { static long allocateNativeContents(CharSXPWrapper vector, String contents) {
NativeMirror mirror = (NativeMirror) vector.getNativeMirror(); NativeMirror mirror = (NativeMirror) vector.getNativeMirror();
assert mirror != null; assert mirror != null;
assert mirror.dataAddress == 0 ^ contents == null; assert mirror.dataAddress == 0 ^ (contents == null || mirror.dataAddress == EMPTY_DATA_ADDRESS);
if (mirror.dataAddress == 0) { if (mirror.dataAddress == 0) {
noCahrSXPNative.invalidate(); noCahrSXPNative.invalidate();
mirror.allocateNative(contents); mirror.allocateNative(contents);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment