1 /* 2 * Copyright (c) 2015, 2017, 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 #ifndef SHARE_GC_Z_ZARRAY_INLINE_HPP 25 #define SHARE_GC_Z_ZARRAY_INLINE_HPP 26 27 #include "gc/z/zArray.hpp" 28 #include "memory/allocation.inline.hpp" 29 #include "runtime/atomic.hpp" 30 31 template <typename T> 32 inline ZArray<T>::ZArray() : 33 _array(NULL), 34 _size(0), 35 _capacity(0) {} 36 37 template <typename T> 38 inline ZArray<T>::~ZArray() { 39 if (_array != NULL) { 40 FREE_C_HEAP_ARRAY(T, _array); 41 } 42 } 43 44 template <typename T> 45 inline size_t ZArray<T>::size() const { 46 return _size; 47 } 48 49 template <typename T> 50 inline bool ZArray<T>::is_empty() const { 51 return size() == 0; 52 } 53 54 template <typename T> 55 inline T ZArray<T>::at(size_t index) const { 56 assert(index < _size, "Index out of bounds"); 57 return _array[index]; 58 } 59 60 template <typename T> 61 inline void ZArray<T>::expand(size_t new_capacity) { 62 T* new_array = NEW_C_HEAP_ARRAY(T, new_capacity, mtGC); 63 if (_array != NULL) { 64 memcpy(new_array, _array, sizeof(T) * _capacity); 65 FREE_C_HEAP_ARRAY(T, _array); 66 } 67 68 _array = new_array; 69 _capacity = new_capacity; 70 } 71 72 template <typename T> 73 inline void ZArray<T>::add(T value) { 74 if (_size == _capacity) { 75 const size_t new_capacity = (_capacity > 0) ? _capacity * 2 : initial_capacity; 76 expand(new_capacity); 77 } 78 79 _array[_size++] = value; 80 } 81 82 template <typename T> 83 inline void ZArray<T>::clear() { 84 _size = 0; 85 } 86 87 template <typename T, bool parallel> 88 inline ZArrayIteratorImpl<T, parallel>::ZArrayIteratorImpl(ZArray<T>* array) : 89 _array(array), 90 _next(0) {} 91 92 template <typename T, bool parallel> 93 inline bool ZArrayIteratorImpl<T, parallel>::next(T* elem) { 94 if (parallel) { 95 const size_t next = Atomic::add(1u, &_next) - 1u; 96 if (next < _array->size()) { 97 *elem = _array->at(next); 98 return true; 99 } 100 } else { 101 if (_next < _array->size()) { 102 *elem = _array->at(_next++); 103 return true; 104 } 105 } 106 107 // No more elements 108 return false; 109 } 110 111 #endif // SHARE_GC_Z_ZARRAY_INLINE_HPP