Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.AnnotateOriginal;
import com.oracle.svm.core.annotate.Delete;
import com.oracle.svm.core.annotate.KeepOriginal;
import com.oracle.svm.core.annotate.NeverInline;
Expand Down Expand Up @@ -150,6 +151,10 @@ private static Enum<?> valueOf(Class<Enum<?>> enumType, String name) {
throw new IllegalArgumentException("No enum constant " + enumType.getName() + "." + name);
}
}

@AnnotateOriginal
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public native int ordinal();
}

@TargetClass(java.lang.String.class)
Expand All @@ -160,6 +165,32 @@ public String intern() {
String thisStr = SubstrateUtil.cast(this, String.class);
return ImageSingletons.lookup(StringInternSupport.class).intern(thisStr);
}

@AnnotateOriginal
@TargetElement(onlyWith = JDK11OrLater.class)
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
native boolean isLatin1();

@AnnotateOriginal
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public native int length();

@AnnotateOriginal
@TargetElement(onlyWith = JDK11OrLater.class)
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
native byte coder();

@Alias @TargetElement(name = "value", onlyWith = JDK11OrLater.class) byte[] valueJDK11;

@Alias @TargetElement(name = "value", onlyWith = JDK8OrEarlier.class) char[] valueJDK8;
}

@TargetClass(className = "java.lang.StringUTF16", onlyWith = JDK11OrLater.class)
final class Target_java_lang_StringUTF16 {

@AnnotateOriginal
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
static native char getChar(byte[] val, int index);
}

@TargetClass(java.lang.Throwable.class)
Expand Down Expand Up @@ -663,6 +694,16 @@ final class Target_jdk_internal_loader_ClassLoaders {
/** Dummy class to have a class with the file's name. */
public final class JavaLangSubstitutions {

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static byte[] getBytes(String string) {
return SubstrateUtil.cast(string, Target_java_lang_String.class).valueJDK11;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static boolean isLatin1(String string) {
return SubstrateUtil.cast(string, Target_java_lang_String.class).isLatin1();
}

public static final class ClassValueSupport {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.jdk;

import com.oracle.svm.core.annotate.Uninterruptible;

/**
* Abstract base class for all uninterruptible hashtables.
*/
public interface UninterruptibleAbstractHashtable<T extends UninterruptibleEntry<T>> {

static <T extends UninterruptibleEntry<T>> UninterruptibleThreadSafeHashtable<T> makeHashtableThreadSafe(String name, UninterruptibleAbstractHashtable<T> table) {
return new UninterruptibleThreadSafeHashtable<>(name, table);
}

/**
* Sets hashtable size.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
void setSize(int size);

/**
* Gets hashtable size.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
int getSize();

/**
* Gets hashtable.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
T[] getTable();

/**
* Check if {@code valueOnStack} exists in hashtable, if it's exists, return that value.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
T contains(T valueOnStack);

/**
* Put {@code valueOnStack} in hashtable.
*
* @return new entry id or existing entry id, if old one is equals to the {@code valueOnStack}.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
long put(T valueOnStack);

/**
* Put {@code valueOnStack} in hashtable.
*
* @return true if new entry is created, false, if it's already there.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
boolean putIfAbsent(T valueOnStack);

/**
* Deallocate memory occupied by {@code t}.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
void free(T t);

/**
* Clear all entries from map.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
void clear();

/**
* Teardown hashtable.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
void teardown();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,37 @@
*/
package com.oracle.svm.core.jdk;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.UnmanagedMemorySupport;
import org.graalvm.word.Pointer;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.UnmanagedMemoryUtil;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.locks.VMMutex;

/**
* An uninterruptible hashtable with a fixed size that uses chaining in case of a collision.
*/
public abstract class UninterruptibleHashtable<T extends UninterruptibleEntry<T>> {
private static final int DEFAULT_TABLE_LENGTH = 2053;
public abstract class UninterruptibleHashtable<T extends UninterruptibleEntry<T>> implements UninterruptibleAbstractHashtable<T> {

private final T[] table;
private final VMMutex mutex;
protected static final int DEFAULT_TABLE_LENGTH = 2053;

private long nextId;
private int size;
protected final T[] table;

protected long nextId;
protected int size;

@Platforms(Platform.HOSTED_ONLY.class)
public UninterruptibleHashtable(String name) {
this(name, DEFAULT_TABLE_LENGTH);
public UninterruptibleHashtable() {
this(DEFAULT_TABLE_LENGTH);
}

@Platforms(Platform.HOSTED_ONLY.class)
public UninterruptibleHashtable(String name, int primeLength) {
public UninterruptibleHashtable(int primeLength) {
this.table = createTable(primeLength);
this.mutex = new VMMutex(name);
this.size = 0;
}

Expand All @@ -62,78 +65,115 @@ public UninterruptibleHashtable(String name, int primeLength) {
protected abstract boolean isEqual(T a, T b);

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected abstract T copyToHeap(T valueOnStack);
protected T allocateOnHeap(Pointer pointerOnStack, UnsignedWord sizeToAlloc) {
T pointerOnHeap = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(sizeToAlloc);
if (pointerOnHeap.isNonNull()) {
UnmanagedMemoryUtil.copy(pointerOnStack, (Pointer) pointerOnHeap, sizeToAlloc);
return pointerOnHeap;
}
return WordFactory.nullPointer();
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected abstract void free(T t);
protected abstract T copyToHeap(T valueOnStack);

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void clear() {
for (int i = 0; i < table.length; i++) {
T entry = table[i];
while (entry.isNonNull()) {
T tmp = entry;
entry = entry.getNext();
free(tmp);
}
table[i] = WordFactory.nullPointer();
protected long insertEntry(T valueOnStack) {
int index = Integer.remainderUnsigned(valueOnStack.getHash(), DEFAULT_TABLE_LENGTH);
T newEntry = copyToHeap(valueOnStack);
if (newEntry.isNonNull()) {
long id = ++nextId;
T existingEntry = table[index];
newEntry.setNext(existingEntry);
newEntry.setId(id);
table[index] = newEntry;
size++;
return id;
}
size = 0;
return 0L;
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void setSize(int size) {
this.size = size;
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void teardown() {
clear();
public int getSize() {
return size;
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getSize() {
return size;
public T[] getTable() {
return table;
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public long add(T valueOnStack) {
public T contains(T valueOnStack) {
int index = Integer.remainderUnsigned(valueOnStack.getHash(), DEFAULT_TABLE_LENGTH);
T entry = table[index];
while (entry.isNonNull()) {
if (isEqual(valueOnStack, entry)) {
return entry;
}
entry = entry.getNext();
}
return WordFactory.nullPointer();
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public long put(T valueOnStack) {
assert valueOnStack.isNonNull();

mutex.lockNoTransition();
try {
// Try to find the entry in the hashtable
int index = Integer.remainderUnsigned(valueOnStack.getHash(), DEFAULT_TABLE_LENGTH);
T entry = table[index];
while (entry.isNonNull()) {
if (isEqual(valueOnStack, entry)) {
return entry.getId();
}
entry = entry.getNext();
}
T entry = contains(valueOnStack);
if (entry.isNonNull()) {
return entry.getId();
} else {
return insertEntry(valueOnStack);
}
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public boolean putIfAbsent(T valueOnStack) {
assert valueOnStack.isNonNull();

return insertEntry(index, valueOnStack);
} finally {
mutex.unlock();
T entry = contains(valueOnStack);
if (entry.isNonNull()) {
return false;
} else {
insertEntry(valueOnStack);
return true;
}
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private long insertEntry(int index, T valueOnStack) {
T newEntry = copyToHeap(valueOnStack);
if (newEntry.isNonNull()) {
long id = ++nextId;
T existingEntry = table[index];
newEntry.setNext(existingEntry);
newEntry.setId(id);
table[index] = newEntry;
size++;
return id;
public abstract void free(T t);

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void clear() {
for (int i = 0; i < table.length; i++) {
T entry = table[i];
while (entry.isNonNull()) {
T tmp = entry;
entry = entry.getNext();
free(tmp);
}
table[i] = WordFactory.nullPointer();
}
return 0L;
size = 0;
}

public T[] getTable() {
return table;
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void teardown() {
clear();
}
}
Loading