Path: blob/master/src/java.desktop/share/classes/sun/java2d/marlin/OffHeapArray.java
41159 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.marlin;2627import static sun.java2d.marlin.MarlinConst.LOG_UNSAFE_MALLOC;28import jdk.internal.misc.Unsafe;29import jdk.internal.ref.CleanerFactory;3031/**32*33*/34final class OffHeapArray {3536// unsafe reference37static final Unsafe UNSAFE;38// size of int / float39static final int SIZE_INT;4041static {42UNSAFE = Unsafe.getUnsafe();43SIZE_INT = Unsafe.ARRAY_INT_INDEX_SCALE;44}4546/* members */47long address;48long length;49int used;5051OffHeapArray(final Object parent, final long len) {52// note: may throw OOME:53this.address = UNSAFE.allocateMemory(len);54this.length = len;55this.used = 0;56if (LOG_UNSAFE_MALLOC) {57MarlinUtils.logInfo(System.currentTimeMillis()58+ ": OffHeapArray.allocateMemory = "59+ len + " to addr = " + this.address);60}6162// Register a cleaning function to ensure freeing off-heap memory:63CleanerFactory.cleaner().register(parent, () -> this.free());64}6566/*67* As realloc may change the address, updating address is MANDATORY68* @param len new array length69* @throws OutOfMemoryError if the allocation is refused by the system70*/71void resize(final long len) {72// note: may throw OOME:73this.address = UNSAFE.reallocateMemory(address, len);74this.length = len;75if (LOG_UNSAFE_MALLOC) {76MarlinUtils.logInfo(System.currentTimeMillis()77+ ": OffHeapArray.reallocateMemory = "78+ len + " to addr = " + this.address);79}80}8182void free() {83UNSAFE.freeMemory(this.address);84if (LOG_UNSAFE_MALLOC) {85MarlinUtils.logInfo(System.currentTimeMillis()86+ ": OffHeapArray.freeMemory = "87+ this.length88+ " at addr = " + this.address);89}90this.address = 0L;91}9293void fill(final byte val) {94UNSAFE.setMemory(this.address, this.length, val);95}96}979899