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_VM_UTILITIES_GROWABLEARRAY_HPP
26 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
27
28 #include "memory/allocation.hpp"
29 #include "utilities/debug.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 #include "utilities/ostream.hpp"
32
33 // A growable array.
34
35 /*************************************************************************/
36 /* */
37 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
38 /* */
39 /* Should you use GrowableArrays to contain handles you must be certain */
40 /* the the GrowableArray does not outlive the HandleMark that contains */
41 /* the handles. Since GrowableArrays are typically resource allocated */
42 /* the following is an example of INCORRECT CODE, */
43 /* */
44 /* ResourceMark rm; */
45 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */
46 /* if (blah) { */
47 /* while (...) { */
48 /* HandleMark hm; */
194 GrowableArray() : GenericGrowableArray(2, 0, false) {
195 _data = (E*)raw_allocate(sizeof(E));
196 ::new ((void*)&_data[0]) E();
197 ::new ((void*)&_data[1]) E();
198 }
199
200 // Does nothing for resource and arena objects
201 ~GrowableArray() { if (on_C_heap()) clear_and_deallocate(); }
202
203 void clear() { _len = 0; }
204 int length() const { return _len; }
205 int max_length() const { return _max; }
206 void trunc_to(int l) { assert(l <= _len,"cannot increase length"); _len = l; }
207 bool is_empty() const { return _len == 0; }
208 bool is_nonempty() const { return _len != 0; }
209 bool is_full() const { return _len == _max; }
210 DEBUG_ONLY(E* data_addr() const { return _data; })
211
212 void print();
213
214 int append(const E& elem) {
215 check_nesting();
216 if (_len == _max) grow(_len);
217 int idx = _len++;
218 _data[idx] = elem;
219 return idx;
220 }
221
222 bool append_if_missing(const E& elem) {
223 // Returns TRUE if elem is added.
224 bool missed = !contains(elem);
225 if (missed) append(elem);
226 return missed;
227 }
228
229 E& at(int i) {
230 assert(0 <= i && i < _len, "illegal index");
231 return _data[i];
232 }
233
278 E at_grow(int i, const E& fill = E()) {
279 assert(0 <= i, "negative index");
280 check_nesting();
281 if (i >= _len) {
282 if (i >= _max) grow(i);
283 for (int j = _len; j <= i; j++)
284 _data[j] = fill;
285 _len = i+1;
286 }
287 return _data[i];
288 }
289
290 void at_put_grow(int i, const E& elem, const E& fill = E()) {
291 assert(0 <= i, "negative index");
292 check_nesting();
293 raw_at_put_grow(i, elem, fill);
294 }
295
296 bool contains(const E& elem) const {
297 for (int i = 0; i < _len; i++) {
298 if (_data[i] == elem) return true;
299 }
300 return false;
301 }
302
303 int find(const E& elem) const {
304 for (int i = 0; i < _len; i++) {
305 if (_data[i] == elem) return i;
306 }
307 return -1;
308 }
309
310 int find_from_end(const E& elem) const {
311 for (int i = _len-1; i >= 0; i--) {
312 if (_data[i] == elem) return i;
313 }
314 return -1;
315 }
316
317 int find(void* token, bool f(void*, E)) const {
318 for (int i = 0; i < _len; i++) {
|
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_VM_UTILITIES_GROWABLEARRAY_HPP
26 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
27
28 #include "memory/allocation.hpp"
29 #include "oops/oop.hpp"
30 #include "utilities/debug.hpp"
31 #include "utilities/globalDefinitions.hpp"
32 #include "utilities/ostream.hpp"
33
34 // A growable array.
35
36 /*************************************************************************/
37 /* */
38 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
39 /* */
40 /* Should you use GrowableArrays to contain handles you must be certain */
41 /* the the GrowableArray does not outlive the HandleMark that contains */
42 /* the handles. Since GrowableArrays are typically resource allocated */
43 /* the following is an example of INCORRECT CODE, */
44 /* */
45 /* ResourceMark rm; */
46 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */
47 /* if (blah) { */
48 /* while (...) { */
49 /* HandleMark hm; */
195 GrowableArray() : GenericGrowableArray(2, 0, false) {
196 _data = (E*)raw_allocate(sizeof(E));
197 ::new ((void*)&_data[0]) E();
198 ::new ((void*)&_data[1]) E();
199 }
200
201 // Does nothing for resource and arena objects
202 ~GrowableArray() { if (on_C_heap()) clear_and_deallocate(); }
203
204 void clear() { _len = 0; }
205 int length() const { return _len; }
206 int max_length() const { return _max; }
207 void trunc_to(int l) { assert(l <= _len,"cannot increase length"); _len = l; }
208 bool is_empty() const { return _len == 0; }
209 bool is_nonempty() const { return _len != 0; }
210 bool is_full() const { return _len == _max; }
211 DEBUG_ONLY(E* data_addr() const { return _data; })
212
213 void print();
214
215 inline static bool safe_equals(oop obj1, oop obj2) {
216 return oopDesc::equals(obj1, obj2);
217 }
218
219 template <class X>
220 inline static bool safe_equals(X i1, X i2) {
221 return i1 == i2;
222 }
223
224 int append(const E& elem) {
225 check_nesting();
226 if (_len == _max) grow(_len);
227 int idx = _len++;
228 _data[idx] = elem;
229 return idx;
230 }
231
232 bool append_if_missing(const E& elem) {
233 // Returns TRUE if elem is added.
234 bool missed = !contains(elem);
235 if (missed) append(elem);
236 return missed;
237 }
238
239 E& at(int i) {
240 assert(0 <= i && i < _len, "illegal index");
241 return _data[i];
242 }
243
288 E at_grow(int i, const E& fill = E()) {
289 assert(0 <= i, "negative index");
290 check_nesting();
291 if (i >= _len) {
292 if (i >= _max) grow(i);
293 for (int j = _len; j <= i; j++)
294 _data[j] = fill;
295 _len = i+1;
296 }
297 return _data[i];
298 }
299
300 void at_put_grow(int i, const E& elem, const E& fill = E()) {
301 assert(0 <= i, "negative index");
302 check_nesting();
303 raw_at_put_grow(i, elem, fill);
304 }
305
306 bool contains(const E& elem) const {
307 for (int i = 0; i < _len; i++) {
308 if (safe_equals(_data[i], elem)) return true;
309 }
310 return false;
311 }
312
313 int find(const E& elem) const {
314 for (int i = 0; i < _len; i++) {
315 if (_data[i] == elem) return i;
316 }
317 return -1;
318 }
319
320 int find_from_end(const E& elem) const {
321 for (int i = _len-1; i >= 0; i--) {
322 if (_data[i] == elem) return i;
323 }
324 return -1;
325 }
326
327 int find(void* token, bool f(void*, E)) const {
328 for (int i = 0; i < _len; i++) {
|