Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame^] | 17 | #ifndef ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_ |
| 18 | #define ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_ |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 19 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 20 | #include "base/casts.h" |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 21 | #include "dex_file.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | // CompactDex is a currently ART internal dex file format that aims to reduce storage/RAM usage. |
| 26 | class CompactDexFile : public DexFile { |
| 27 | public: |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 28 | static constexpr uint8_t kDexMagic[kDexMagicSize] = { 'c', 'd', 'e', 'x' }; |
| 29 | static constexpr uint8_t kDexMagicVersion[] = {'0', '0', '1', '\0'}; |
| 30 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 31 | enum class FeatureFlags : uint32_t { |
| 32 | kDefaultMethods = 0x1, |
| 33 | }; |
| 34 | |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 35 | class Header : public DexFile::Header { |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 36 | public: |
| 37 | uint32_t GetFeatureFlags() const { |
| 38 | return feature_flags_; |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | uint32_t feature_flags_ = 0u; |
| 43 | |
| 44 | friend class CompactDexWriter; |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 45 | }; |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 46 | |
| 47 | struct CodeItem : public DexFile::CodeItem { |
| 48 | private: |
| 49 | // TODO: Insert compact dex specific fields here. |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 50 | friend class CompactDexFile; |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 51 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
| 52 | }; |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 53 | |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 54 | // Write the compact dex specific magic. |
| 55 | static void WriteMagic(uint8_t* magic); |
| 56 | |
| 57 | // Write the current version, note that the input is the address of the magic. |
| 58 | static void WriteCurrentVersion(uint8_t* magic); |
| 59 | |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 60 | // Returns true if the byte string points to the magic value. |
| 61 | static bool IsMagicValid(const uint8_t* magic); |
| 62 | virtual bool IsMagicValid() const OVERRIDE; |
| 63 | |
| 64 | // Returns true if the byte string after the magic is the correct value. |
| 65 | static bool IsVersionValid(const uint8_t* magic); |
| 66 | virtual bool IsVersionValid() const OVERRIDE; |
| 67 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 68 | const Header& GetHeader() const { |
| 69 | return down_cast<const Header&>(DexFile::GetHeader()); |
| 70 | } |
| 71 | |
| 72 | virtual bool SupportsDefaultMethods() const OVERRIDE; |
| 73 | |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 74 | uint32_t GetCodeItemSize(const DexFile::CodeItem& item) const OVERRIDE; |
| 75 | |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 76 | private: |
| 77 | // Not supported yet. |
| 78 | CompactDexFile(const uint8_t* base, |
| 79 | size_t size, |
| 80 | const std::string& location, |
| 81 | uint32_t location_checksum, |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 82 | const OatDexFile* oat_dex_file, |
| 83 | DexFileContainer* container) |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 84 | : DexFile(base, |
| 85 | size, |
| 86 | location, |
| 87 | location_checksum, |
| 88 | oat_dex_file, |
| 89 | container, |
| 90 | /*is_compact_dex*/ true) {} |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 91 | |
| 92 | friend class DexFile; |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 93 | friend class DexFileLoader; |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(CompactDexFile); |
| 96 | }; |
| 97 | |
| 98 | } // namespace art |
| 99 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame^] | 100 | #endif // ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_ |