1 /*
  2  * Copyright (c) 2007, 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 package nsk.share.gc.gp;
 25 
 26 import java.util.List;
 27 import java.util.ArrayList;
 28 
 29 import nsk.share.gc.Memory;
 30 import nsk.share.gc.gp.array.*;
 31 import nsk.share.gc.gp.string.*;
 32 import nsk.share.test.LocalRandom;
 33 
 34 /**
 35  * Factory for garbage producers
 36  */
 37 public class GarbageProducers {
 38         private List<GarbageProducer> primitiveArrayProducers;
 39         private List<GarbageProducer> valueArrayProducers;
 40         private List<GarbageProducer> arrayProducers;
 41         private List<GarbageProducer<String>> stringProducers;
 42         private List<GarbageProducer> allProducers;
 43 
 44         /**
 45          * Get all primitive array producers.
 46          */
 47         public List<GarbageProducer> getPrimitiveArrayProducers() {
 48                 if (primitiveArrayProducers == null) {
 49                         primitiveArrayProducers = new ArrayList<GarbageProducer>();
 50                         primitiveArrayProducers.add(new ByteArrayProducer());
 51                         primitiveArrayProducers.add(new BooleanArrayProducer());
 52                         primitiveArrayProducers.add(new ShortArrayProducer());
 53                         primitiveArrayProducers.add(new CharArrayProducer());
 54                         primitiveArrayProducers.add(new IntArrayProducer());
 55                         primitiveArrayProducers.add(new LongArrayProducer());
 56                         primitiveArrayProducers.add(new FloatArrayProducer());
 57                         primitiveArrayProducers.add(new DoubleArrayProducer());
 58                 }
 59                 return primitiveArrayProducers;
 60         }
 61 
 62         /**
 63          * Get all primitive array producers.
 64          */
 65         public List<GarbageProducer> getValueArrayProducers() {
 66             if (valueArrayProducers == null) {
 67                 valueArrayProducers = new ArrayList<GarbageProducer>();
 68                 valueArrayProducers.add(new ByteObjArrayProducer());
 69                 valueArrayProducers.add(new BooleanObjArrayProducer());
 70                 valueArrayProducers.add(new IntegerObjArrayProducer());
 71             }
 72             return primitiveArrayProducers;
 73         }
 74         /**
 75          * Get all array producers.
 76          */
 77         public List<GarbageProducer> getArrayProducers() {
 78                 if (arrayProducers == null) {
 79                         arrayProducers = new ArrayList<GarbageProducer>();
 80                         arrayProducers.addAll(getPrimitiveArrayProducers());
 81                         arrayProducers.add(new ObjectArrayProducer());
 82                         if (Memory.isValhallaEnabled()) {
 83                             arrayProducers.addAll(getValueArrayProducers());
 84                         }
 85                 }
 86                 return arrayProducers;
 87         }
 88 
 89         /**
 90          * Get all string producers.
 91          */
 92         public List<GarbageProducer<String>> getStringProducers() {
 93                 if (stringProducers == null) {
 94                         stringProducers = new ArrayList<GarbageProducer<String>>();
 95                         stringProducers.add(new RandomStringProducer());
 96                         stringProducers.add(new InternedStringProducer());
 97                 }
 98                 return stringProducers;
 99         }
100 
101         public List<GarbageProducer> getAllProducers() {
102                 if (allProducers == null) {
103                         allProducers = new ArrayList<GarbageProducer>();
104                         allProducers.addAll(getArrayProducers());
105                         allProducers.addAll(getStringProducers());
106                 }
107                 return allProducers;
108         }
109 }
110 
111  class IntegerObjArrayProducer implements GarbageProducer<Integer[]> {
112     public Integer[] create(long memory) {
113         int size = Memory.getIntegerArrayElementSize();
114         if (!Memory.isValhallaEnabled()) {
115             // Let assume that every Integer is new object
116             size += Memory.getObjectExtraSize();
117         }
118         Integer[] arr = new Integer[Memory.getArrayLength(memory, size)];
119         LocalRandom.nextInts(arr);
120         return arr;
121     }
122 
123     public void validate(Integer[] arr) {
124         LocalRandom.validate(arr);
125     }
126 }
127 
128 
129 class ByteObjArrayProducer implements GarbageProducer<Byte[]> {
130     public Byte[] create(long memory) {
131         Byte[] arr = new Byte[Memory.getArrayLength(memory, Memory.getByteArrayElementSize())];
132         LocalRandom.nextBytes(arr);
133         return arr;
134     }
135 
136     public void validate(Byte[] arr) {
137         LocalRandom.validate(arr);
138     }
139 }
140 
141 class BooleanObjArrayProducer implements GarbageProducer<Boolean[]> {
142     public Boolean[] create(long memory) {
143         Boolean[] arr = new Boolean[Memory.getArrayLength(memory, Memory.getBooleanArrayElementSize())];
144         LocalRandom.nextBooleans(arr);
145         return arr;
146     }
147 
148     public void validate(Boolean[] arr) {
149         LocalRandom.validate(arr);
150     }
151 }