|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.core.jdk; |
| 26 | + |
| 27 | +import org.graalvm.nativeimage.ImageSingletons; |
| 28 | +import org.graalvm.nativeimage.Platform; |
| 29 | +import org.graalvm.nativeimage.Platforms; |
| 30 | +import org.graalvm.nativeimage.impl.UnmanagedMemorySupport; |
| 31 | +import org.graalvm.word.Pointer; |
| 32 | +import org.graalvm.word.UnsignedWord; |
| 33 | +import org.graalvm.word.WordFactory; |
| 34 | + |
| 35 | +import com.oracle.svm.core.UnmanagedMemoryUtil; |
| 36 | +import com.oracle.svm.core.annotate.Uninterruptible; |
| 37 | + |
| 38 | +/** |
| 39 | + * An uninterruptible hashtable with a fixed size that uses chaining in case of a collision. |
| 40 | + */ |
| 41 | +public abstract class AbstractUninterruptibleHashtable<T extends UninterruptibleEntry<T>> implements UninterruptibleHashtable<T> { |
| 42 | + private static final int DEFAULT_TABLE_LENGTH = 2053; |
| 43 | + |
| 44 | + private final T[] table; |
| 45 | + |
| 46 | + private int size; |
| 47 | + |
| 48 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 49 | + public AbstractUninterruptibleHashtable() { |
| 50 | + this(DEFAULT_TABLE_LENGTH); |
| 51 | + } |
| 52 | + |
| 53 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 54 | + public AbstractUninterruptibleHashtable(int primeLength) { |
| 55 | + this.table = createTable(primeLength); |
| 56 | + this.size = 0; |
| 57 | + } |
| 58 | + |
| 59 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 60 | + protected abstract T[] createTable(int length); |
| 61 | + |
| 62 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 63 | + protected abstract boolean isEqual(T a, T b); |
| 64 | + |
| 65 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 66 | + protected abstract T copyToHeap(T valueOnStack); |
| 67 | + |
| 68 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 69 | + protected T copyToHeap(T pointerOnStack, UnsignedWord sizeToAlloc) { |
| 70 | + T pointerOnHeap = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(sizeToAlloc); |
| 71 | + if (pointerOnHeap.isNonNull()) { |
| 72 | + UnmanagedMemoryUtil.copy((Pointer) pointerOnStack, (Pointer) pointerOnHeap, sizeToAlloc); |
| 73 | + return pointerOnHeap; |
| 74 | + } |
| 75 | + return WordFactory.nullPointer(); |
| 76 | + } |
| 77 | + |
| 78 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 79 | + protected abstract void free(T t); |
| 80 | + |
| 81 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 82 | + protected T insertEntry(T valueOnStack) { |
| 83 | + int index = Integer.remainderUnsigned(valueOnStack.getHash(), DEFAULT_TABLE_LENGTH); |
| 84 | + T newEntry = copyToHeap(valueOnStack); |
| 85 | + if (newEntry.isNonNull()) { |
| 86 | + T existingEntry = table[index]; |
| 87 | + newEntry.setNext(existingEntry); |
| 88 | + table[index] = newEntry; |
| 89 | + size++; |
| 90 | + return newEntry; |
| 91 | + } |
| 92 | + return WordFactory.nullPointer(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 97 | + public int getSize() { |
| 98 | + return size; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 103 | + public T[] getTable() { |
| 104 | + return table; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 109 | + public T get(T valueOnStack) { |
| 110 | + int index = Integer.remainderUnsigned(valueOnStack.getHash(), DEFAULT_TABLE_LENGTH); |
| 111 | + T entry = table[index]; |
| 112 | + while (entry.isNonNull()) { |
| 113 | + if (isEqual(valueOnStack, entry)) { |
| 114 | + return entry; |
| 115 | + } |
| 116 | + entry = entry.getNext(); |
| 117 | + } |
| 118 | + return WordFactory.nullPointer(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 123 | + public T getOrPut(T valueOnStack) { |
| 124 | + assert valueOnStack.isNonNull(); |
| 125 | + |
| 126 | + T entry = get(valueOnStack); |
| 127 | + if (entry.isNonNull()) { |
| 128 | + return WordFactory.nullPointer(); |
| 129 | + } else { |
| 130 | + return insertEntry(valueOnStack); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 136 | + public boolean putIfAbsent(T valueOnStack) { |
| 137 | + assert valueOnStack.isNonNull(); |
| 138 | + |
| 139 | + T existingEntry = get(valueOnStack); |
| 140 | + if (existingEntry.isNonNull()) { |
| 141 | + return false; |
| 142 | + } else { |
| 143 | + T newEntry = insertEntry(valueOnStack); |
| 144 | + return newEntry.isNonNull(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 150 | + public void clear() { |
| 151 | + for (int i = 0; i < table.length; i++) { |
| 152 | + T entry = table[i]; |
| 153 | + while (entry.isNonNull()) { |
| 154 | + T tmp = entry; |
| 155 | + entry = entry.getNext(); |
| 156 | + free(tmp); |
| 157 | + } |
| 158 | + table[i] = WordFactory.nullPointer(); |
| 159 | + } |
| 160 | + size = 0; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 165 | + public void teardown() { |
| 166 | + clear(); |
| 167 | + } |
| 168 | +} |
0 commit comments