blob: 585f8966e3ce43c806305dde12465dae1a74e516 [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
Carl Shapiroae188c62011-04-08 13:11:58 -070022#ifdef __cplusplus
23extern "C" {
24#endif
25
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080026/* 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 */
37ClassObject* 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 */
43ClassObject* 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 */
53ArrayObject* 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 */
62ArrayObject* 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 */
78ArrayObject* 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 */
90ArrayObject* 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 */
100ArrayObject* dvmAllocMultiArray(ClassObject* arrayClass, int curDim,
101 const int* dimensions);
102
103/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800104 * Verify that the object is actually an array.
105 *
106 * Does not verify that the object is actually a non-NULL object.
107 */
108INLINE bool dvmIsArray(const ArrayObject* arrayObj)
109{
110 return ( ((Object*)arrayObj)->clazz->descriptor[0] == '[' );
111}
112
113/*
Carl Shapiro30aa9972010-01-13 22:07:50 -0800114 * 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 McFadden95a884f2010-09-22 12:13:29 -0700118INLINE bool dvmIsObjectArrayClass(const ClassObject* clazz)
Carl Shapiro30aa9972010-01-13 22:07:50 -0800119{
Andy McFadden95a884f2010-09-22 12:13:29 -0700120 const char* descriptor = clazz->descriptor;
Carl Shapiro30aa9972010-01-13 22:07:50 -0800121 return descriptor[0] == '[' && (descriptor[1] == 'L' ||
122 descriptor[1] == '[');
123}
124
125/*
Andy McFadden95a884f2010-09-22 12:13:29 -0700126 * 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 */
130INLINE bool dvmIsObjectArray(const ArrayObject* arrayObj)
131{
132 return dvmIsObjectArrayClass(arrayObj->obj.clazz);
133}
134
135/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800136 * 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 */
141INLINE 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 */
152bool dvmCopyObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray,
153 ClassObject* dstElemClass);
154
Andy McFadden4bc10cc2010-01-13 10:29:44 -0800155/*
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 */
160bool dvmUnboxObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray,
161 ClassObject* dstElemClass);
162
Carl Shapirobfe4dcc2010-04-16 17:55:27 -0700163/*
164 * Returns the size of the given array object in bytes.
165 */
166size_t dvmArrayObjectSize(const ArrayObject *array);
Carl Shapiro1e714bb2010-03-16 03:26:49 -0700167
Andy McFadden95a884f2010-09-22 12:13:29 -0700168/*
169 * Returns the width, in bytes, required by elements in instances of
170 * the array class.
171 */
172size_t dvmArrayClassElementWidth(const ClassObject* clazz);
173
Carl Shapiroae188c62011-04-08 13:11:58 -0700174#ifdef __cplusplus
175}
176#endif
177
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800178#endif /*_DALVIK_OO_ARRAY*/