rippled
CountedObject.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_BASICS_COUNTEDOBJECT_H_INCLUDED
21 #define RIPPLE_BASICS_COUNTEDOBJECT_H_INCLUDED
22 
23 #include <ripple/beast/type_name.h>
24 #include <atomic>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 namespace ripple {
30 
33 {
34 public:
35  static CountedObjects&
36  getInstance() noexcept;
37 
40 
41  List
42  getCounts(int minimumThreshold) const;
43 
44 public:
49  class Counter
50  {
51  public:
52  Counter(std::string name) noexcept : name_(std::move(name)), count_(0)
53  {
54  // Insert ourselves at the front of the lock-free linked list
56  Counter* head;
57 
58  do
59  {
60  head = instance.m_head.load();
61  next_ = head;
62  } while (instance.m_head.exchange(this) != head);
63 
64  ++instance.m_count;
65  }
66 
67  ~Counter() noexcept = default;
68 
69  int
70  increment() noexcept
71  {
72  return ++count_;
73  }
74 
75  int
76  decrement() noexcept
77  {
78  return --count_;
79  }
80 
81  int
82  getCount() const noexcept
83  {
84  return count_.load();
85  }
86 
87  Counter*
88  getNext() const noexcept
89  {
90  return next_;
91  }
92 
93  std::string const&
94  getName() const noexcept
95  {
96  return name_;
97  }
98 
99  private:
103  };
104 
105 private:
106  CountedObjects() noexcept;
107  ~CountedObjects() noexcept = default;
108 
109 private:
110  std::atomic<int> m_count;
111  std::atomic<Counter*> m_head;
112 };
113 
114 //------------------------------------------------------------------------------
115 
123 template <class Object>
125 {
126 private:
127  static auto&
128  getCounter() noexcept
129  {
130  static CountedObjects::Counter c{beast::type_name<Object>()};
131  return c;
132  }
133 
134 public:
135  CountedObject() noexcept
136  {
137  getCounter().increment();
138  }
139 
140  CountedObject(CountedObject const&) noexcept
141  {
142  getCounter().increment();
143  }
144 
146  operator=(CountedObject const&) noexcept = default;
147 
148  ~CountedObject() noexcept
149  {
150  getCounter().decrement();
151  }
152 };
153 
154 } // namespace ripple
155 
156 #endif
ripple::CountedObjects::Counter::Counter
Counter(std::string name) noexcept
Definition: CountedObject.h:52
ripple::CountedObjects::Counter::getName
std::string const & getName() const noexcept
Definition: CountedObject.h:94
ripple::CountedObjects::Counter::getNext
Counter * getNext() const noexcept
Definition: CountedObject.h:88
ripple::CountedObjects::m_count
std::atomic< int > m_count
Definition: CountedObject.h:110
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
std::string
STL class.
utility
ripple::CountedObjects::m_head
std::atomic< Counter * > m_head
Definition: CountedObject.h:111
std::pair
ripple::CountedObjects::Counter::name_
const std::string name_
Definition: CountedObject.h:100
vector
ripple::CountedObject::~CountedObject
~CountedObject() noexcept
Definition: CountedObject.h:148
ripple::CountedObjects::CountedObjects
CountedObjects() noexcept
Definition: CountedObject.cpp:34
ripple::CountedObjects::Counter::next_
Counter * next_
Definition: CountedObject.h:102
ripple::CountedObjects::Counter::getCount
int getCount() const noexcept
Definition: CountedObject.h:82
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:27
std::atomic::load
T load(T... args)
ripple::CountedObject::getCounter
static auto & getCounter() noexcept
Definition: CountedObject.h:128
ripple::CountedObjects::Counter::~Counter
~Counter() noexcept=default
atomic
ripple::CountedObjects::Counter
Implementation for CountedObject.
Definition: CountedObject.h:49
ripple::CountedObject::CountedObject
CountedObject(CountedObject const &) noexcept
Definition: CountedObject.h:140
ripple::CountedObjects
Manages all counted object types.
Definition: CountedObject.h:32
ripple::CountedObjects::Counter::increment
int increment() noexcept
Definition: CountedObject.h:70
ripple::CountedObjects::Counter::decrement
int decrement() noexcept
Definition: CountedObject.h:76
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std
STL namespace.
ripple::CountedObject::CountedObject
CountedObject() noexcept
Definition: CountedObject.h:135
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:39
ripple::CountedObjects::Counter::count_
std::atomic< int > count_
Definition: CountedObject.h:101
string