13c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta/*
23c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * Copyright (C) 2014 The Android Open Source Project
33c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta *
43c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
53c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * you may not use this file except in compliance with the License.
63c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * You may obtain a copy of the License at
73c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta *
83c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
93c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta *
103c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
113c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
123c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * See the License for the specific language governing permissions and
143c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * limitations under the License.
153c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta */
163c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta
173c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Guptapackage dalvik.system;
183c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta
193c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Guptaimport com.android.tools.layoutlib.annotations.LayoutlibDelegate;
203c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta
213c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta/**
223c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * Delegate used to provide implementation of a select few native methods of {@link VMRuntime}
233c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * <p/>
243c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * Through the layoutlib_create tool, the original native methods of VMRuntime have been replaced
253c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta * by calls to methods of the same name in this delegate class.
263c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta */
273c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Guptapublic class VMRuntime_Delegate {
283c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta
293c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta    // Copied from libcore/libdvm/src/main/java/dalvik/system/VMRuntime
303c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta    @LayoutlibDelegate
313c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta    /*package*/ static Object newUnpaddedArray(VMRuntime runtime, Class<?> componentType,
323c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int minLength) {
333c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        // Dalvik has 32bit pointers, the array header is 16bytes plus 4bytes for dlmalloc,
343c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        // allocations are 8byte aligned so having 4bytes of array data avoids padding.
353c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        if (!componentType.isPrimitive()) {
363c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int size = ((minLength & 1) == 0) ? minLength + 1 : minLength;
373c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return java.lang.reflect.Array.newInstance(componentType, size);
383c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == char.class) {
393c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int bytes = 20 + (2 * minLength);
403c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int alignedUpBytes = (bytes + 7) & -8;
413c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int dataBytes = alignedUpBytes - 20;
423c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int size = dataBytes / 2;
433c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new char[size];
443c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == int.class) {
453c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int size = ((minLength & 1) == 0) ? minLength + 1 : minLength;
463c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new int[size];
473c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == byte.class) {
483c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int bytes = 20 + minLength;
493c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int alignedUpBytes = (bytes + 7) & -8;
503c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int dataBytes = alignedUpBytes - 20;
513c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int size = dataBytes;
523c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new byte[size];
533c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == boolean.class) {
543c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int bytes = 20 + minLength;
553c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int alignedUpBytes = (bytes + 7) & -8;
563c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int dataBytes = alignedUpBytes - 20;
573c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int size = dataBytes;
583c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new boolean[size];
593c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == short.class) {
603c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int bytes = 20 + (2 * minLength);
613c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int alignedUpBytes = (bytes + 7) & -8;
623c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int dataBytes = alignedUpBytes - 20;
633c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int size = dataBytes / 2;
643c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new short[size];
653c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == float.class) {
663c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            int size = ((minLength & 1) == 0) ? minLength + 1 : minLength;
673c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new float[size];
683c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == long.class) {
693c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new long[minLength];
703c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else if (componentType == double.class) {
713c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            return new double[minLength];
723c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        } else {
733c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            assert componentType == void.class;
743c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta            throw new IllegalArgumentException("Can't allocate an array of void");
753c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta        }
763c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta    }
773c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta
783c989eaa0af6e3c4427af4119cf4949b1f05dba2Deepanshu Gupta}
79