EstervQrCode 1.1.1
Library for qr code manipulation
allocator_stats.impl.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 
5 #ifndef OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
6 #define OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
7 
8 #include "./allocator_stats.hpp"
9 
10 //#define OPENCV_DISABLE_ALLOCATOR_STATS
11 
12 #include <atomic>
13 
14 #ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
15 #if defined(__GNUC__) && (\
16  (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 4) || \
17  (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && !defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)) \
18  )
19 #define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE int
20 #endif
21 #endif
22 
23 #ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
24 #define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE long long
25 #endif
26 
27 namespace cv { namespace utils {
28 
29 #ifdef CV__ALLOCATOR_STATS_LOG
30 namespace {
31 #endif
32 
34 {
35 #ifdef OPENCV_DISABLE_ALLOCATOR_STATS
36 
37 public:
40 
41  uint64_t getCurrentUsage() const CV_OVERRIDE { return 0; }
42  uint64_t getTotalUsage() const CV_OVERRIDE { return 0; }
43  uint64_t getNumberOfAllocations() const CV_OVERRIDE { return 0; }
44  uint64_t getPeakUsage() const CV_OVERRIDE { return 0; }
45 
47  void resetPeakUsage() CV_OVERRIDE {};
48 
49  void onAllocate(size_t /*sz*/) {}
50  void onFree(size_t /*sz*/) {}
51 
52 #else
53 
54 protected:
55  typedef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE counter_t;
57 public:
60 
61  uint64_t getCurrentUsage() const CV_OVERRIDE { return (uint64_t)curr.load(); }
62  uint64_t getTotalUsage() const CV_OVERRIDE { return (uint64_t)total.load(); }
63  uint64_t getNumberOfAllocations() const CV_OVERRIDE { return (uint64_t)total_allocs.load(); }
64  uint64_t getPeakUsage() const CV_OVERRIDE { return (uint64_t)peak.load(); }
65 
68 
69  // Controller interface
70  void onAllocate(size_t sz)
71  {
72 #ifdef CV__ALLOCATOR_STATS_LOG
73  CV__ALLOCATOR_STATS_LOG(cv::format("allocate: %lld (curr=%lld)", (long long int)sz, (long long int)curr.load()));
74 #endif
75 
76  counter_t new_curr = curr.fetch_add((counter_t)sz) + (counter_t)sz;
77 
78  // peak = std::max((uint64_t)peak, new_curr);
79  auto prev_peak = peak.load();
80  while (prev_peak < new_curr)
81  {
82  if (peak.compare_exchange_weak(prev_peak, new_curr))
83  break;
84  }
85  // end of peak = max(...)
86 
87  total += (counter_t)sz;
88  total_allocs++;
89  }
90  void onFree(size_t sz)
91  {
92 #ifdef CV__ALLOCATOR_STATS_LOG
93  CV__ALLOCATOR_STATS_LOG(cv::format("free: %lld (curr=%lld)", (long long int)sz, (long long int)curr.load()));
94 #endif
95  curr -= (counter_t)sz;
96  }
97 #endif // OPENCV_DISABLE_ALLOCATOR_STATS
98 };
99 
100 #ifdef CV__ALLOCATOR_STATS_LOG
101 } // namespace
102 #endif
103 
104 }} // namespace
105 
106 #endif // OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
Definition: allocator_stats.hpp:13
Definition: allocator_stats.impl.hpp:34
std::atomic< counter_t > total
Definition: allocator_stats.impl.hpp:56
std::atomic< counter_t > total_allocs
Definition: allocator_stats.impl.hpp:56
uint64_t getTotalUsage() const CV_OVERRIDE
Definition: allocator_stats.impl.hpp:62
void onAllocate(size_t sz)
Definition: allocator_stats.impl.hpp:70
uint64_t getCurrentUsage() const CV_OVERRIDE
Definition: allocator_stats.impl.hpp:61
AllocatorStatistics()
Definition: allocator_stats.impl.hpp:58
std::atomic< counter_t > peak
Definition: allocator_stats.impl.hpp:56
uint64_t getPeakUsage() const CV_OVERRIDE
Definition: allocator_stats.impl.hpp:64
void onFree(size_t sz)
Definition: allocator_stats.impl.hpp:90
uint64_t getNumberOfAllocations() const CV_OVERRIDE
Definition: allocator_stats.impl.hpp:63
void resetPeakUsage() CV_OVERRIDE
Definition: allocator_stats.impl.hpp:67
~AllocatorStatistics() CV_OVERRIDE
Definition: allocator_stats.impl.hpp:59
OPENCV_ALLOCATOR_STATS_COUNTER_TYPE counter_t
Definition: allocator_stats.impl.hpp:55
std::atomic< counter_t > curr
Definition: allocator_stats.impl.hpp:56
T compare_exchange_weak(T... args)
T fetch_add(T... args)
#define CV_OVERRIDE
Definition: cvdef.h:792
CV_EXPORTS String format(const char *fmt,...) CV_FORMAT_PRINTF(1
Returns a text string formatted using the printf-like expression.
T load(T... args)
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
T store(T... args)