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.annotation.Inline;
24
import org.jnode.annotation.MagicPermission;
25
import org.jnode.vm.VmMagic;
26
import org.jnode.vm.classmgr.ObjectFlags;
27
import org.jnode.vm.classmgr.VmClassType;
28
import org.jnode.vm.classmgr.VmMethod;
29
import org.jnode.vm.memmgr.HeapHelper;
30
31
/**
32
 * @author Loic Rouchon (loic.rouchon@insa-lyon.fr)
33
 */
34
@MagicPermission
35
final class GenGCYellowObjectManager {
36
37
    private final HeapHelper heapHelper;
38
39
    private VmDefaultHeap currentHeap;
40
41
    private long nbYellowObjects = 0L;
42
43
    private GenGCStatistics stats;
44
45
    public GenGCYellowObjectManager(HeapHelper heapHelper) {
46
        this.heapHelper = heapHelper;
47
    }
48
49
    @Inline
50
    public final void markObjectAsYellow(Object object) {
51
        heapHelper.atomicChangeObjectColor(object, ObjectFlags.GC_WHITE, ObjectFlags.GC_YELLOW);
52
        nbYellowObjects++;
53
        syncStats();
54
    }
55
56
    @Inline
57
    public final void deleteObject(Object object) {
58
        nbYellowObjects--;
59
        finalizeObject(object);
60
        currentHeap.free(object);
61
        syncStats();
62
    }
63
64
    @Inline
65
    public final boolean areObjectsYellow() {
66
        return nbYellowObjects > 0L;
67
    }
68
69
    @Inline
70
    private void syncStats() {
71
        stats.nbYellowObjects = nbYellowObjects;
72
    }
73
74
    /**
75
     * @param currentHeap
76
     *            The currentHeap to set.
77
     */
78
    public final void setCurrentHeap(VmDefaultHeap currentHeap) {
79
        this.currentHeap = currentHeap;
80
    }
81
82
    /**
83
     * Checks if the object needs to be finalized and if yes, do it
84
     * 
85
     * @param objectToFinalize
86
     *            the object to finalize
87
     */
88
    @Inline
89
    private void finalizeObject(Object objectToFinalize) {
90
        if (!VmMagic.isFinalized(objectToFinalize)) {
91
            final VmClassType<?> vmClass = VmMagic.getObjectType(objectToFinalize);
92
            if (vmClass.hasFinalizer()) {
93
                doFinalize(objectToFinalize, vmClass);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Finalizes the given object of type <code>vmClass</code>
100
     * 
101
     * @param objectToFinalize
102
     *            the object to finalize
103
     * @param vmClass
104
     *            the vmClass corresponding to the object class
105
     */
106
    @Inline
107
    private void doFinalize(Object objectToFinalize, VmClassType<?> vmClass) {
108
        final VmMethod finalizeMethod = vmClass.getFinalizeMethod();
109
        try {
110
            heapHelper.invokeFinalizer(finalizeMethod, objectToFinalize);
111
        } catch (Throwable ex) {
112
            // Ignore error in finalize
113
        }
114
        heapHelper.setFinalized(objectToFinalize);
115
    }
116
117
    public void setStats(GenGCStatistics stats) {
118
        this.stats = stats;
119
    }
120
}