Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:58

0001 //===------------------SharedCluster.h --------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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   // The cluster manager is used primarily to manage the
0054   // children of root ValueObjects. So it will always have
0055   // one element - the root.  Pointers will often have dynamic
0056   // values, so having 2 entries is pretty common.  It's also
0057   // pretty common to have small (2,3) structs, so setting the
0058   // static size to 4 will cover those cases with no allocations
0059   // w/o wasting too much space.
0060   llvm::SmallPtrSet<T *, 4> m_objects;
0061   std::mutex m_mutex;
0062 };
0063 
0064 } // namespace lldb_private
0065 
0066 #endif // LLDB_UTILITY_SHAREDCLUSTER_H