CARLA
 
载入中...
搜索中...
未找到
LifetimeProfiled.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#ifndef LIBCARLA_ENABLE_LIFETIME_PROFILER // 如果没有定义生命周期分析器
8# define LIBCARLA_ENABLE_LIFETIME_PROFILER // 定义生命周期分析器
9#endif // LIBCARLA_ENABLE_LIFETIME_PROFILER // 结束条件编译
10
11#include "carla/Debug.h" // 引入调试头文件
12#include "carla/Logging.h" // 引入日志记录头文件
13#include "carla/profiler/LifetimeProfiled.h" // 引入生命周期分析的头文件
14
15#include <mutex> // 引入互斥锁头文件
16#include <unordered_map> // 引入无序映射头文件
17
18namespace carla { // 定义 carla 命名空间
19namespace profiler { // 定义 profiler 命名空间
20
21 // 日志记录函数模板,用于输出对象生命周期相关的信息
22 template <typename ... Args>
23 static inline void log(Args && ... args) {
24 logging::write_to_stream(std::cerr, "LIFETIME:", std::forward<Args>(args) ..., '\n'); // 将信息写入标准错误流
25 }
26
27 class LifetimeProfiler { // 生命周期分析器类
28 public:
29
30 ~LifetimeProfiler() { // 析构函数
31 std::lock_guard<std::mutex> lock(_mutex); // 加锁以保护共享资源
32 if (!_objects.empty()) { // 如果有未析构的对象
33 log("WARNING! the following objects were not destructed."); // 输出警告信息
34 for (const auto &item : _objects) { // 遍历未析构的对象
35 log(item.second, "still alive."); // 输出每个对象的名称
36 }
37 DEBUG_ERROR; // 记录调试错误
38 }
39 }
40
41 // 注册对象,记录其生命周期
42 void Register(void *object, std::string display_name) {
43#if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_DEBUG // 如果日志级别低于等于调试
44 log('+', display_name); // 输出注册对象的信息
45#endif
46 std::lock_guard<std::mutex> lock(_mutex); // 加锁以保护共享资源
47 _objects.emplace(object, std::move(display_name)); // 将对象及其名称添加到映射中
48 }
49
50 // 注销对象,移除其生命周期记录
51 void Deregister(void *object) {
52 std::lock_guard<std::mutex> lock(_mutex); // 加锁以保护共享资源
53 auto it = _objects.find(object); // 查找对象
54 DEBUG_ASSERT(it != _objects.end()); // 断言对象存在
55#if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_DEBUG // 如果日志级别低于等于调试
56 log('-', it->second); // 输出注销对象的信息
57#endif
58 _objects.erase(it); // 从映射中移除对象
59 }
60
61 private:
62
63 std::mutex _mutex; // 互斥锁,保护共享资源
64
65 std::unordered_map<void *, std::string> _objects; // 存储对象指针和其名称的无序映射
66 };
67
68 static LifetimeProfiler PROFILER; // 创建一个静态的生命周期分析器实例
69
70 LifetimeProfiled::LifetimeProfiled(std::string display_name) { // 生命周期分析类的构造函数
71 PROFILER.Register(this, std::move(display_name)); // 注册当前对象
72 }
73
74 LifetimeProfiled::~LifetimeProfiled() { // 生命周期分析类的析构函数
75 PROFILER.Deregister(this); // 注销当前对象
76 }
77
78} // namespace profiler
79} // namespace carla
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
#define DEBUG_ERROR
Definition Debug.h:70
std::unordered_map< void *, std::string > _objects
void Register(void *object, std::string display_name)
static LIBCARLA_NOINLINE void write_to_stream(std::ostream &out, Arg &&arg, Args &&... args)
Definition Logging.h:52
static void log(Args &&... args)
static LifetimeProfiler PROFILER
CARLA模拟器的主命名空间。
Definition Carla.cpp:139