src/hotspot/share/memory/metaspace/counter.hpp
branchstuefe-new-metaspace-branch
changeset 58063 bdf136b8ae0e
child 58227 0e7d9a23261e
equal deleted inserted replaced
58062:65cad575ace3 58063:bdf136b8ae0e
       
     1 /*
       
     2  * Copyright (c) 2019, SAP SE. All rights reserved.
       
     3  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     5  *
       
     6  * This code is free software; you can redistribute it and/or modify it
       
     7  * under the terms of the GNU General Public License version 2 only, as
       
     8  * published by the Free Software Foundation.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13  * version 2 for more details (a copy is included in the LICENSE file that
       
    14  * accompanied this code).
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License version
       
    17  * 2 along with this work; if not, write to the Free Software Foundation,
       
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19  *
       
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    21  * or visit www.oracle.com if you need additional information or have any
       
    22  * questions.
       
    23  *
       
    24  */
       
    25 
       
    26 #ifndef SHARE_MEMORY_METASPACE_COUNTER_HPP
       
    27 #define SHARE_MEMORY_METASPACE_COUNTER_HPP
       
    28 
       
    29 #include "metaprogramming/isSigned.hpp"
       
    30 #include "runtime/atomic.hpp"
       
    31 #include "utilities/debug.hpp"
       
    32 #include "utilities/globalDefinitions.hpp"
       
    33 
       
    34 
       
    35 namespace metaspace {
       
    36 
       
    37 // A very simple helper class which counts something, offers decrement/increment
       
    38 // methods and checks for overflow/underflow on increment/decrement.
       
    39 //
       
    40 // (since we seem to do that alot....)
       
    41 
       
    42 template <class T>
       
    43 class AbstractCounter {
       
    44 
       
    45   T _c;
       
    46 
       
    47   // Only allow unsigned values for now
       
    48   STATIC_ASSERT(IsSigned<T>::value == false);
       
    49 
       
    50 public:
       
    51 
       
    52   AbstractCounter() : _c(0) {}
       
    53 
       
    54   T get() const           { return _c; }
       
    55 
       
    56   void increment()            { assert(_c + 1 > _c, "overflow"); _c ++; }
       
    57   void increment_by(T v)      { assert(_c + v >= _c, "overflow"); _c += v; }
       
    58   void decrement()            { assert(_c - 1 < _c, "underflow"); _c --; }
       
    59   void decrement_by(T v)      { assert(_c - v <= _c, "underflow"); _c -= v; }
       
    60 
       
    61   void reset()                { _c = 0; }
       
    62 
       
    63 #ifdef ASSERT
       
    64   void check(T expected) const {
       
    65     assert(_c == expected, "Counter mismatch: %d, expected: %d.",
       
    66            (int)_c, (int)expected);
       
    67     }
       
    68 #endif
       
    69 
       
    70 };
       
    71 
       
    72 typedef AbstractCounter<size_t>   SizeCounter;
       
    73 typedef AbstractCounter<unsigned> IntCounter;
       
    74 
       
    75 
       
    76 template <class T>
       
    77 class AbstractAtomicCounter {
       
    78 
       
    79   volatile T _c;
       
    80 
       
    81   // Only allow unsigned values for now
       
    82   STATIC_ASSERT(IsSigned<T>::value == false);
       
    83 
       
    84 public:
       
    85 
       
    86   AbstractAtomicCounter() : _c(0) {}
       
    87 
       
    88   T get() const               { return _c; }
       
    89 
       
    90   void increment()            { assert(_c + 1 > _c, "overflow"); Atomic::inc(&_c); }
       
    91   void increment_by(T v)      { assert(_c + v >= _c, "overflow"); Atomic::add(v, &_c); }
       
    92   void decrement()            { assert(_c - 1 < _c, "underflow"); Atomic::dec(&_c); }
       
    93   void decrement_by(T v)      { assert(_c - v <= _c, "underflow"); Atomic::sub(v, &_c); }
       
    94 
       
    95 #ifdef ASSERT
       
    96   void check(T expected) const {
       
    97     assert(_c == expected, "Counter mismatch: %d, expected: %d.",
       
    98            (int)_c, (int)expected);
       
    99     }
       
   100 #endif
       
   101 
       
   102 };
       
   103 
       
   104 typedef AbstractAtomicCounter<size_t> SizeAtomicCounter;
       
   105 
       
   106 
       
   107 
       
   108 } // namespace metaspace
       
   109 
       
   110 #endif // SHARE_MEMORY_METASPACE_WORDSIZECOUNTER_HPP
       
   111