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