--- a/src/hotspot/share/memory/metaspace/counter.hpp Wed Sep 18 07:46:02 2019 +0200
+++ b/src/hotspot/share/memory/metaspace/counter.hpp Thu Sep 19 15:21:27 2019 +0200
@@ -44,19 +44,35 @@
T _c;
+ // Optional name for easier reference
+ const char* const _name;
+
// Only allow unsigned values for now
STATIC_ASSERT(IsSigned<T>::value == false);
public:
- AbstractCounter() : _c(0) {}
+ AbstractCounter(const char* name) : _c(0), _name(name) {}
+ AbstractCounter() : _c(0), _name("") {}
T get() const { return _c; }
- void increment() { assert(_c + 1 > _c, "overflow"); _c ++; }
- void increment_by(T v) { assert(_c + v >= _c, "overflow"); _c += v; }
- void decrement() { assert(_c - 1 < _c, "underflow"); _c --; }
- void decrement_by(T v) { assert(_c - v <= _c, "underflow"); _c -= v; }
+ void increment() { increment_by(1); }
+ void decrement() { decrement_by(1); }
+
+ void increment_by(T v) {
+ assert(_c + v >= _c,
+ "%s overflow (" UINT64_FORMAT "+" UINT64_FORMAT ")",
+ _name, (uint64_t)_c, (uint64_t)v);
+ _c += v;
+ }
+
+ void decrement_by(T v) {
+ assert(_c - v <= _c,
+ "%s underflow (" UINT64_FORMAT "-" UINT64_FORMAT ")",
+ _name, (uint64_t)_c, (uint64_t)v);
+ _c -= v;
+ }
void reset() { _c = 0; }
@@ -78,19 +94,48 @@
volatile T _c;
+ // Optional name for easier reference
+ const char* const _name;
+
// Only allow unsigned values for now
STATIC_ASSERT(IsSigned<T>::value == false);
public:
- AbstractAtomicCounter() : _c(0) {}
+
+ AbstractAtomicCounter(const char* name) : _c(0), _name(name) {}
+ AbstractAtomicCounter() : _c(0), _name("") {}
T get() const { return _c; }
- void increment() { assert(_c + 1 > _c, "overflow"); Atomic::inc(&_c); }
- void increment_by(T v) { assert(_c + v >= _c, "overflow"); Atomic::add(v, &_c); }
- void decrement() { assert(_c - 1 < _c, "underflow"); Atomic::dec(&_c); }
- void decrement_by(T v) { assert(_c - v <= _c, "underflow"); Atomic::sub(v, &_c); }
+ void increment() {
+ assert(_c + 1 != 0,
+ "%s overflow (" UINT64_FORMAT "+1)",
+ _name, (uint64_t)_c);
+ Atomic::inc(&_c);
+ }
+
+ void decrement() {
+ assert(_c >= 1,
+ "%s underflow (" UINT64_FORMAT "-1)",
+ _name, (uint64_t)_c);
+ Atomic::dec(&_c);
+ }
+
+ void increment_by(T v) {
+ T v1 = _c;
+ T v2 = Atomic::add(v, &_c);
+ assert(v2 > v1,
+ "%s overflow (" UINT64_FORMAT "+" UINT64_FORMAT ")",
+ _name, (uint64_t)v1, (uint64_t)v);
+ }
+
+ void decrement_by(T v) {
+ assert(_c >= v,
+ "%s underflow (" UINT64_FORMAT "-" UINT64_FORMAT ")",
+ _name, (uint64_t)_c, (uint64_t)v);
+ Atomic::sub(v, &_c);
+ }
#ifdef ASSERT
void check(T expected) const {