Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Utility/SharedCluster.h
Line
Count
Source (jump to first uncovered line)
1
//===------------------SharedCluster.h --------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLDB_UTILITY_SHAREDCLUSTER_H
10
#define LLDB_UTILITY_SHAREDCLUSTER_H
11
12
#include "lldb/Utility/LLDBAssert.h"
13
#include "llvm/ADT/STLExtras.h"
14
#include "llvm/ADT/SmallVector.h"
15
16
#include <memory>
17
#include <mutex>
18
19
namespace lldb_private {
20
21
template <class T>
22
class ClusterManager : public std::enable_shared_from_this<ClusterManager<T>> {
23
public:
24
80.0k
  static std::shared_ptr<ClusterManager> Create() {
25
80.0k
    return std::shared_ptr<ClusterManager>(new ClusterManager());
26
80.0k
  }
27
28
75.2k
  ~ClusterManager() {
29
75.2k
    for (T *obj : m_objects)
30
113k
      delete obj;
31
75.2k
  }
32
33
142k
  void ManageObject(T *new_object) {
34
142k
    std::lock_guard<std::mutex> guard(m_mutex);
35
142k
    assert(!llvm::is_contained(m_objects, new_object) &&
36
142k
           "ManageObject called twice for the same object?");
37
0
    m_objects.push_back(new_object);
38
142k
  }
39
40
653k
  std::shared_ptr<T> GetSharedPointer(T *desired_object) {
41
653k
    std::lock_guard<std::mutex> guard(m_mutex);
42
653k
    auto this_sp = this->shared_from_this();
43
653k
    if (!llvm::is_contained(m_objects, desired_object)) {
44
0
      lldbassert(false && "object not found in shared cluster when expected");
45
0
      desired_object = nullptr;
46
0
    }
47
653k
    return {std::move(this_sp), desired_object};
48
653k
  }
49
50
private:
51
80.0k
  ClusterManager() : m_objects() {}
52
53
  llvm::SmallVector<T *, 16> m_objects;
54
  std::mutex m_mutex;
55
};
56
57
} // namespace lldb_private
58
59
#endif // LLDB_UTILITY_SHAREDCLUSTER_H