1 /* 2 * Copyright (c) 2007, 2018, 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 26 package com.sun.marlin; 27 28 import static com.sun.marlin.MarlinConst.LOG_UNSAFE_MALLOC; 29 import java.lang.reflect.Field; 30 import java.security.AccessController; 31 import java.security.PrivilegedAction; 32 import sun.misc.Unsafe; 33 34 /** 35 * 36 * @author bourgesl 37 */ 38 final class OffHeapArray { 39 40 // unsafe reference 41 static final Unsafe UNSAFE; 42 // size of int / float 43 static final int SIZE_INT; 44 45 static { 46 UNSAFE = AccessController.doPrivileged(new PrivilegedAction<Unsafe>() { 47 @Override 48 public Unsafe run() { 49 Unsafe ref = null; 50 try { 51 final Field field = Unsafe.class.getDeclaredField("theUnsafe"); 52 field.setAccessible(true); 53 ref = (Unsafe) field.get(null); 54 } catch (Exception e) { 55 throw new InternalError("Unable to get sun.misc.Unsafe instance", e); 56 } 57 return ref; 58 } 59 }); 60 61 SIZE_INT = Unsafe.ARRAY_INT_INDEX_SCALE; 62 } 63 64 /* members */ 65 long address; 66 long length; 67 int used; 68 69 OffHeapArray(final Object parent, final long len) { 70 // note: may throw OOME: 71 this.address = UNSAFE.allocateMemory(len); 72 this.length = len; 73 this.used = 0; 74 if (LOG_UNSAFE_MALLOC) { 75 MarlinUtils.logInfo(System.currentTimeMillis() 76 + ": OffHeapArray.allocateMemory = " 77 + len + " to addr = " + this.address); 78 } 79 80 // Register a cleaning function to ensure freeing off-heap memory: 81 MarlinUtils.getCleaner().register(parent, () -> this.free()); 82 } 83 84 /* 85 * As realloc may change the address, updating address is MANDATORY 86 * @param len new array length 87 * @throws OutOfMemoryError if the allocation is refused by the system 88 */ 89 void resize(final long len) { 90 // note: may throw OOME: 91 this.address = UNSAFE.reallocateMemory(address, len); 92 this.length = len; 93 if (LOG_UNSAFE_MALLOC) { 94 MarlinUtils.logInfo(System.currentTimeMillis() 95 + ": OffHeapArray.reallocateMemory = " 96 + len + " to addr = " + this.address); 97 } 98 } 99 100 void free() { 101 UNSAFE.freeMemory(this.address); 102 if (LOG_UNSAFE_MALLOC) { 103 MarlinUtils.logInfo(System.currentTimeMillis() 104 + ": OffHeapArray.freeMemory = " 105 + this.length 106 + " at addr = " + this.address); 107 } 108 this.address = 0L; 109 } 110 111 void fill(final byte val) { 112 UNSAFE.setMemory(this.address, this.length, val); 113 } 114 }