1 /* 2 * Copyright (c) 2016, 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 25 #ifndef SHARE_VM_GC_G1_G1FIXEDSIZESTACK_HPP 26 #define SHARE_VM_GC_G1_G1FIXEDSIZESTACK_HPP 27 28 #include "memory/allocation.hpp" 29 #include "utilities/debug.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 32 // Base class for a fixed size stack, i.e. its backing array is of fixed size. Collects 33 // methods common to any instances of the array. The backing array is preallocated 34 // on the C heap. 35 // 36 // Pushing elements and access to elements below _length is MT safe. Any other 37 // operations modifying it are not. 38 class G1FixedSizeStackBase : public CHeapObj<mtGC> { 39 protected: 40 address _base; // The real base address. 41 size_t _max_length; // The length of the array. 42 43 // Current number of elements pushed. The MSB of this value is also used to 44 // indicate that a concurrent writer is currently pushing to it. 45 size_t volatile _length; 46 47 static const size_t LengthMask = ~nth_bit(BitsPerWord - 1); 48 49 protected: 50 51 G1FixedSizeStackBase() : _base(NULL), _max_length(0), _length(0) { } 52 53 // Allocate and initialize the backing array. 54 void initialize(size_t max_length, size_t elem_size); 55 56 void verify_index(size_t index) const PRODUCT_RETURN; 57 58 public: 59 virtual ~G1FixedSizeStackBase(); 60 61 size_t max_length() const { return _max_length; } 62 inline size_t length() const; 63 }; 64 65 template<class T> 66 class G1FixedSizeStack : public G1FixedSizeStackBase { 67 private: 68 T* base() const { return (T*)G1FixedSizeStackBase::_base; } 69 70 // Set the element of the given array at the given index to the 71 // given value. Assume the index is valid. 72 void set_by_index_raw(size_t index, T const value) { 73 this->base()[index] = value; 74 } 75 76 public: 77 // Return the element of the array at the given index. Assume 78 // the index is valid. This is a convenience method that does sanity 79 // checking on the index. 80 T get_by_index(size_t index) const { 81 verify_index(index); 82 return this->base()[index]; 83 } 84 85 void clear() { 86 #ifdef ASSERT 87 _length = _max_length; 88 for (size_t i = 0; i < _max_length; i++) { 89 set_by_index_raw(i, T()); 90 } 91 #endif 92 _length = 0; 93 } 94 95 G1FixedSizeStack() : G1FixedSizeStackBase() {} 96 97 void initialize(size_t max_length) { 98 G1FixedSizeStackBase::initialize(max_length, sizeof(T)); 99 this->clear(); 100 } 101 102 inline void par_push(T const elem); 103 }; 104 105 #endif // SHARE_VM_GC_G1_G1FIXEDSIZESTACK_HPP