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
31template <typename T>
32inline ZArray<T>::ZArray() :
33 _array(NULL),
34 _size(0),
35 _capacity(0) {}
36
37template <typename T>
38inline ZArray<T>::~ZArray() {
39 if (_array != NULL) {
40 FREE_C_HEAP_ARRAY(T, _array);
41 }
42}
43
44template <typename T>
45inline size_t ZArray<T>::size() const {
46 return _size;
47}
48
49template <typename T>
50inline bool ZArray<T>::is_empty() const {
51 return size() == 0;
52}
53
54template <typename T>
55inline T ZArray<T>::at(size_t index) const {
56 assert(index < _size, "Index out of bounds");
57 return _array[index];
58}
59
60template <typename T>
61inline 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
72template <typename T>
73inline 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
82template <typename T>
83inline void ZArray<T>::transfer(ZArray<T>* from) {
84 assert(_array == NULL, "Should be empty");
85 _array = from->_array;
86 _size = from->_size;
87 _capacity = from->_capacity;
88 from->_array = NULL;
89 from->_size = 0;
90 from->_capacity = 0;
91}
92
93template <typename T>
94inline void ZArray<T>::clear() {
95 _size = 0;
96}
97
98template <typename T, bool parallel>
99inline ZArrayIteratorImpl<T, parallel>::ZArrayIteratorImpl(ZArray<T>* array) :
100 _array(array),
101 _next(0) {}
102
103template <typename T, bool parallel>
104inline bool ZArrayIteratorImpl<T, parallel>::next(T* elem) {
105 if (parallel) {
106 const size_t next = Atomic::add(1u, &_next) - 1u;
107 if (next < _array->size()) {
108 *elem = _array->at(next);
109 return true;
110 }
111 } else {
112 if (_next < _array->size()) {
113 *elem = _array->at(_next++);
114 return true;
115 }
116 }
117
118 // No more elements
119 return false;
120}
121
122#endif // SHARE_GC_Z_ZARRAY_INLINE_HPP
123