1 /* 2 * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 // Class Stack (below) grows and shrinks by linking together "segments" which 26 // are allocated on demand. Segments are arrays of the element type (E) plus an 27 // extra pointer-sized field to store the segment link. Recently emptied 28 // segments are kept in a cache and reused. 29 // 30 // Notes/caveats: 31 // 32 // The size of an element must either evenly divide the size of a pointer or be 33 // a multiple of the size of a pointer. 34 // 35 // Destructors are not called for elements popped off the stack, so element 36 // types which rely on destructors for things like reference counting will not 37 // work properly. 38 // 39 // Class Stack allocates segments from the C heap. However, two protected 40 // virtual methods are used to alloc/free memory which subclasses can override: 41 // 42 // virtual void* alloc(size_t bytes); 43 // virtual void free(void* addr, size_t bytes); 44 // 185 186 Stack<E>& stack() const { return _stack; } 187 188 bool is_empty() const { return _cur_seg == NULL; } 189 190 E next() { return *next_addr(); } 191 E* next_addr(); 192 193 void sync(); // Sync the iterator's state to the stack's current state. 194 195 private: 196 Stack<E>& _stack; 197 size_t _cur_seg_size; 198 E* _cur_seg; 199 size_t _full_seg_size; 200 }; 201 202 #ifdef __GNUC__ 203 #undef inline 204 #endif // __GNUC__ | 1 /* 2 * Copyright (c) 2009, 2010, 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_UTILITIES_STACK_HPP 26 #define SHARE_VM_UTILITIES_STACK_HPP 27 28 #include "memory/allocation.inline.hpp" 29 30 // Class Stack (below) grows and shrinks by linking together "segments" which 31 // are allocated on demand. Segments are arrays of the element type (E) plus an 32 // extra pointer-sized field to store the segment link. Recently emptied 33 // segments are kept in a cache and reused. 34 // 35 // Notes/caveats: 36 // 37 // The size of an element must either evenly divide the size of a pointer or be 38 // a multiple of the size of a pointer. 39 // 40 // Destructors are not called for elements popped off the stack, so element 41 // types which rely on destructors for things like reference counting will not 42 // work properly. 43 // 44 // Class Stack allocates segments from the C heap. However, two protected 45 // virtual methods are used to alloc/free memory which subclasses can override: 46 // 47 // virtual void* alloc(size_t bytes); 48 // virtual void free(void* addr, size_t bytes); 49 // 190 191 Stack<E>& stack() const { return _stack; } 192 193 bool is_empty() const { return _cur_seg == NULL; } 194 195 E next() { return *next_addr(); } 196 E* next_addr(); 197 198 void sync(); // Sync the iterator's state to the stack's current state. 199 200 private: 201 Stack<E>& _stack; 202 size_t _cur_seg_size; 203 E* _cur_seg; 204 size_t _full_seg_size; 205 }; 206 207 #ifdef __GNUC__ 208 #undef inline 209 #endif // __GNUC__ 210 211 #endif // SHARE_VM_UTILITIES_STACK_HPP |