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(!contains(o), "entries must be unique");
34   if (_current >= _limit) {
35     grow();
36   }
37   *_current = o;
38   _current++;
39   validate("post-push");
40 }
41 
42 inline oop LockStack::pop() {
43   validate("pre-pop");
44   oop* new_loc = _current - 1;
45   assert(new_loc < _current, "underflow, probably unbalanced push/pop");
46   _current = new_loc;
47   oop o = *_current;
48   assert(!contains(o), "entries must be unique");
49   validate("post-pop");
50   return o;
51 }
52 
53 inline void LockStack::remove(oop o) {
54   validate("pre-remove");
55   assert(contains(o), "entry must be present");
56   for (oop* loc = _base; loc < _current; loc++) {
57     if (*loc == o) {
58       oop* last = _current - 1;
59       for (; loc < last; loc++) {
60         *loc = *(loc + 1);
61       }
62       _current--;
63       break;
64     }
65   }
66   assert(!contains(o), "entries must be unique: " PTR_FORMAT, p2i(o));
67   validate("post-remove");
68 }
69 
70 inline bool LockStack::contains(oop o) const {
71   validate("pre-contains");
72   bool found = false;
73   size_t i = 0;
74   size_t found_i = 0;
75   for (oop* loc = _current - 1; loc >= _base; loc--) {
76     if (*loc == o) {
77       return true;
78     }
79   }
80   validate("post-contains");
81   return false;
82 }
83 
84 inline void LockStack::oops_do(OopClosure* cl) {
85   validate("pre-oops-do");
86   for (oop* loc = _base; loc < _current; loc++) {
87     cl->do_oop(loc);
88   }
89   validate("post-oops-do");
90 }
91 
92 #endif // SHARE_RUNTIME_LOCKSTACK_INLINE_HPP