jcoomes@2191: /* stefank@2314: * Copyright (c) 2009, 2010, 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_HPP stefank@2314: #define SHARE_VM_UTILITIES_STACK_HPP stefank@2314: stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: jcoomes@2191: // Class Stack (below) grows and shrinks by linking together "segments" which jcoomes@2191: // are allocated on demand. Segments are arrays of the element type (E) plus an jcoomes@2191: // extra pointer-sized field to store the segment link. Recently emptied jcoomes@2191: // segments are kept in a cache and reused. jcoomes@2191: // jcoomes@2191: // Notes/caveats: jcoomes@2191: // jcoomes@2191: // The size of an element must either evenly divide the size of a pointer or be jcoomes@2191: // a multiple of the size of a pointer. jcoomes@2191: // jcoomes@2191: // Destructors are not called for elements popped off the stack, so element jcoomes@2191: // types which rely on destructors for things like reference counting will not jcoomes@2191: // work properly. jcoomes@2191: // jcoomes@2191: // Class Stack allocates segments from the C heap. However, two protected jcoomes@2191: // virtual methods are used to alloc/free memory which subclasses can override: jcoomes@2191: // jcoomes@2191: // virtual void* alloc(size_t bytes); jcoomes@2191: // virtual void free(void* addr, size_t bytes); jcoomes@2191: // jcoomes@2191: // The alloc() method must return storage aligned for any use. The jcoomes@2191: // implementation in class Stack assumes that alloc() will terminate the process jcoomes@2191: // if the allocation fails. jcoomes@2191: jcoomes@2191: template class StackIterator; jcoomes@2191: jcoomes@2191: // StackBase holds common data/methods that don't depend on the element type, jcoomes@2191: // factored out to reduce template code duplication. jcoomes@2191: class StackBase jcoomes@2191: { jcoomes@2191: public: jcoomes@2191: size_t segment_size() const { return _seg_size; } // Elements per segment. jcoomes@2191: size_t max_size() const { return _max_size; } // Max elements allowed. jcoomes@2191: size_t max_cache_size() const { return _max_cache_size; } // Max segments jcoomes@2191: // allowed in cache. jcoomes@2191: jcoomes@2191: size_t cache_size() const { return _cache_size; } // Segments in the cache. jcoomes@2191: jcoomes@2191: protected: jcoomes@2191: // The ctor arguments correspond to the like-named functions above. jcoomes@2191: // segment_size: number of items per segment jcoomes@2191: // max_cache_size: maxmium number of *segments* to cache jcoomes@2191: // max_size: maximum number of items allowed, rounded to a multiple of jcoomes@2191: // the segment size (0 == unlimited) jcoomes@2191: inline StackBase(size_t segment_size, size_t max_cache_size, size_t max_size); jcoomes@2191: jcoomes@2191: // Round max_size to a multiple of the segment size. Treat 0 as unlimited. jcoomes@2191: static inline size_t adjust_max_size(size_t max_size, size_t seg_size); jcoomes@2191: jcoomes@2191: protected: jcoomes@2191: const size_t _seg_size; // Number of items per segment. jcoomes@2191: const size_t _max_size; // Maximum number of items allowed in the stack. jcoomes@2191: const size_t _max_cache_size; // Maximum number of segments to cache. jcoomes@2191: size_t _cur_seg_size; // Number of items in the current segment. jcoomes@2191: size_t _full_seg_size; // Number of items in already-filled segments. jcoomes@2191: size_t _cache_size; // Number of segments in the cache. jcoomes@2191: }; jcoomes@2191: jcoomes@2191: #ifdef __GNUC__ jcoomes@2191: #define inline jcoomes@2191: #endif // __GNUC__ jcoomes@2191: jcoomes@2191: template jcoomes@2191: class Stack: public StackBase jcoomes@2191: { jcoomes@2191: public: jcoomes@2191: friend class StackIterator; jcoomes@2191: jcoomes@2191: // segment_size: number of items per segment jcoomes@2191: // max_cache_size: maxmium number of *segments* to cache jcoomes@2191: // max_size: maximum number of items allowed, rounded to a multiple of jcoomes@2191: // the segment size (0 == unlimited) jcoomes@2191: inline Stack(size_t segment_size = default_segment_size(), jcoomes@2191: size_t max_cache_size = 4, size_t max_size = 0); jcoomes@2191: inline ~Stack() { clear(true); } jcoomes@2191: jcoomes@2191: inline bool is_empty() const { return _cur_seg == NULL; } jcoomes@2191: inline bool is_full() const { return _full_seg_size >= max_size(); } jcoomes@2191: jcoomes@2191: // Performance sensitive code should use is_empty() instead of size() == 0 and jcoomes@2191: // is_full() instead of size() == max_size(). Using a conditional here allows jcoomes@2191: // just one var to be updated when pushing/popping elements instead of two; jcoomes@2191: // _full_seg_size is updated only when pushing/popping segments. jcoomes@2191: inline size_t size() const { jcoomes@2191: return is_empty() ? 0 : _full_seg_size + _cur_seg_size; jcoomes@2191: } jcoomes@2191: jcoomes@2191: inline void push(E elem); jcoomes@2191: inline E pop(); jcoomes@2191: jcoomes@2191: // Clear everything from the stack, releasing the associated memory. If jcoomes@2191: // clear_cache is true, also release any cached segments. jcoomes@2191: void clear(bool clear_cache = false); jcoomes@2191: jcoomes@2191: static inline size_t default_segment_size(); jcoomes@2191: jcoomes@2191: protected: jcoomes@2191: // Each segment includes space for _seg_size elements followed by a link jcoomes@2191: // (pointer) to the previous segment; the space is allocated as a single block jcoomes@2191: // of size segment_bytes(). _seg_size is rounded up if necessary so the link jcoomes@2191: // is properly aligned. The C struct for the layout would be: jcoomes@2191: // jcoomes@2191: // struct segment { jcoomes@2191: // E elements[_seg_size]; jcoomes@2191: // E* link; jcoomes@2191: // }; jcoomes@2191: jcoomes@2191: // Round up seg_size to keep the link field aligned. jcoomes@2191: static inline size_t adjust_segment_size(size_t seg_size); jcoomes@2191: jcoomes@2191: // Methods for allocation size and getting/setting the link. jcoomes@2191: inline size_t link_offset() const; // Byte offset of link field. jcoomes@2191: inline size_t segment_bytes() const; // Segment size in bytes. jcoomes@2191: inline E** link_addr(E* seg) const; // Address of the link field. jcoomes@2191: inline E* get_link(E* seg) const; // Extract the link from seg. jcoomes@2191: inline E* set_link(E* new_seg, E* old_seg); // new_seg.link = old_seg. jcoomes@2191: jcoomes@2191: virtual E* alloc(size_t bytes); jcoomes@2191: virtual void free(E* addr, size_t bytes); jcoomes@2191: jcoomes@2191: void push_segment(); jcoomes@2191: void pop_segment(); jcoomes@2191: jcoomes@2191: void free_segments(E* seg); // Free all segments in the list. jcoomes@2191: inline void reset(bool reset_cache); // Reset all data fields. jcoomes@2191: jcoomes@2191: DEBUG_ONLY(void verify(bool at_empty_transition) const;) jcoomes@2191: DEBUG_ONLY(void zap_segment(E* seg, bool zap_link_field) const;) jcoomes@2191: jcoomes@2191: private: jcoomes@2191: E* _cur_seg; // Current segment. jcoomes@2191: E* _cache; // Segment cache to avoid ping-ponging. jcoomes@2191: }; jcoomes@2191: jcoomes@2191: template class ResourceStack: public Stack, public ResourceObj jcoomes@2191: { jcoomes@2191: public: jcoomes@2191: // If this class becomes widely used, it may make sense to save the Thread jcoomes@2191: // and use it when allocating segments. jcoomes@2191: ResourceStack(size_t segment_size = Stack::default_segment_size()): jcoomes@2191: Stack(segment_size, max_uintx) jcoomes@2191: { } jcoomes@2191: jcoomes@2191: // Set the segment pointers to NULL so the parent dtor does not free them; jcoomes@2191: // that must be done by the ResourceMark code. jcoomes@2191: ~ResourceStack() { Stack::reset(true); } jcoomes@2191: jcoomes@2191: protected: jcoomes@2191: virtual E* alloc(size_t bytes); jcoomes@2191: virtual void free(E* addr, size_t bytes); jcoomes@2191: jcoomes@2191: private: jcoomes@2191: void clear(bool clear_cache = false); jcoomes@2191: }; jcoomes@2191: jcoomes@2191: template jcoomes@2191: class StackIterator: public StackObj jcoomes@2191: { jcoomes@2191: public: jcoomes@2191: StackIterator(Stack& stack): _stack(stack) { sync(); } jcoomes@2191: jcoomes@2191: Stack& stack() const { return _stack; } jcoomes@2191: jcoomes@2191: bool is_empty() const { return _cur_seg == NULL; } jcoomes@2191: jcoomes@2191: E next() { return *next_addr(); } jcoomes@2191: E* next_addr(); jcoomes@2191: jcoomes@2191: void sync(); // Sync the iterator's state to the stack's current state. jcoomes@2191: jcoomes@2191: private: jcoomes@2191: Stack& _stack; jcoomes@2191: size_t _cur_seg_size; jcoomes@2191: E* _cur_seg; jcoomes@2191: size_t _full_seg_size; jcoomes@2191: }; jcoomes@2191: jcoomes@2191: #ifdef __GNUC__ jcoomes@2191: #undef inline jcoomes@2191: #endif // __GNUC__ stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_STACK_HPP