File indexing completed on 2026-05-10 08:42:58
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLDB_UTILITY_SHAREDCLUSTER_H
0010 #define LLDB_UTILITY_SHAREDCLUSTER_H
0011
0012 #include "lldb/Utility/LLDBAssert.h"
0013 #include "llvm/ADT/STLExtras.h"
0014 #include "llvm/ADT/SmallPtrSet.h"
0015
0016 #include <memory>
0017 #include <mutex>
0018
0019 namespace lldb_private {
0020
0021 template <class T>
0022 class ClusterManager : public std::enable_shared_from_this<ClusterManager<T>> {
0023 public:
0024 static std::shared_ptr<ClusterManager> Create() {
0025 return std::shared_ptr<ClusterManager>(new ClusterManager());
0026 }
0027
0028 ~ClusterManager() {
0029 for (T *obj : m_objects)
0030 delete obj;
0031 }
0032
0033 void ManageObject(T *new_object) {
0034 std::lock_guard<std::mutex> guard(m_mutex);
0035 auto ret = m_objects.insert(new_object);
0036 assert(ret.second && "ManageObject called twice for the same object?");
0037 (void)ret;
0038 }
0039
0040 std::shared_ptr<T> GetSharedPointer(T *desired_object) {
0041 std::lock_guard<std::mutex> guard(m_mutex);
0042 auto this_sp = this->shared_from_this();
0043 size_t count = m_objects.count(desired_object);
0044 if (count == 0) {
0045 lldbassert(false && "object not found in shared cluster when expected");
0046 desired_object = nullptr;
0047 }
0048 return {std::move(this_sp), desired_object};
0049 }
0050
0051 private:
0052 ClusterManager() : m_objects() {}
0053
0054
0055
0056
0057
0058
0059
0060 llvm::SmallPtrSet<T *, 4> m_objects;
0061 std::mutex m_mutex;
0062 };
0063
0064 }
0065
0066 #endif