Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
a4fa4e36 | 15 | import java.util.LinkedHashMap; |
866e5b51 | 16 | import java.util.List; |
0594c61c | 17 | import java.util.Map; |
866e5b51 | 18 | |
a4fa4e36 MK |
19 | import org.eclipse.jdt.annotation.NonNull; |
20 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; | |
21 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
22 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
23 | ||
24 | import com.google.common.collect.ImmutableList; | |
25 | ||
866e5b51 | 26 | /** |
d37aaa7f | 27 | * A CTF structure declaration. |
77fdc5df | 28 | * |
d37aaa7f FC |
29 | * A structure is similar to a C structure, it is a compound data type that |
30 | * contains other datatypes in fields. they are stored in an hashmap and indexed | |
31 | * by names which are strings. | |
32 | * | |
33 | * @version 1.0 | |
34 | * @author Matthew Khouzam | |
35 | * @author Simon Marchi | |
866e5b51 | 36 | */ |
a4fa4e36 | 37 | public class StructDeclaration extends Declaration { |
866e5b51 FC |
38 | |
39 | // ------------------------------------------------------------------------ | |
40 | // Attributes | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
a4fa4e36 MK |
43 | /** linked list of field names. So fieldName->fieldValue */ |
44 | private final Map<String, IDeclaration> fFieldMap = new LinkedHashMap<>(); | |
45 | ||
46 | /** List of strings for acceleration */ | |
47 | @NonNull | |
48 | private ImmutableList<String> fFieldNames; | |
49 | /** array declaration for acceleration */ | |
50 | private List<IDeclaration> fFieldDeclarations; | |
51 | ||
52 | /** maximum bit alignment */ | |
53 | private long fMaxAlign; | |
866e5b51 FC |
54 | |
55 | // ------------------------------------------------------------------------ | |
56 | // Constructors | |
57 | // ------------------------------------------------------------------------ | |
58 | ||
9ac2eb62 MK |
59 | /** |
60 | * The struct declaration, add fields later | |
61 | * | |
62 | * @param align | |
63 | * the minimum alignment of the struct. (if a struct is 8bit | |
64 | * aligned and has a 32 bit aligned field, the struct becomes 32 | |
65 | * bit aligned. | |
66 | */ | |
a4fa4e36 MK |
67 | @SuppressWarnings("null") |
68 | // ImmutableList.of() | |
2b7f6f09 | 69 | public StructDeclaration(long align) { |
a4fa4e36 MK |
70 | fMaxAlign = Math.max(align, 1); |
71 | fFieldNames = ImmutableList.of(); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Struct declaration constructor | |
76 | * | |
77 | * @param names | |
78 | * the names of all the fields | |
79 | * @param declarations | |
80 | * all the fields | |
81 | * @since 3.0 | |
82 | */ | |
83 | @SuppressWarnings("null") | |
84 | // ImmutableList.of() | |
85 | public StructDeclaration(String[] names, Declaration[] declarations) { | |
86 | fMaxAlign = 1; | |
87 | fFieldNames = ImmutableList.of(); | |
88 | for (int i = 0; i < names.length; i++) { | |
89 | addField(names[i], declarations[i]); | |
90 | } | |
866e5b51 FC |
91 | } |
92 | ||
93 | // ------------------------------------------------------------------------ | |
94 | // Getters/Setters/Predicates | |
95 | // ------------------------------------------------------------------------ | |
96 | ||
9ac2eb62 MK |
97 | /** |
98 | * Get current alignment | |
a4fa4e36 | 99 | * |
9ac2eb62 MK |
100 | * @return the alignment of the struct and all its fields |
101 | */ | |
2b7f6f09 | 102 | public long getMaxAlign() { |
a4fa4e36 | 103 | return fMaxAlign; |
866e5b51 FC |
104 | } |
105 | ||
9ac2eb62 MK |
106 | /** |
107 | * Query if the struct has a given field | |
a4fa4e36 MK |
108 | * |
109 | * @param name | |
110 | * the name of the field, scopeless please | |
9ac2eb62 MK |
111 | * @return does the field exist? |
112 | */ | |
866e5b51 | 113 | public boolean hasField(String name) { |
a4fa4e36 | 114 | return fFieldMap.containsKey(name); |
866e5b51 FC |
115 | } |
116 | ||
9ac2eb62 MK |
117 | /** |
118 | * get the fields of the struct in a map. Faster access time than a list. | |
a4fa4e36 | 119 | * |
9ac2eb62 | 120 | * @return a HashMap of the fields (key is the name) |
0594c61c | 121 | * @since 2.0 |
9ac2eb62 | 122 | */ |
0594c61c | 123 | public Map<String, IDeclaration> getFields() { |
a4fa4e36 | 124 | return fFieldMap; |
866e5b51 FC |
125 | } |
126 | ||
9ac2eb62 | 127 | /** |
a4fa4e36 MK |
128 | * Gets the field list. Very important since the map of fields does not |
129 | * retain the order of the fields. | |
130 | * | |
9ac2eb62 | 131 | * @return the field list. |
a4fa4e36 | 132 | * @since 3.0 |
9ac2eb62 | 133 | */ |
a4fa4e36 MK |
134 | public Iterable<String> getFieldsList() { |
135 | return fFieldMap.keySet(); | |
866e5b51 FC |
136 | } |
137 | ||
fd74e6c1 MK |
138 | @Override |
139 | public long getAlignment() { | |
a4fa4e36 MK |
140 | return this.fMaxAlign; |
141 | } | |
142 | ||
143 | /** | |
144 | * @since 3.0 | |
145 | */ | |
146 | @Override | |
147 | public int getMaximumSize() { | |
148 | int maxSize = 0; | |
149 | if (fFieldDeclarations != null) { | |
150 | for (IDeclaration field : fFieldDeclarations) { | |
151 | maxSize += field.getMaximumSize(); | |
152 | } | |
153 | } | |
154 | return Math.min(maxSize, Integer.MAX_VALUE); | |
fd74e6c1 | 155 | } |
9ac2eb62 | 156 | |
866e5b51 FC |
157 | // ------------------------------------------------------------------------ |
158 | // Operations | |
159 | // ------------------------------------------------------------------------ | |
160 | ||
a4fa4e36 MK |
161 | /** |
162 | * @since 3.0 | |
163 | */ | |
164 | @SuppressWarnings("null") | |
165 | // immutablelist | |
866e5b51 FC |
166 | @Override |
167 | public StructDefinition createDefinition(IDefinitionScope definitionScope, | |
a4fa4e36 MK |
168 | String fieldName, BitBuffer input) throws CTFReaderException { |
169 | alignRead(input); | |
170 | final Definition[] myFields = new Definition[fFieldNames.size()]; | |
171 | StructDefinition structDefinition = new StructDefinition(this, definitionScope, fieldName, fFieldNames, myFields); | |
172 | for (int i = 0; i < fFieldNames.size(); i++) { | |
173 | myFields[i] = fFieldDeclarations.get(i).createDefinition(structDefinition, fFieldNames.get(i), input); | |
174 | } | |
175 | return structDefinition; | |
866e5b51 FC |
176 | } |
177 | ||
9ac2eb62 MK |
178 | /** |
179 | * Add a field to the struct | |
a4fa4e36 MK |
180 | * |
181 | * @param name | |
182 | * the name of the field, scopeless | |
183 | * @param declaration | |
184 | * the declaration of the field | |
9ac2eb62 | 185 | */ |
a4fa4e36 MK |
186 | @SuppressWarnings("null") |
187 | // Immutable list copyof cannot return null | |
866e5b51 | 188 | public void addField(String name, IDeclaration declaration) { |
a4fa4e36 MK |
189 | fFieldMap.put(name, declaration); |
190 | fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment()); | |
191 | fFieldNames = ImmutableList.copyOf(fFieldMap.keySet()); | |
192 | fFieldDeclarations = ImmutableList.<IDeclaration>copyOf(fFieldMap.values()); | |
866e5b51 FC |
193 | } |
194 | ||
195 | @Override | |
196 | public String toString() { | |
197 | /* Only used for debugging */ | |
198 | return "[declaration] struct[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
199 | } | |
200 | ||
4dd0eaed MK |
201 | @Override |
202 | public int hashCode() { | |
203 | final int prime = 31; | |
204 | int result = 1; | |
a4fa4e36 MK |
205 | result = (prime * result) + fFieldMap.entrySet().hashCode(); |
206 | result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32)); | |
4dd0eaed MK |
207 | return result; |
208 | } | |
209 | ||
4dd0eaed MK |
210 | @Override |
211 | public boolean equals(Object obj) { | |
212 | if (this == obj) { | |
213 | return true; | |
214 | } | |
215 | if (obj == null) { | |
216 | return false; | |
217 | } | |
218 | if (!(obj instanceof StructDeclaration)) { | |
219 | return false; | |
220 | } | |
221 | StructDeclaration other = (StructDeclaration) obj; | |
a4fa4e36 | 222 | if (!fFieldMap.entrySet().equals(other.fFieldMap.entrySet())) { |
4dd0eaed MK |
223 | return false; |
224 | } | |
a4fa4e36 | 225 | if (fMaxAlign != other.fMaxAlign) { |
4dd0eaed MK |
226 | return false; |
227 | } | |
228 | return true; | |
229 | } | |
230 | ||
866e5b51 | 231 | } |