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