Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | * Header file of an in-memory representation of DEX files. |
| 17 | */ |
| 18 | |
| 19 | #ifndef ART_DEXLAYOUT_DEX_CONTAINER_H_ |
| 20 | #define ART_DEXLAYOUT_DEX_CONTAINER_H_ |
| 21 | |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | // Dex container holds the artifacts produced by dexlayout and contains up to two sections: a main |
| 27 | // section and a data section. |
| 28 | // This container may also hold metadata used for multi dex deduplication in the future. |
| 29 | class DexContainer { |
| 30 | public: |
| 31 | virtual ~DexContainer() {} |
| 32 | |
| 33 | class Section { |
| 34 | public: |
| 35 | virtual ~Section() {} |
| 36 | |
| 37 | // Returns the start of the memory region. |
| 38 | virtual uint8_t* Begin() = 0; |
| 39 | |
| 40 | // Size in bytes. |
| 41 | virtual size_t Size() const = 0; |
| 42 | |
| 43 | // Resize the backing storage. |
| 44 | virtual void Resize(size_t size) = 0; |
| 45 | |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 46 | // Clear the container. |
| 47 | virtual void Clear() = 0; |
| 48 | |
Vladimir Marko | 9e4b42a | 2020-04-28 12:28:21 +0100 | [diff] [blame] | 49 | // Release the data, clearing the container contents. |
| 50 | virtual std::vector<uint8_t> ReleaseData() = 0; |
| 51 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 52 | // Returns the end of the memory region. |
| 53 | uint8_t* End() { |
| 54 | return Begin() + Size(); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | // Vector backed section. |
| 59 | class VectorSection : public Section { |
| 60 | public: |
| 61 | virtual ~VectorSection() {} |
| 62 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 63 | uint8_t* Begin() override { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 64 | return &data_[0]; |
| 65 | } |
| 66 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 67 | size_t Size() const override { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 68 | return data_.size(); |
| 69 | } |
| 70 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 71 | void Resize(size_t size) override { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 72 | data_.resize(size, 0u); |
| 73 | } |
| 74 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 75 | void Clear() override { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 76 | data_.clear(); |
| 77 | } |
| 78 | |
Vladimir Marko | 9e4b42a | 2020-04-28 12:28:21 +0100 | [diff] [blame] | 79 | std::vector<uint8_t> ReleaseData() override { |
| 80 | std::vector<uint8_t> temp; |
| 81 | temp.swap(data_); |
| 82 | return temp; |
| 83 | } |
| 84 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 85 | private: |
| 86 | std::vector<uint8_t> data_; |
| 87 | }; |
| 88 | |
| 89 | virtual Section* GetMainSection() = 0; |
| 90 | virtual Section* GetDataSection() = 0; |
| 91 | virtual bool IsCompactDexContainer() const = 0; |
| 92 | }; |
| 93 | |
| 94 | } // namespace art |
| 95 | |
| 96 | #endif // ART_DEXLAYOUT_DEX_CONTAINER_H_ |