rippled
Loading...
Searching...
No Matches
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 <xrpl/beast/type_name.h>
24#include <atomic>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace ripple {
30
33{
34public:
35 static CountedObjects&
36 getInstance() noexcept;
37
40
41 List
42 getCounts(int minimumThreshold) const;
43
44public:
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
105private:
106 CountedObjects() noexcept;
107 ~CountedObjects() noexcept = default;
108
109private:
110 std::atomic<int> m_count;
111 std::atomic<Counter*> m_head;
112};
113
114//------------------------------------------------------------------------------
115
123template <class Object>
125{
126private:
127 static auto&
128 getCounter() noexcept
129 {
130 static CountedObjects::Counter c{beast::type_name<Object>()};
131 return c;
132 }
133
134public:
135 CountedObject() noexcept
136 {
137 getCounter().increment();
138 }
139
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
Tracks the number of instances of an object.
CountedObject & operator=(CountedObject const &) noexcept=default
CountedObject(CountedObject const &) noexcept
static auto & getCounter() noexcept
Implementation for CountedObject.
Definition: CountedObject.h:50
Counter(std::string name) noexcept
Definition: CountedObject.h:52
Counter * getNext() const noexcept
Definition: CountedObject.h:88
int getCount() const noexcept
Definition: CountedObject.h:82
std::string const & getName() const noexcept
Definition: CountedObject.h:94
Manages all counted object types.
Definition: CountedObject.h:33
std::atomic< Counter * > m_head
std::atomic< int > m_count
static CountedObjects & getInstance() noexcept
List getCounts(int minimumThreshold) const
T load(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
STL namespace.