| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | * Array handling. |
| 18 | */ |
| 19 | #ifndef _DALVIK_OO_ARRAY |
| 20 | #define _DALVIK_OO_ARRAY |
| 21 | |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame^] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 26 | /* width of an object reference, for arrays of objects */ |
| 27 | #define kObjectArrayRefWidth sizeof(Object*) |
| 28 | |
| 29 | /* |
| 30 | * Find a matching array class. If it doesn't exist, create it. |
| 31 | * |
| 32 | * "descriptor" looks like "[I". |
| 33 | * |
| 34 | * "loader" should be the defining class loader for the elements held |
| 35 | * in the array. |
| 36 | */ |
| 37 | ClassObject* dvmFindArrayClass(const char* descriptor, Object* loader); |
| 38 | |
| 39 | /* |
| 40 | * Find the array class for the specified class. If "elemClassObj" is the |
| 41 | * class "Foo", this returns the class object for "[Foo". |
| 42 | */ |
| 43 | ClassObject* dvmFindArrayClassForElement(ClassObject* elemClassObj); |
| 44 | |
| 45 | /* |
| 46 | * Allocate space for a new array object. |
| 47 | * |
| 48 | * "allocFlags" determines whether the new object will be added to the |
| 49 | * "tracked alloc" table. |
| 50 | * |
| 51 | * Returns NULL with an exception raised if allocation fails. |
| 52 | */ |
| 53 | ArrayObject* dvmAllocArray(ClassObject* arrayClass, size_t length, |
| 54 | size_t elemWidth, int allocFlags); |
| 55 | |
| 56 | /* |
| 57 | * Create a new array, given an array class. The class may represent an |
| 58 | * array of references or primitives. |
| 59 | * |
| 60 | * Returns NULL with an exception raised if allocation fails. |
| 61 | */ |
| 62 | ArrayObject* dvmAllocArrayByClass(ClassObject* arrayClass, |
| 63 | size_t length, int allocFlags); |
| 64 | |
| 65 | /* |
| 66 | * Create a new array that holds references to members of the specified class. |
| 67 | * |
| 68 | * "elemClassObj" is the element type, and may itself be an array class. It |
| 69 | * may not be a primitive class. |
| 70 | * |
| 71 | * "allocFlags" determines whether the new object will be added to the |
| 72 | * "tracked alloc" table. |
| 73 | * |
| 74 | * This is less efficient than dvmAllocArray(), but occasionally convenient. |
| 75 | * |
| 76 | * Returns NULL with an exception raised if allocation fails. |
| 77 | */ |
| 78 | ArrayObject* dvmAllocObjectArray(ClassObject* elemClassObj, size_t length, |
| 79 | int allocFlags); |
| 80 | |
| 81 | /* |
| 82 | * Allocate an array whose members are primitives (bools, ints, etc.). |
| 83 | * |
| 84 | * "type" should be 'I', 'J', 'Z', etc. |
| 85 | * |
| 86 | * The new object will be added to the "tracked alloc" table. |
| 87 | * |
| 88 | * Returns NULL with an exception raised if allocation fails. |
| 89 | */ |
| 90 | ArrayObject* dvmAllocPrimitiveArray(char type, size_t length, int allocFlags); |
| 91 | |
| 92 | /* |
| 93 | * Allocate an array with multiple dimensions. Elements may be Objects or |
| 94 | * primitive types. |
| 95 | * |
| 96 | * The base object will be added to the "tracked alloc" table. |
| 97 | * |
| 98 | * Returns NULL with an exception raised if allocation fails. |
| 99 | */ |
| 100 | ArrayObject* dvmAllocMultiArray(ClassObject* arrayClass, int curDim, |
| 101 | const int* dimensions); |
| 102 | |
| 103 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 104 | * Verify that the object is actually an array. |
| 105 | * |
| 106 | * Does not verify that the object is actually a non-NULL object. |
| 107 | */ |
| 108 | INLINE bool dvmIsArray(const ArrayObject* arrayObj) |
| 109 | { |
| 110 | return ( ((Object*)arrayObj)->clazz->descriptor[0] == '[' ); |
| 111 | } |
| 112 | |
| 113 | /* |
| Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 114 | * Verify that the array is an object array and not a primitive array. |
| 115 | * |
| 116 | * Does not verify that the object is actually a non-NULL object. |
| 117 | */ |
| Andy McFadden | 95a884f | 2010-09-22 12:13:29 -0700 | [diff] [blame] | 118 | INLINE bool dvmIsObjectArrayClass(const ClassObject* clazz) |
| Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 119 | { |
| Andy McFadden | 95a884f | 2010-09-22 12:13:29 -0700 | [diff] [blame] | 120 | const char* descriptor = clazz->descriptor; |
| Carl Shapiro | 30aa997 | 2010-01-13 22:07:50 -0800 | [diff] [blame] | 121 | return descriptor[0] == '[' && (descriptor[1] == 'L' || |
| 122 | descriptor[1] == '['); |
| 123 | } |
| 124 | |
| 125 | /* |
| Andy McFadden | 95a884f | 2010-09-22 12:13:29 -0700 | [diff] [blame] | 126 | * Verify that the array is an object array and not a primitive array. |
| 127 | * |
| 128 | * Does not verify that the object is actually a non-NULL object. |
| 129 | */ |
| 130 | INLINE bool dvmIsObjectArray(const ArrayObject* arrayObj) |
| 131 | { |
| 132 | return dvmIsObjectArrayClass(arrayObj->obj.clazz); |
| 133 | } |
| 134 | |
| 135 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 136 | * Verify that the class is an array class. |
| 137 | * |
| 138 | * TODO: there may be some performance advantage to setting a flag in |
| 139 | * the accessFlags field instead of chasing into the name string. |
| 140 | */ |
| 141 | INLINE bool dvmIsArrayClass(const ClassObject* clazz) |
| 142 | { |
| 143 | return (clazz->descriptor[0] == '['); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Copy the entire contents of one array of objects to another. If the copy |
| 148 | * is impossible because of a type clash, we fail and return "false". |
| 149 | * |
| 150 | * "dstElemClass" is the type of element that "dstArray" holds. |
| 151 | */ |
| 152 | bool dvmCopyObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray, |
| 153 | ClassObject* dstElemClass); |
| 154 | |
| Andy McFadden | 4bc10cc | 2010-01-13 10:29:44 -0800 | [diff] [blame] | 155 | /* |
| 156 | * Copy the entire contents of an array of boxed primitives into an |
| 157 | * array of primitives. The boxed value must fit in the primitive (i.e. |
| 158 | * narrowing conversions are not allowed). |
| 159 | */ |
| 160 | bool dvmUnboxObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray, |
| 161 | ClassObject* dstElemClass); |
| 162 | |
| Carl Shapiro | bfe4dcc | 2010-04-16 17:55:27 -0700 | [diff] [blame] | 163 | /* |
| 164 | * Returns the size of the given array object in bytes. |
| 165 | */ |
| 166 | size_t dvmArrayObjectSize(const ArrayObject *array); |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 167 | |
| Andy McFadden | 95a884f | 2010-09-22 12:13:29 -0700 | [diff] [blame] | 168 | /* |
| 169 | * Returns the width, in bytes, required by elements in instances of |
| 170 | * the array class. |
| 171 | */ |
| 172 | size_t dvmArrayClassElementWidth(const ClassObject* clazz); |
| 173 | |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame^] | 174 | #ifdef __cplusplus |
| 175 | } |
| 176 | #endif |
| 177 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 178 | #endif /*_DALVIK_OO_ARRAY*/ |