SkipList: fixed infinite loop when one thread inserts a key and another remove the...
[libcds.git] / cds / memory / michael / osalloc_stat.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_MEMORY_MICHAEL_ALLOCATOR_OSALLOC_STAT_H
32 #define CDSLIB_MEMORY_MICHAEL_ALLOCATOR_OSALLOC_STAT_H
33
34 #include <cds/algo/atomic.h>
35
36 namespace cds { namespace memory { namespace michael {
37
38     /// Statistics for large  (allocated directly from %OS) block
39     struct os_allocated_atomic
40     {
41         ///@cond
42         atomics::atomic<size_t>              nAllocCount         ;   ///< Event count of large block allocation from %OS
43         atomics::atomic<size_t>              nFreeCount          ;   ///< Event count of large block deallocation to %OS
44         atomics::atomic<unsigned long long>  nBytesAllocated     ;   ///< Total size of allocated large blocks, in bytes
45         atomics::atomic<unsigned long long>  nBytesDeallocated   ;   ///< Total size of deallocated large blocks, in bytes
46
47         os_allocated_atomic()
48             : nAllocCount(0)
49             , nFreeCount(0)
50             , nBytesAllocated(0)
51             , nBytesDeallocated(0)
52         {}
53         ///@endcond
54
55         /// Adds \p nSize to nBytesAllocated counter
56         void incBytesAllocated( size_t nSize )
57         {
58             nAllocCount.fetch_add( 1, atomics::memory_order_relaxed);
59             nBytesAllocated.fetch_add( nSize, atomics::memory_order_relaxed );
60         }
61
62         /// Adds \p nSize to nBytesDeallocated counter
63         void incBytesDeallocated( size_t nSize )
64         {
65             nFreeCount.fetch_add( 1, atomics::memory_order_relaxed );
66             nBytesDeallocated.fetch_add( nSize, atomics::memory_order_relaxed );
67         }
68
69         /// Returns count of \p alloc and \p alloc_aligned function call (for large block allocated directly from %OS)
70         size_t allocCount() const
71         {
72             return nAllocCount.load(atomics::memory_order_relaxed);
73         }
74
75         /// Returns count of \p free and \p free_aligned function call (for large block allocated directly from %OS)
76         size_t freeCount() const
77         {
78             return nFreeCount.load(atomics::memory_order_relaxed);
79         }
80
81         /// Returns current value of nBytesAllocated counter
82         uint64_t allocatedBytes() const
83         {
84             return nBytesAllocated.load(atomics::memory_order_relaxed);
85         }
86
87         /// Returns current value of nBytesAllocated counter
88         uint64_t deallocatedBytes() const
89         {
90             return nBytesDeallocated.load(atomics::memory_order_relaxed);
91         }
92     };
93
94     /// Dummy statistics for large (allocated directly from %OS) block
95     /**
96         This class does not gather any statistics.
97         Class interface is the same as \ref os_allocated_atomic.
98     */
99     struct os_allocated_empty
100     {
101     //@cond
102         /// Adds \p nSize to nBytesAllocated counter
103         void incBytesAllocated( size_t nSize )
104         { CDS_UNUSED(nSize); }
105
106         /// Adds \p nSize to nBytesDeallocated counter
107         void incBytesDeallocated( size_t nSize )
108         { CDS_UNUSED(nSize); }
109
110         /// Returns count of \p alloc and \p alloc_aligned function call (for large block allocated directly from OS)
111         size_t allocCount() const
112         {
113             return 0;
114         }
115
116         /// Returns count of \p free and \p free_aligned function call (for large block allocated directly from OS)
117         size_t freeCount() const
118         {
119             return 0;
120         }
121
122         /// Returns current value of nBytesAllocated counter
123         uint64_t allocatedBytes() const
124         {
125             return 0;
126         }
127
128         /// Returns current value of nBytesAllocated counter
129         uint64_t deallocatedBytes() const
130         {
131             return 0;
132         }
133     //@endcond
134     };
135
136
137 }}} // namespace cds::memory::michael
138
139 #endif  /// CDSLIB_MEMORY_MICHAEL_ALLOCATOR_OSALLOC_STAT_H