/src/libjxl/tools/tracking_memory_manager.h
Line | Count | Source |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #ifndef TOOLS_TRACKING_MEMORY_MANAGER_H_ |
7 | | #define TOOLS_TRACKING_MEMORY_MANAGER_H_ |
8 | | |
9 | | #include <jxl/memory_manager.h> |
10 | | |
11 | | #include <cstddef> |
12 | | #include <cstdint> |
13 | | #include <cstdio> |
14 | | #include <mutex> |
15 | | #include <unordered_map> |
16 | | |
17 | | #include "lib/jxl/base/status.h" |
18 | | |
19 | | namespace jpegxl { |
20 | | namespace tools { |
21 | | |
22 | | const uint64_t kGiB = 1u << 30; |
23 | | |
24 | | class TrackingMemoryManager { |
25 | | public: |
26 | | explicit TrackingMemoryManager(uint64_t cap = 0, uint64_t total_cap = 0); |
27 | | |
28 | | // void setInner(JxlMemoryManager* inner) { inner_ = inner; } |
29 | | |
30 | 64.4k | JxlMemoryManager* get() { return &outer_; } |
31 | | |
32 | | jxl::Status Reset(); |
33 | | |
34 | | bool seen_oom = false; |
35 | | uint64_t max_bytes_in_use = 0; |
36 | | uint64_t total_allocations = 0; |
37 | | uint64_t total_bytes_allocated = 0; |
38 | | |
39 | | private: |
40 | | static void* Alloc(void* opaque, size_t size); |
41 | | static void Free(void* opaque, void* address); |
42 | | |
43 | | std::unordered_map<void*, size_t> allocations_; |
44 | | std::mutex numbers_mutex_; |
45 | | std::mutex map_mutex_; |
46 | | uint64_t cap_; |
47 | | uint64_t total_cap_; |
48 | | uint64_t bytes_in_use_ = 0; |
49 | | uint64_t num_allocations_ = 0; |
50 | | JxlMemoryManager outer_; |
51 | | JxlMemoryManager default_; |
52 | | JxlMemoryManager* inner_; |
53 | | }; |
54 | | |
55 | | } // namespace tools |
56 | | } // namespace jpegxl |
57 | | |
58 | | #endif // TOOLS_TRACKING_MEMORY_MANAGER_H_ |