Array.h revision 4bc10cc26f856f2447bb23316e6729bcd20c2bf4
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
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/*
100 * Find the synthesized object for the primitive class, generating it
101 * if this is the first reference.
102 */
103ClassObject* dvmFindPrimitiveClass(char type);
104
105/*
106 * Verify that the object is actually an array.
107 *
108 * Does not verify that the object is actually a non-NULL object.
109 */
110INLINE bool dvmIsArray(const ArrayObject* arrayObj)
111{
112    return ( ((Object*)arrayObj)->clazz->descriptor[0] == '[' );
113}
114
115/*
116 * Verify that the class is an array class.
117 *
118 * TODO: there may be some performance advantage to setting a flag in
119 * the accessFlags field instead of chasing into the name string.
120 */
121INLINE bool dvmIsArrayClass(const ClassObject* clazz)
122{
123    return (clazz->descriptor[0] == '[');
124}
125
126/*
127 * Copy the entire contents of one array of objects to another.  If the copy
128 * is impossible because of a type clash, we fail and return "false".
129 *
130 * "dstElemClass" is the type of element that "dstArray" holds.
131 */
132bool dvmCopyObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray,
133    ClassObject* dstElemClass);
134
135/*
136 * Copy the entire contents of an array of boxed primitives into an
137 * array of primitives.  The boxed value must fit in the primitive (i.e.
138 * narrowing conversions are not allowed).
139 */
140bool dvmUnboxObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray,
141    ClassObject* dstElemClass);
142
143#endif /*_DALVIK_OO_ARRAY*/
144