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     GrowableArrayCHeap<T, mtGC>(0) {}
  34 
  35 template <typename T>
  36 inline void ZArray<T>::transfer(ZArray<T>* from) {
  37   assert(this->_data == NULL, "Should be empty");
  38   this->_data = from->_data;
  39   this->_len = from->_len;
  40   this->_max = from->_max;
  41   from->_data = NULL;
  42   from->_len = 0;
  43   from->_max = 0;
  44 }
  45 
  46 template <typename T, bool parallel>
  47 inline ZArrayIteratorImpl<T, parallel>::ZArrayIteratorImpl(ZArray<T>* array) :
  48     _array(array),
  49     _next(0) {}
  50 
  51 template <typename T, bool parallel>
  52 inline bool ZArrayIteratorImpl<T, parallel>::next(T* elem) {
  53   if (parallel) {
  54     const int next = Atomic::fetch_and_add(&_next, 1);
  55     if (next < _array->length()) {
  56       *elem = _array->at(next);
  57       return true;
  58     }
  59   } else {
  60     if (_next < _array->length()) {
  61       *elem = _array->at(_next++);
  62       return true;
  63     }
  64   }
  65 
  66   // No more elements
  67   return false;
  68 }
  69 
  70 template <typename T>
  71 inline ZArrayIterator<T>::ZArrayIterator(ZArray<T>* array) :
  72     ZArrayIteratorImpl<T, ZARRAY_SERIAL>(array) {}
  73 
  74 template <typename T>
  75 inline ZArrayParallelIterator<T>::ZArrayParallelIterator(ZArray<T>* array) :
  76     ZArrayIteratorImpl<T, ZARRAY_PARALLEL>(array) {}
  77 
  78 #endif // SHARE_GC_Z_ZARRAY_INLINE_HPP