rippled
CountedObject.cpp
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 #include <ripple/basics/CountedObject.h>
21 #include <algorithm>
22 #include <type_traits>
23 
24 namespace ripple {
25 
26 CountedObjects&
28 {
29  static CountedObjects instance;
30 
31  return instance;
32 }
33 
34 CountedObjects::CountedObjects() noexcept : m_count(0), m_head(nullptr)
35 {
36 }
37 
39 CountedObjects::getCounts(int minimumThreshold) const
40 {
41  List counts;
42 
43  // When other operations are concurrent, the count
44  // might be temporarily less than the actual count.
45  counts.reserve(m_count.load());
46 
47  for (auto* ctr = m_head.load(); ctr != nullptr; ctr = ctr->getNext())
48  {
49  if (ctr->getCount() >= minimumThreshold)
50  counts.emplace_back(ctr->getName(), ctr->getCount());
51  }
52 
53  std::sort(counts.begin(), counts.end());
54 
55  return counts;
56 }
57 
58 } // namespace ripple
ripple::CountedObjects::m_count
std::atomic< int > m_count
Definition: CountedObject.h:110
ripple::CountedObjects::m_head
std::atomic< Counter * > m_head
Definition: CountedObject.h:111
std::vector::reserve
T reserve(T... args)
std::vector
STL class.
ripple::CountedObjects::CountedObjects
CountedObjects() noexcept
Definition: CountedObject.cpp:34
std::sort
T sort(T... args)
algorithm
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:27
std::atomic::load
T load(T... args)
ripple::CountedObjects
Manages all counted object types.
Definition: CountedObject.h:32
std::vector::emplace_back
T emplace_back(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::vector::begin
T begin(T... args)
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:39
std::vector::end
T end(T... args)
type_traits