Array.h revision 375fb116bcb817b37509ab579dbd55cdbb765cbf
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_H_
20#define DALVIK_OO_ARRAY_H_
21
22/*
23 * Find a matching array class.  If it doesn't exist, create it.
24 *
25 * "descriptor" looks like "[I".
26 *
27 * "loader" should be the defining class loader for the elements held
28 * in the array.
29 */
30ClassObject* dvmFindArrayClass(const char* descriptor, Object* loader);
31
32/*
33 * Find the array class for the specified class.  If "elemClassObj" is the
34 * class "Foo", this returns the class object for "[Foo".
35 */
36ClassObject* dvmFindArrayClassForElement(ClassObject* elemClassObj);
37
38/*
39 * Create a new array, given an array class.  The class may represent an
40 * array of references or primitives.
41 *
42 * Returns NULL with an exception raised if allocation fails.
43 */
44extern "C" ArrayObject* dvmAllocArrayByClass(ClassObject* arrayClass,
45    size_t length, int allocFlags);
46
47/*
48 * Allocate an array whose members are primitives (bools, ints, etc.).
49 *
50 * "type" should be 'I', 'J', 'Z', etc.
51 *
52 * The new object will be added to the "tracked alloc" table.
53 *
54 * Returns NULL with an exception raised if allocation fails.
55 */
56ArrayObject* dvmAllocPrimitiveArray(char type, size_t length, int allocFlags);
57
58/*
59 * Allocate an array with multiple dimensions.  Elements may be Objects or
60 * primitive types.
61 *
62 * The base object will be added to the "tracked alloc" table.
63 *
64 * Returns NULL with an exception raised if allocation fails.
65 */
66ArrayObject* dvmAllocMultiArray(ClassObject* arrayClass, int curDim,
67    const int* dimensions);
68
69/*
70 * Verify that the object is actually an array.
71 *
72 * Does not verify that the object is actually a non-NULL object.
73 */
74INLINE bool dvmIsArray(const ArrayObject* arrayObj)
75{
76    return ( ((Object*)arrayObj)->clazz->descriptor[0] == '[' );
77}
78
79/*
80 * Verify that the array is an object array and not a primitive array.
81 *
82 * Does not verify that the object is actually a non-NULL object.
83 */
84INLINE bool dvmIsObjectArrayClass(const ClassObject* clazz)
85{
86    const char* descriptor = clazz->descriptor;
87    return descriptor[0] == '[' && (descriptor[1] == 'L' ||
88                                    descriptor[1] == '[');
89}
90
91/*
92 * Verify that the array is an object array and not a primitive array.
93 *
94 * Does not verify that the object is actually a non-NULL object.
95 */
96INLINE bool dvmIsObjectArray(const ArrayObject* arrayObj)
97{
98    return dvmIsObjectArrayClass(arrayObj->clazz);
99}
100
101/*
102 * Verify that the class is an array class.
103 *
104 * TODO: there may be some performance advantage to setting a flag in
105 * the accessFlags field instead of chasing into the name string.
106 */
107INLINE bool dvmIsArrayClass(const ClassObject* clazz)
108{
109    return (clazz->descriptor[0] == '[');
110}
111
112/*
113 * Copy the entire contents of one array of objects to another.  If the copy
114 * is impossible because of a type clash, we fail and return "false".
115 *
116 * "dstElemClass" is the type of element that "dstArray" holds.
117 */
118bool dvmCopyObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray,
119    ClassObject* dstElemClass);
120
121/*
122 * Copy the entire contents of an array of boxed primitives into an
123 * array of primitives.  The boxed value must fit in the primitive (i.e.
124 * narrowing conversions are not allowed).
125 */
126bool dvmUnboxObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray,
127    ClassObject* dstElemClass);
128
129/*
130 * Returns the size of the given array object in bytes.
131 */
132size_t dvmArrayObjectSize(const ArrayObject *array);
133
134/*
135 * Returns the width, in bytes, required by elements in instances of
136 * the array class.
137 */
138size_t dvmArrayClassElementWidth(const ClassObject* clazz);
139
140#endif  // DALVIK_OO_ARRAY_H_
141