ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
MemoryManagerStatistic.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstddef>
11 #include <map>
12 #include <mutex>
13 #include <unordered_map>
14 
16 
17 namespace cloudViewer {
18 namespace core {
19 
21 public:
22  enum class PrintLevel {
24  All = 0,
27  Unbalanced = 1,
29  None = 2,
30  };
31 
33 
36 
38 
40  void SetPrintLevel(PrintLevel level);
41 
45  void SetPrintAtProgramEnd(bool print);
46 
48  void SetPrintAtMallocFree(bool print);
49 
51  void Print() const;
52 
55  bool HasLeaks() const;
56 
58  void CountMalloc(void* ptr, size_t byte_size, const Device& device);
59 
63  void CountFree(void* ptr, const Device& device);
64 
66  void Reset();
67 
68 private:
69  MemoryManagerStatistic() = default;
70 
71  struct MemoryStatistics {
72  bool IsBalanced() const;
73 
74  int64_t count_malloc_ = 0;
75  int64_t count_free_ = 0;
76  std::unordered_map<void*, size_t> active_allocations_;
77  };
78 
81 
84  bool print_at_program_end_ = true;
85 
87  bool print_at_malloc_free_ = false;
88 
89  std::mutex statistics_mutex_;
90  std::map<Device, MemoryStatistics> statistics_;
91 };
92 
93 } // namespace core
94 } // namespace cloudViewer
@ All
Statistics for all used devices are printed.
MemoryManagerStatistic & operator=(MemoryManagerStatistic &)=delete
void SetPrintAtMallocFree(bool print)
Enables or disables printing at each malloc and free.
void Print() const
Prints statistics for all recorded devices depending on the print level.
void SetPrintLevel(PrintLevel level)
Sets the level of provided information for printing.
void CountFree(void *ptr, const Device &device)
static MemoryManagerStatistic & GetInstance()
void CountMalloc(void *ptr, size_t byte_size, const Device &device)
Adds the given allocation to the statistics.
MemoryManagerStatistic(const MemoryManagerStatistic &)=delete
Generic file read and write utility for python interface.
#define IsBalanced
Definition: rename.h:110