Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 855ae0fc2a37e345401b3ec57d122d7284bee3fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
 * Copyright (c) 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.model;

import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.ISymbols;
import org.eclipse.tcf.util.TCFDataCache;

public class TCFNodeSymbol extends TCFNode {

    private final TCFData<ISymbols.Symbol> context;
    private final TCFData<String[]> children;

    private int update_policy;
    private ISymbolOwner owner;

    private TCFNodeSymbol prev;
    private TCFNodeSymbol next;

    private static final int MAX_SYMBOL_COUNT = 64;
    private static TCFNodeSymbol sym_list;
    private static int sym_count;
    private static boolean gc_posted;

    protected TCFNodeSymbol(final TCFNode parent, final String id) {
        super(parent, id);
        context = new TCFData<ISymbols.Symbol>(channel) {
            @Override
            protected boolean startDataRetrieval() {
                ISymbols syms = launch.getService(ISymbols.class);
                if (id == null || syms == null) {
                    set(null, null, null);
                    return true;
                }
                command = syms.getContext(id, new ISymbols.DoneGetContext() {
                    public void doneGetContext(IToken token, Exception error, ISymbols.Symbol sym) {
                        set(token, error, sym);
                        if (error != null || sym == null) setUpdatePolicy(null, 0);
                        else setUpdatePolicy(sym.getOwnerID(), sym.getUpdatePolicy());
                    }
                });
                return false;
            }
        };
        children = new TCFData<String[]>(channel) {
            @Override
            protected boolean startDataRetrieval() {
                ISymbols syms = launch.getService(ISymbols.class);
                if (id == null || syms == null) {
                    set(null, null, null);
                    return true;
                }
                command = syms.getChildren(id, new ISymbols.DoneGetChildren() {
                    public void doneGetChildren(IToken token, Exception error, String[] ids) {
                        set(token, error, ids);
                    }
                });
                return false;
            }
        };
        setUpdatePolicy(null, 0);
        if (sym_list == null) {
            prev = next = this;
        }
        else {
            prev = sym_list;
            next = sym_list.next;
            prev.next = next.prev = this;
        }
        sym_list = this;
        if (!gc_posted) {
            // Garbage collection: dispose unused symbols
            gc_posted = true;
            Protocol.invokeLater(5000, new Runnable() {
                public void run() {
                    gc_posted = false;
                    int cnt = sym_count / 16;
                    while (sym_count > MAX_SYMBOL_COUNT) {
                        TCFNodeSymbol s = sym_list.next;
                        if (s.context.isPending()) break;
                        if (s.children.isPending()) break;
                        s.dispose();
                        if (cnt == 0) break;
                        cnt--;
                    }
                    if (sym_count > 0) {
                        gc_posted = true;
                        Protocol.invokeLater(5000, this);
                    }
                }
            });
        }
        sym_count++;
    }

    @Override
    public void dispose() {
        assert !isDisposed();
        if (owner != null) {
            owner.removeSymbol(this);
            owner = null;
        }
        if (sym_list == this) sym_list = prev;
        if (sym_list == this) {
            sym_list = null;
        }
        else {
            prev.next = next;
            next.prev = prev;
        }
        prev = next = null;
        sym_count--;
        assert (sym_count == 0) == (sym_list == null);
        super.dispose();
    }

    public TCFDataCache<ISymbols.Symbol> getContext() {
        if (sym_list != this) {
            prev.next = next;
            next.prev = prev;
            prev = sym_list;
            next = sym_list.next;
            prev.next = next.prev = this;
            sym_list = this;
        }
        return context;
    }

    public TCFDataCache<String[]> getChildren() {
        if (sym_list != this) {
            prev.next = next;
            next.prev = prev;
            prev = sym_list;
            next = sym_list.next;
            prev.next = next.prev = this;
            sym_list = this;
        }
        return children;
    }

    private void setUpdatePolicy(String id, int policy) {
        update_policy = policy;
        if (!isDisposed()) {
            TCFNode n = model.getNode(id);
            if (!(n instanceof ISymbolOwner)) n = parent;
            if (n != owner) {
                if (owner != null) owner.removeSymbol(this);
                owner = (ISymbolOwner)n;
                owner.addSymbol(this);
            }
        }
    }

    void onMemoryMapChanged() {
        context.reset();
        children.reset();
    }

    void onExeStateChange() {
        if (update_policy == ISymbols.UPDATE_ON_MEMORY_MAP_CHANGES) return;
        context.reset();
        children.reset();
    }
}

Back to the top