1
/*
2
 * $Id$
3
 *
4
 * Copyright (C) 2003-2009 JNode.org
5
 *
6
 * This library is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful, but 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this library; If not, write to the Free Software Foundation, Inc., 
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
 */
20
 
21
package org.jnode.vm.memmgr.generational;
22
23
import org.jnode.vm.VmSystemObject;
24
import org.jnode.annotation.Inline;
25
import org.vmmagic.pragma.Uninterruptible;
26
27
/**
28
 * @author epr
29
 */
30
final class GenGCStack extends VmSystemObject implements Uninterruptible {
31
32
    /**
33
     * The default size of a stack
34
     */
35
    public static final int DEFAULT_STACK_SIZE = 4096;
36
    /**
37
     * The actual stack
38
     */
39
    private final Object[] stack;
40
    /**
41
     * The size of the stack (in objects)
42
     */
43
    private final int size;
44
    /**
45
     * The stackpointer
46
     */
47
    private int stackPtr;
48
    /**
49
     * Has the stack occurred an overflow?
50
     */
51
    private boolean overflow;
52
53
    /**
54
     * Create a new instance
55
     */
56
    public GenGCStack() {
57
        this.stack = new Object[DEFAULT_STACK_SIZE];
58
        this.size = stack.length;
59
    }
60
61
    /**
62
     * Push a given object on the stack. If an overflow occurs, mark
63
     * the overflow and do not push the object.
64
     *
65
     * @param object
66
     */
67
    @Inline
68
    public final void push(Object object) {
69
        if (object == null) {
70
            throw new IllegalArgumentException("Cannot push null object");
71
        }
72
        if (stackPtr == size) {
73
            overflow = true;
74
        } else {
75
            stack[stackPtr++] = object;
76
        }
77
    }
78
79
    /**
80
     * Gets the last pushed object of the stack and remove it from the stack.
81
     *
82
     * @return The object
83
     */
84
    @Inline
85
    public final Object pop() {
86
        if (stackPtr == 0) {
87
            return null;
88
        } else {
89
            stackPtr--;
90
            Object result = stack[stackPtr];
91
            if (result == null) {
92
                throw new IllegalStateException("Null object found on GCStack");
93
            }
94
            stack[stackPtr] = null;
95
            return result;
96
        }
97
    }
98
99
    /**
100
     * Is this stack empty?
101
     *
102
     * @return boolean
103
     */
104
    public boolean isEmpty() {
105
        return (stackPtr == 0);
106
    }
107
108
    /**
109
     * Has a stackoverflow occurred?
110
     *
111
     * @return boolean
112
     */
113
    @Inline
114
    public final boolean isOverflow() {
115
        return overflow;
116
    }
117
118
    /**
119
     * Reset the contents of this stack to its original state
120
     */
121
    public void reset() {
122
        stackPtr = 0;
123
        overflow = false;
124
    }
125
}