1 /*
 2  * Copyright (c) 2016, 2018, 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 #include "precompiled.hpp"
26 #include "code/dependencyContext.hpp"
27 #include "unittest.hpp"
28 
29 class TestDependencyContext {
30  public:
31   char* _buffer[sizeof(nmethod) * 3];
32   nmethod* _nmethods[3];
33 
34   nmethodBucket* volatile _dependency_context;
35   volatile uint64_t _last_cleanup;
36 
37   DependencyContext dependencies() {
38     DependencyContext depContext(&_dependency_context, &_last_cleanup);
39     return depContext;
40   }
41 
42   TestDependencyContext()
43     : _dependency_context(NULL),
44       _last_cleanup(0) {
45     CodeCache_lock->lock_without_safepoint_check();
46 
47     memset(_buffer, 0, sizeof(nmethod) * 3);
48     _nmethods[0] = reinterpret_cast<nmethod*>(_buffer) + 0;
49     _nmethods[1] = reinterpret_cast<nmethod*>(_buffer) + 1;
50     _nmethods[2] = reinterpret_cast<nmethod*>(_buffer) + 2;
51 
52     _nmethods[0]->clear_unloading_state();
53     _nmethods[1]->clear_unloading_state();
54     _nmethods[2]->clear_unloading_state();
55 
56     dependencies().add_dependent_nmethod(_nmethods[2]);
57     dependencies().add_dependent_nmethod(_nmethods[1]);
58     dependencies().add_dependent_nmethod(_nmethods[0]);
59   }
60 
61   ~TestDependencyContext() {
62     wipe();
63     CodeCache_lock->unlock();
64   }
65 
66   void wipe() {
67     DependencyContext ctx(&_dependency_context, &_last_cleanup);
68     nmethodBucket* b = ctx.dependencies();
69     ctx.set_dependencies(NULL);
70     while (b != NULL) {
71       nmethodBucket* next = b->next();
72       delete b;
73       b = next;
74     }
75   }
76 };
77 
78 static void test_remove_dependent_nmethod(int id) {
79   TestDependencyContext c;
80   DependencyContext depContext = c.dependencies();
81 
82   nmethod* nm = c._nmethods[id];
83   depContext.remove_dependent_nmethod(nm);
84 
85   NOT_PRODUCT(ASSERT_FALSE(depContext.is_dependent_nmethod(nm)));
86 }
87 
88 TEST_VM(code, dependency_context) {
89   test_remove_dependent_nmethod(0);
90   test_remove_dependent_nmethod(1);
91   test_remove_dependent_nmethod(2);
92 }