1 /* 2 * Copyright (C) 2021 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 #pragma once 18 19 #include <atomic> 20 #include <memory> 21 #include <vector> 22 23 #include <unwindstack/Memory.h> 24 25 namespace unwindstack { 26 27 class MemoryXz : public Memory { 28 public: 29 MemoryXz(Memory* memory, uint64_t addr, uint64_t size, const std::string& name); 30 ~MemoryXz(); 31 32 bool Init(); Size()33 size_t Size() { return size_; } 34 size_t Read(uint64_t addr, void* dst, size_t size) override; 35 36 // Methods used in tests. MemoryUsage()37 size_t MemoryUsage() { return used_; } BlockCount()38 size_t BlockCount() { return blocks_.size(); } BlockSize()39 size_t BlockSize() { return 1 << block_size_log2_; } 40 41 private: 42 static constexpr size_t kMaxCompressedSize = 1 << 30; // 1GB. Arbitrary. 43 44 struct XzBlock { 45 std::unique_ptr<uint8_t[]> decompressed_data; 46 uint32_t decompressed_size; 47 uint32_t compressed_offset; 48 uint32_t compressed_size; 49 uint16_t stream_flags; 50 }; 51 bool ReadBlocks(); 52 bool Decompress(XzBlock* block); 53 54 // Compressed input. 55 Memory* compressed_memory_; 56 uint64_t compressed_addr_; 57 uint64_t compressed_size_; 58 std::string name_; 59 60 // Decompressed output. 61 std::vector<XzBlock> blocks_; 62 uint32_t used_ = 0; // Memory usage of the currently decompressed blocks. 63 uint32_t size_ = 0; // Decompressed size of all blocks. 64 uint32_t block_size_log2_ = 31; 65 66 // Statistics (used only for optional debug log messages). 67 static std::atomic_size_t total_used_; // Currently decompressed memory (current memory use). 68 static std::atomic_size_t total_size_; // Size of mini-debug-info if it was all decompressed. 69 static std::atomic_size_t total_open_; // Number of mini-debug-info files currently in use. 70 }; 71 72 } // namespace unwindstack 73