1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dalvik.system; 18 19 /** 20 * Common VMRuntime Delegate code used by both layoutlib and simulated_device. 21 */ 22 class VMRuntimeCommonHelper { 23 24 // Copied from libcore/libdvm/src/main/java/dalvik/system/VMRuntime 25 @SuppressWarnings("UnnecessaryLocalVariable") newUnpaddedArray(VMRuntime runtime, Class<?> componentType, int minLength)26 /*package*/ static Object newUnpaddedArray(VMRuntime runtime, Class<?> componentType, 27 int minLength) { 28 // Dalvik has 32bit pointers, the array header is 16bytes plus 4bytes for dlmalloc, 29 // allocations are 8byte aligned so having 4bytes of array data avoids padding. 30 if (!componentType.isPrimitive()) { 31 int size = ((minLength & 1) == 0) ? minLength + 1 : minLength; 32 return java.lang.reflect.Array.newInstance(componentType, size); 33 } else if (componentType == char.class) { 34 int bytes = 20 + (2 * minLength); 35 int alignedUpBytes = (bytes + 7) & -8; 36 int dataBytes = alignedUpBytes - 20; 37 int size = dataBytes / 2; 38 return new char[size]; 39 } else if (componentType == int.class) { 40 int size = ((minLength & 1) == 0) ? minLength + 1 : minLength; 41 return new int[size]; 42 } else if (componentType == byte.class) { 43 int bytes = 20 + minLength; 44 int alignedUpBytes = (bytes + 7) & -8; 45 int dataBytes = alignedUpBytes - 20; 46 int size = dataBytes; 47 return new byte[size]; 48 } else if (componentType == boolean.class) { 49 int bytes = 20 + minLength; 50 int alignedUpBytes = (bytes + 7) & -8; 51 int dataBytes = alignedUpBytes - 20; 52 int size = dataBytes; 53 return new boolean[size]; 54 } else if (componentType == short.class) { 55 int bytes = 20 + (2 * minLength); 56 int alignedUpBytes = (bytes + 7) & -8; 57 int dataBytes = alignedUpBytes - 20; 58 int size = dataBytes / 2; 59 return new short[size]; 60 } else if (componentType == float.class) { 61 int size = ((minLength & 1) == 0) ? minLength + 1 : minLength; 62 return new float[size]; 63 } else if (componentType == long.class) { 64 return new long[minLength]; 65 } else if (componentType == double.class) { 66 return new double[minLength]; 67 } else { 68 assert componentType == void.class; 69 throw new IllegalArgumentException("Can't allocate an array of void"); 70 } 71 } 72 73 getNotifyNativeInterval()74 /*package*/ static int getNotifyNativeInterval() { 75 // This cannot return 0, otherwise it is responsible for triggering an exception 76 // whenever trying to use a NativeAllocationRegistry with size 0 77 return 128; // see art/runtime/gc/heap.h -> kNotifyNativeInterval 78 } 79 } 80