1 /*
 2  * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_RUNTIME_LOCKSTACK_INLINE_HPP
26 #define SHARE_RUNTIME_LOCKSTACK_INLINE_HPP
27 
28 #include "memory/iterator.hpp"
29 #include "runtime/lockStack.hpp"
30 
31 inline void LockStack::push(oop o) {
32   validate("pre-push");
33   assert(oopDesc::is_oop(o), "must be");
34   assert(!contains(o), "entries must be unique");
35   if (_current >= _limit) {
36     grow();
37   }
38   *_current = o;
39   _current++;
40   validate("post-push");
41 }
42 
43 inline oop LockStack::pop() {
44   validate("pre-pop");
45   oop* new_loc = _current - 1;
46   assert(new_loc < _current, "underflow, probably unbalanced push/pop");
47   _current = new_loc;
48   oop o = *_current;
49   assert(!contains(o), "entries must be unique");
50   validate("post-pop");
51   return o;
52 }
53 
54 inline void LockStack::remove(oop o) {
55   validate("pre-remove");
56   assert(contains(o), "entry must be present");
57   for (oop* loc = _base; loc < _current; loc++) {
58     if (*loc == o) {
59       oop* last = _current - 1;
60       for (; loc < last; loc++) {
61         *loc = *(loc + 1);
62       }
63       _current--;
64       break;
65     }
66   }
67   assert(!contains(o), "entries must be unique: " PTR_FORMAT, p2i(o));
68   validate("post-remove");
69 }
70 
71 inline bool LockStack::contains(oop o) const {
72   validate("pre-contains");
73   bool found = false;
74   size_t i = 0;
75   size_t found_i = 0;
76   for (oop* loc = _current - 1; loc >= _base; loc--) {
77     if (*loc == o) {
78       return true;
79     }
80   }
81   validate("post-contains");
82   return false;
83 }
84 
85 inline void LockStack::oops_do(OopClosure* cl) {
86   validate("pre-oops-do");
87   for (oop* loc = _base; loc < _current; loc++) {
88     cl->do_oop(loc);
89   }
90   validate("post-oops-do");
91 }
92 
93 #endif // SHARE_RUNTIME_LOCKSTACK_INLINE_HPP