jcoomes@2191: /* mikael@4153: * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. jcoomes@2191: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jcoomes@2191: * jcoomes@2191: * This code is free software; you can redistribute it and/or modify it jcoomes@2191: * under the terms of the GNU General Public License version 2 only, as jcoomes@2191: * published by the Free Software Foundation. jcoomes@2191: * jcoomes@2191: * This code is distributed in the hope that it will be useful, but WITHOUT jcoomes@2191: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jcoomes@2191: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jcoomes@2191: * version 2 for more details (a copy is included in the LICENSE file that jcoomes@2191: * accompanied this code). jcoomes@2191: * jcoomes@2191: * You should have received a copy of the GNU General Public License version jcoomes@2191: * 2 along with this work; if not, write to the Free Software Foundation, jcoomes@2191: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jcoomes@2191: * stefank@2314: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA stefank@2314: * or visit www.oracle.com if you need additional information or have any stefank@2314: * questions. jcoomes@2191: * jcoomes@2191: */ jcoomes@2191: stefank@2314: #ifndef SHARE_VM_UTILITIES_STACK_INLINE_HPP stefank@2314: #define SHARE_VM_UTILITIES_STACK_INLINE_HPP stefank@2314: stefank@2314: #include "utilities/stack.hpp" stefank@2314: zgu@3900: template StackBase::StackBase(size_t segment_size, size_t max_cache_size, jcoomes@2191: size_t max_size): jcoomes@2191: _seg_size(segment_size), jcoomes@2191: _max_cache_size(max_cache_size), jcoomes@2191: _max_size(adjust_max_size(max_size, segment_size)) jcoomes@2191: { jcoomes@2191: assert(_max_size % _seg_size == 0, "not a multiple"); jcoomes@2191: } jcoomes@2191: zgu@3900: template size_t StackBase::adjust_max_size(size_t max_size, size_t seg_size) jcoomes@2191: { jcoomes@2191: assert(seg_size > 0, "cannot be 0"); jcoomes@2191: assert(max_size >= seg_size || max_size == 0, "max_size too small"); jcoomes@2191: const size_t limit = max_uintx - (seg_size - 1); jcoomes@2191: if (max_size == 0 || max_size > limit) { jcoomes@2191: max_size = limit; jcoomes@2191: } jcoomes@2191: return (max_size + seg_size - 1) / seg_size * seg_size; jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: Stack::Stack(size_t segment_size, size_t max_cache_size, size_t max_size): zgu@3900: StackBase(adjust_segment_size(segment_size), max_cache_size, max_size) jcoomes@2191: { jcoomes@2191: reset(true); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::push(E item) jcoomes@2191: { jcoomes@2191: assert(!is_full(), "pushing onto a full stack"); zgu@3900: if (this->_cur_seg_size == this->_seg_size) { jcoomes@2191: push_segment(); jcoomes@2191: } zgu@3900: this->_cur_seg[this->_cur_seg_size] = item; zgu@3900: ++this->_cur_seg_size; jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: E Stack::pop() jcoomes@2191: { jcoomes@2191: assert(!is_empty(), "popping from an empty stack"); zgu@3900: if (this->_cur_seg_size == 1) { zgu@3900: E tmp = _cur_seg[--this->_cur_seg_size]; jcoomes@2191: pop_segment(); jcoomes@2191: return tmp; jcoomes@2191: } zgu@3900: return this->_cur_seg[--this->_cur_seg_size]; jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::clear(bool clear_cache) jcoomes@2191: { jcoomes@2191: free_segments(_cur_seg); jcoomes@2191: if (clear_cache) free_segments(_cache); jcoomes@2191: reset(clear_cache); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: size_t Stack::default_segment_size() jcoomes@2191: { jcoomes@2191: // Number of elements that fit in 4K bytes minus the size of two pointers jcoomes@2191: // (link field and malloc header). jcoomes@2191: return (4096 - 2 * sizeof(E*)) / sizeof(E); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: size_t Stack::adjust_segment_size(size_t seg_size) jcoomes@2191: { jcoomes@2191: const size_t elem_sz = sizeof(E); jcoomes@2191: const size_t ptr_sz = sizeof(E*); jcoomes@2191: assert(elem_sz % ptr_sz == 0 || ptr_sz % elem_sz == 0, "bad element size"); jcoomes@2191: if (elem_sz < ptr_sz) { jcoomes@2191: return align_size_up(seg_size * elem_sz, ptr_sz) / elem_sz; jcoomes@2191: } jcoomes@2191: return seg_size; jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: size_t Stack::link_offset() const jcoomes@2191: { zgu@3900: return align_size_up(this->_seg_size * sizeof(E), sizeof(E*)); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: size_t Stack::segment_bytes() const jcoomes@2191: { jcoomes@2191: return link_offset() + sizeof(E*); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: E** Stack::link_addr(E* seg) const jcoomes@2191: { jcoomes@2191: return (E**) ((char*)seg + link_offset()); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: E* Stack::get_link(E* seg) const jcoomes@2191: { jcoomes@2191: return *link_addr(seg); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: E* Stack::set_link(E* new_seg, E* old_seg) jcoomes@2191: { jcoomes@2191: *link_addr(new_seg) = old_seg; jcoomes@2191: return new_seg; jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: E* Stack::alloc(size_t bytes) jcoomes@2191: { zgu@3900: return (E*) NEW_C_HEAP_ARRAY(char, bytes, F); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::free(E* addr, size_t bytes) jcoomes@2191: { zgu@3900: FREE_C_HEAP_ARRAY(char, (char*) addr, F); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::push_segment() jcoomes@2191: { zgu@3900: assert(this->_cur_seg_size == this->_seg_size, "current segment is not full"); jcoomes@2191: E* next; zgu@3900: if (this->_cache_size > 0) { jcoomes@2191: // Use a cached segment. jcoomes@2191: next = _cache; jcoomes@2191: _cache = get_link(_cache); zgu@3900: --this->_cache_size; jcoomes@2191: } else { jcoomes@2191: next = alloc(segment_bytes()); jcoomes@2191: DEBUG_ONLY(zap_segment(next, true);) jcoomes@2191: } jcoomes@2191: const bool at_empty_transition = is_empty(); zgu@3900: this->_cur_seg = set_link(next, _cur_seg); zgu@3900: this->_cur_seg_size = 0; zgu@3900: this->_full_seg_size += at_empty_transition ? 0 : this->_seg_size; jcoomes@2191: DEBUG_ONLY(verify(at_empty_transition);) jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::pop_segment() jcoomes@2191: { zgu@3900: assert(this->_cur_seg_size == 0, "current segment is not empty"); jcoomes@2191: E* const prev = get_link(_cur_seg); zgu@3900: if (this->_cache_size < this->_max_cache_size) { jcoomes@2191: // Add the current segment to the cache. jcoomes@2191: DEBUG_ONLY(zap_segment(_cur_seg, false);) jcoomes@2191: _cache = set_link(_cur_seg, _cache); zgu@3900: ++this->_cache_size; jcoomes@2191: } else { jcoomes@2191: DEBUG_ONLY(zap_segment(_cur_seg, true);) jcoomes@2191: free(_cur_seg, segment_bytes()); jcoomes@2191: } jcoomes@2191: const bool at_empty_transition = prev == NULL; zgu@3900: this->_cur_seg = prev; zgu@3900: this->_cur_seg_size = this->_seg_size; zgu@3900: this->_full_seg_size -= at_empty_transition ? 0 : this->_seg_size; jcoomes@2191: DEBUG_ONLY(verify(at_empty_transition);) jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::free_segments(E* seg) jcoomes@2191: { jcoomes@2191: const size_t bytes = segment_bytes(); jcoomes@2191: while (seg != NULL) { jcoomes@2191: E* const prev = get_link(seg); jcoomes@2191: free(seg, bytes); jcoomes@2191: seg = prev; jcoomes@2191: } jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::reset(bool reset_cache) jcoomes@2191: { zgu@3900: this->_cur_seg_size = this->_seg_size; // So push() will alloc a new segment. zgu@3900: this->_full_seg_size = 0; jcoomes@2191: _cur_seg = NULL; jcoomes@2191: if (reset_cache) { zgu@3900: this->_cache_size = 0; jcoomes@2191: _cache = NULL; jcoomes@2191: } jcoomes@2191: } jcoomes@2191: jcoomes@2191: #ifdef ASSERT zgu@3900: template zgu@3900: void Stack::verify(bool at_empty_transition) const jcoomes@2191: { zgu@3900: assert(size() <= this->max_size(), "stack exceeded bounds"); zgu@3900: assert(this->cache_size() <= this->max_cache_size(), "cache exceeded bounds"); zgu@3900: assert(this->_cur_seg_size <= this->segment_size(), "segment index exceeded bounds"); jcoomes@2191: zgu@3900: assert(this->_full_seg_size % this->_seg_size == 0, "not a multiple"); jcoomes@2191: assert(at_empty_transition || is_empty() == (size() == 0), "mismatch"); zgu@3900: assert((_cache == NULL) == (this->cache_size() == 0), "mismatch"); jcoomes@2191: jcoomes@2191: if (is_empty()) { zgu@3900: assert(this->_cur_seg_size == this->segment_size(), "sanity"); jcoomes@2191: } jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void Stack::zap_segment(E* seg, bool zap_link_field) const jcoomes@2191: { jcoomes@2191: if (!ZapStackSegments) return; jcoomes@2191: const size_t zap_bytes = segment_bytes() - (zap_link_field ? 0 : sizeof(E*)); jcoomes@2191: uint32_t* cur = (uint32_t*)seg; jcoomes@2191: const uint32_t* end = cur + zap_bytes / sizeof(uint32_t); jcoomes@2191: while (cur < end) { jcoomes@2191: *cur++ = 0xfadfaded; jcoomes@2191: } jcoomes@2191: } jcoomes@2191: #endif jcoomes@2191: zgu@3900: template zgu@3900: E* ResourceStack::alloc(size_t bytes) jcoomes@2191: { jcoomes@2191: return (E*) resource_allocate_bytes(bytes); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void ResourceStack::free(E* addr, size_t bytes) jcoomes@2191: { jcoomes@2191: resource_free_bytes((char*) addr, bytes); jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: void StackIterator::sync() jcoomes@2191: { jcoomes@2191: _full_seg_size = _stack._full_seg_size; jcoomes@2191: _cur_seg_size = _stack._cur_seg_size; jcoomes@2191: _cur_seg = _stack._cur_seg; jcoomes@2191: } jcoomes@2191: zgu@3900: template zgu@3900: E* StackIterator::next_addr() jcoomes@2191: { jcoomes@2191: assert(!is_empty(), "no items left"); jcoomes@2191: if (_cur_seg_size == 1) { jcoomes@2191: E* addr = _cur_seg; jcoomes@2191: _cur_seg = _stack.get_link(_cur_seg); jcoomes@2191: _cur_seg_size = _stack.segment_size(); jcoomes@2191: _full_seg_size -= _stack.segment_size(); jcoomes@2191: return addr; jcoomes@2191: } jcoomes@2191: return _cur_seg + --_cur_seg_size; jcoomes@2191: } stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_STACK_INLINE_HPP