1 /*
   2  * Copyright (c) 2002, 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 /*
  26  * @test
  27  * @key gc
  28  *
  29  * @summary converted from VM Testbase gc/gctests/nativeGC01.
  30  * VM Testbase keywords: [gc]
  31  * VM Testbase readme:
  32  * This test creates a large linked list of 10,000 elements. After creating the
  33  * linked list a native function is called. The native function calls a java
  34  * method that removes all java references to the linked list and forces a garbage
  35  * collection. The only live reference to the linked list if on the C stack . As
  36  * there is a live reference on the C stack, the linked list should not be garbage collected.
  37  * After forcing a garbage collection, the native code calls another java method
  38  * that traverses the linked list to verify that the linked list has not
  39  * been garbage collected.
  40  *
  41  * @library /vmTestbase
  42  *          /test/lib
  43  * @run driver jdk.test.lib.FileInstaller . .
  44  * @run main/othervm/native gc.gctests.nativeGC01.nativeGC01
  45  */
  46 
  47 package gc.gctests.nativeGC01;
  48 
  49 import nsk.share.test.*;
  50 import nsk.share.gc.*;
  51 import java.util.Vector;
  52 
  53 public class nativeGC01 extends TestBase {
  54         private CircularLinkedList cl;
  55 
  56         public native int nativeMethod01(CircularLinkedList cl);
  57 
  58         // This method is a callback that is invoked by a by the native
  59         // function "nativeMethod()" before the bowels of the nativeMethod()
  60         // function are executed.
  61 
  62         public void callbackGC() {
  63                 cl = null;
  64                 System.gc();
  65         }
  66 
  67         public void run() {
  68                 int elementCount;
  69                 cl = buildBigCircularLinkedList(); // build a 2 meg Circular Linked list.
  70                 // Determine number of elements in the linked list with a native method
  71                 // after GC has been done.
  72 
  73                 try {
  74                         elementCount = nativeMethod01(cl);
  75 
  76                         if (elementCount == 10000) {
  77                                 log.info("Test Passed");
  78                         } else {
  79                                 log.info("Test Failed");
  80                                 setFailed(true);
  81                         }
  82                 } catch (Exception e ) {
  83                         log.info(e);
  84                         log.info("broken test");
  85                         setFailed(true);
  86                 }
  87 
  88         }
  89 
  90         // build a circular linked list of 0.4 Meg
  91         private CircularLinkedList  buildBigCircularLinkedList() {
  92                 CircularLinkedList cl = new CircularLinkedList(100);
  93                 for(int i = 0; i < 10000; i++)
  94                         cl.grow();
  95                 return cl;
  96         }
  97 
  98         static {
  99                 System.loadLibrary("nativeGC01");
 100         }
 101 
 102         public static void main(String args[]){
 103                 Tests.runTest(new nativeGC01(), args);
 104         }
 105 }