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
25#include <atomic>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace ripple {
31
34{
35public:
36 static CountedObjects&
37 getInstance() noexcept;
38
41
42 List
43 getCounts(int minimumThreshold) const;
44
45public:
50 class Counter
51 {
52 public:
53 Counter(std::string name) noexcept : name_(std::move(name)), count_(0)
54 {
55 // Insert ourselves at the front of the lock-free linked list
57 Counter* head;
58
59 do
60 {
61 head = instance.m_head.load();
62 next_ = head;
63 } while (instance.m_head.exchange(this) != head);
64
65 ++instance.m_count;
66 }
67
68 ~Counter() noexcept = default;
69
70 int
71 increment() noexcept
72 {
73 return ++count_;
74 }
75
76 int
77 decrement() noexcept
78 {
79 return --count_;
80 }
81
82 int
83 getCount() const noexcept
84 {
85 return count_.load();
86 }
87
88 Counter*
89 getNext() const noexcept
90 {
91 return next_;
92 }
93
94 std::string const&
95 getName() const noexcept
96 {
97 return name_;
98 }
99
100 private:
104 };
105
106private:
107 CountedObjects() noexcept;
108 ~CountedObjects() noexcept = default;
109
110private:
111 std::atomic<int> m_count;
112 std::atomic<Counter*> m_head;
113};
114
115//------------------------------------------------------------------------------
116
124template <class Object>
126{
127private:
128 static auto&
129 getCounter() noexcept
130 {
131 static CountedObjects::Counter c{beast::type_name<Object>()};
132 return c;
133 }
134
135public:
136 CountedObject() noexcept
137 {
138 getCounter().increment();
139 }
140
142 {
143 getCounter().increment();
144 }
145
147 operator=(CountedObject const&) noexcept = default;
148
149 ~CountedObject() noexcept
150 {
151 getCounter().decrement();
152 }
153};
154
155} // namespace ripple
156
157#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.
Counter(std::string name) noexcept
Counter * getNext() const noexcept
int getCount() const noexcept
std::string const & getName() const noexcept
Manages all counted object types.
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:25
STL namespace.