1
1
/*
2
- * Copyright (c) 2014, 2019 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2014, 2021 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -43,8 +43,8 @@ class MemoryCounter {
43
43
volatile size_t _count;
44
44
volatile size_t _size;
45
45
46
- DEBUG_ONLY (size_t _peak_count;)
47
- DEBUG_ONLY (size_t _peak_size; )
46
+ DEBUG_ONLY (volatile size_t _peak_count;)
47
+ DEBUG_ONLY (volatile size_t _peak_size; )
48
48
49
49
public:
50
50
MemoryCounter () : _count(0 ), _size(0 ) {
@@ -53,36 +53,40 @@ class MemoryCounter {
53
53
}
54
54
55
55
inline void allocate (size_t sz) {
56
- Atomic::inc (&_count);
56
+ size_t cnt = Atomic::add (&_count, size_t ( 1 ), memory_order_relaxed );
57
57
if (sz > 0 ) {
58
- Atomic::add (&_size, sz);
59
- DEBUG_ONLY (_peak_size = MAX2 (_peak_size, _size));
58
+ size_t sum = Atomic::add (&_size, sz, memory_order_relaxed );
59
+ DEBUG_ONLY (update_peak_size (sum);)
60
60
}
61
- DEBUG_ONLY (_peak_count = MAX2 (_peak_count, _count );)
61
+ DEBUG_ONLY (update_peak_count (cnt );)
62
62
}
63
63
64
64
inline void deallocate (size_t sz) {
65
- assert (_count > 0 , " Nothing allocated yet" );
66
- assert (_size >= sz, " deallocation > allocated" );
67
- Atomic::dec (&_count);
65
+ assert (count () > 0 , " Nothing allocated yet" );
66
+ assert (size () >= sz, " deallocation > allocated" );
67
+ Atomic::dec (&_count, memory_order_relaxed );
68
68
if (sz > 0 ) {
69
- Atomic::sub (&_size, sz);
69
+ Atomic::sub (&_size, sz, memory_order_relaxed );
70
70
}
71
71
}
72
72
73
73
inline void resize (ssize_t sz) {
74
74
if (sz != 0 ) {
75
- assert (sz >= 0 || _size >= size_t (-sz), " Must be" );
76
- Atomic::add (&_size, size_t (sz));
77
- DEBUG_ONLY (_peak_size = MAX2 (_size, _peak_size );)
75
+ assert (sz >= 0 || size () >= size_t (-sz), " Must be" );
76
+ size_t sum = Atomic::add (&_size, size_t (sz), memory_order_relaxed );
77
+ DEBUG_ONLY (update_peak_size (sum );)
78
78
}
79
79
}
80
80
81
- inline size_t count () const { return _count; }
82
- inline size_t size () const { return _size; }
83
- DEBUG_ONLY (inline size_t peak_count () const { return _peak_count; })
84
- DEBUG_ONLY (inline size_t peak_size () const { return _peak_size; })
81
+ inline size_t count () const { return Atomic::load (&_count); }
82
+ inline size_t size () const { return Atomic::load (&_size); }
85
83
84
+ #ifdef ASSERT
85
+ void update_peak_count (size_t cnt);
86
+ void update_peak_size (size_t sz);
87
+ size_t peak_count () const ;
88
+ size_t peak_size () const ;
89
+ #endif // ASSERT
86
90
};
87
91
88
92
/*
0 commit comments