1 /* 2 * Copyright (c) 2015, 2019, 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 FREE_C_HEAP_ARRAY(T, _array); 40 } 41 42 template <typename T> 43 inline size_t ZArray<T>::size() const { 44 return _size; 45 } 46 47 template <typename T> 48 inline bool ZArray<T>::is_empty() const { 49 return size() == 0; 50 } 51 52 template <typename T> 53 inline T ZArray<T>::at(size_t index) const { 54 assert(index < _size, "Index out of bounds"); 55 return _array[index]; 56 } 57 58 template <typename T> 59 inline void ZArray<T>::expand(size_t new_capacity) { 60 T* new_array = NEW_C_HEAP_ARRAY(T, new_capacity, mtGC); 61 if (_array != NULL) { 62 memcpy(new_array, _array, sizeof(T) * _capacity); 63 FREE_C_HEAP_ARRAY(T, _array); 64 } 65 66 _array = new_array; 67 _capacity = new_capacity; 68 } 69 70 template <typename T> 71 inline void ZArray<T>::add(T value) { 72 if (_size == _capacity) { 73 const size_t new_capacity = (_capacity > 0) ? _capacity * 2 : initial_capacity; 74 expand(new_capacity); 75 } 76 77 _array[_size++] = value; 78 } 79 80 template <typename T> 81 inline void ZArray<T>::transfer(ZArray<T>* from) { 82 assert(_array == NULL, "Should be empty"); 83 _array = from->_array; 84 _size = from->_size; 85 _capacity = from->_capacity; 86 from->_array = NULL; 87 from->_size = 0; 88 from->_capacity = 0; 89 } 90 91 template <typename T> 92 inline void ZArray<T>::clear() { 93 _size = 0; 94 } 95 96 template <typename T, bool parallel> 97 inline ZArrayIteratorImpl<T, parallel>::ZArrayIteratorImpl(ZArray<T>* array) : 98 _array(array), 99 _next(0) {} 100 101 template <typename T, bool parallel> 102 inline bool ZArrayIteratorImpl<T, parallel>::next(T* elem) { 103 if (parallel) { 104 const size_t next = Atomic::add(1u, &_next) - 1u; 105 if (next < _array->size()) { 106 *elem = _array->at(next); 107 return true; 108 } 109 } else { 110 if (_next < _array->size()) { 111 *elem = _array->at(_next++); 112 return true; 113 } 114 } 115 116 // No more elements 117 return false; 118 } 119 120 #endif // SHARE_GC_Z_ZARRAY_INLINE_HPP