2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * The VM wraps some additional data structures around the DexFile. These
21 #ifndef DALVIK_DVMDEX_H_
22 #define DALVIK_DVMDEX_H_
25 #include "libdex/DexFile.h"
36 * Some additional VM data structures that are associated with the DEX file.
39 /* pointer to the DexFile we're associated with */
42 /* clone of pDexFile->pHeader (it's used frequently enough) */
43 const DexHeader* pHeader;
45 /* interned strings; parallel to "stringIds" */
46 struct StringObject** pResStrings;
48 /* resolved classes; parallel to "typeIds" */
49 struct ClassObject** pResClasses;
51 /* resolved methods; parallel to "methodIds" */
52 struct Method** pResMethods;
54 /* resolved instance fields; parallel to "fieldIds" */
55 /* (this holds both InstField and StaticField) */
56 struct Field** pResFields;
58 /* interface method lookup cache */
59 struct AtomicCache* pInterfaceCache;
61 /* shared memory region with file contents */
62 bool isMappedReadOnly;
67 /* lock ensuring mutual exclusion during updates */
68 pthread_mutex_t modLock;
73 * Given a file descriptor for an open "optimized" DEX file, map it into
74 * memory and parse the contents.
76 * On success, returns 0 and sets "*ppDvmDex" to a newly-allocated DvmDex.
77 * On failure, returns a meaningful error code [currently just -1].
79 int dvmDexFileOpenFromFd(int fd, DvmDex** ppDvmDex);
82 * Open a partial DEX file. Only useful as part of the optimization process.
84 int dvmDexFileOpenPartial(const void* addr, int len, DvmDex** ppDvmDex);
87 * Free a DvmDex structure, along with any associated structures.
89 void dvmDexFileFree(DvmDex* pDvmDex);
93 * Change the 1- or 2-byte value at the specified address to a new value. If
94 * the location already has the new value, do nothing.
96 * This does not make any synchronization guarantees. The caller must
97 * ensure exclusivity vs. other callers.
99 * For the 2-byte call, the pointer should have 16-bit alignment.
101 * Returns "true" on success.
103 bool dvmDexChangeDex1(DvmDex* pDvmDex, u1* addr, u1 newVal);
104 bool dvmDexChangeDex2(DvmDex* pDvmDex, u2* addr, u2 newVal);
108 * Return the requested item if it has been resolved, or NULL if it hasn't.
110 INLINE struct StringObject* dvmDexGetResolvedString(const DvmDex* pDvmDex,
113 assert(stringIdx < pDvmDex->pHeader->stringIdsSize);
114 return pDvmDex->pResStrings[stringIdx];
116 INLINE struct ClassObject* dvmDexGetResolvedClass(const DvmDex* pDvmDex,
119 assert(classIdx < pDvmDex->pHeader->typeIdsSize);
120 return pDvmDex->pResClasses[classIdx];
122 INLINE struct Method* dvmDexGetResolvedMethod(const DvmDex* pDvmDex,
125 assert(methodIdx < pDvmDex->pHeader->methodIdsSize);
126 return pDvmDex->pResMethods[methodIdx];
128 INLINE struct Field* dvmDexGetResolvedField(const DvmDex* pDvmDex,
131 assert(fieldIdx < pDvmDex->pHeader->fieldIdsSize);
132 return pDvmDex->pResFields[fieldIdx];
136 * Update the resolved item table. Resolution always produces the same
137 * result, so we're not worried about atomicity here.
139 INLINE void dvmDexSetResolvedString(DvmDex* pDvmDex, u4 stringIdx,
140 struct StringObject* str)
142 assert(stringIdx < pDvmDex->pHeader->stringIdsSize);
143 pDvmDex->pResStrings[stringIdx] = str;
145 INLINE void dvmDexSetResolvedClass(DvmDex* pDvmDex, u4 classIdx,
146 struct ClassObject* clazz)
148 assert(classIdx < pDvmDex->pHeader->typeIdsSize);
149 pDvmDex->pResClasses[classIdx] = clazz;
151 INLINE void dvmDexSetResolvedMethod(DvmDex* pDvmDex, u4 methodIdx,
152 struct Method* method)
154 assert(methodIdx < pDvmDex->pHeader->methodIdsSize);
155 pDvmDex->pResMethods[methodIdx] = method;
157 INLINE void dvmDexSetResolvedField(DvmDex* pDvmDex, u4 fieldIdx,
160 assert(fieldIdx < pDvmDex->pHeader->fieldIdsSize);
161 pDvmDex->pResFields[fieldIdx] = field;
164 #endif // DALVIK_DVMDEX_H_