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
newUnpaddedArray(VMRuntime runtime, Class<?> componentType, int minLength)25     /*package*/ static Object newUnpaddedArray(VMRuntime runtime, Class<?> componentType,
26             int minLength) {
27         // Dalvik has 32bit pointers, the array header is 16bytes plus 4bytes for dlmalloc,
28         // allocations are 8byte aligned so having 4bytes of array data avoids padding.
29         if (!componentType.isPrimitive()) {
30             int size = ((minLength & 1) == 0) ? minLength + 1 : minLength;
31             return java.lang.reflect.Array.newInstance(componentType, size);
32         } else if (componentType == char.class) {
33             int bytes = 20 + (2 * minLength);
34             int alignedUpBytes = (bytes + 7) & -8;
35             int dataBytes = alignedUpBytes - 20;
36             int size = dataBytes / 2;
37             return new char[size];
38         } else if (componentType == int.class) {
39             int size = ((minLength & 1) == 0) ? minLength + 1 : minLength;
40             return new int[size];
41         } else if (componentType == byte.class) {
42             int bytes = 20 + minLength;
43             int alignedUpBytes = (bytes + 7) & -8;
44             int dataBytes = alignedUpBytes - 20;
45             int size = dataBytes;
46             return new byte[size];
47         } else if (componentType == boolean.class) {
48             int bytes = 20 + minLength;
49             int alignedUpBytes = (bytes + 7) & -8;
50             int dataBytes = alignedUpBytes - 20;
51             int size = dataBytes;
52             return new boolean[size];
53         } else if (componentType == short.class) {
54             int bytes = 20 + (2 * minLength);
55             int alignedUpBytes = (bytes + 7) & -8;
56             int dataBytes = alignedUpBytes - 20;
57             int size = dataBytes / 2;
58             return new short[size];
59         } else if (componentType == float.class) {
60             int size = ((minLength & 1) == 0) ? minLength + 1 : minLength;
61             return new float[size];
62         } else if (componentType == long.class) {
63             return new long[minLength];
64         } else if (componentType == double.class) {
65             return new double[minLength];
66         } else {
67             assert componentType == void.class;
68             throw new IllegalArgumentException("Can't allocate an array of void");
69         }
70     }
71 
72 
getNotifyNativeInterval()73     /*package*/ static int getNotifyNativeInterval() {
74         // This cannot return 0, otherwise it is responsible for triggering an exception
75         // whenever trying to use a NativeAllocationRegistry with size 0
76         return 128; // see art/runtime/gc/heap.h -> kNotifyNativeInterval
77     }
78 }
79