1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
;
15 import java
.util
.List
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.CompoundDeclaration
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
26 import com
.google
.common
.collect
.ArrayListMultimap
;
27 import com
.google
.common
.collect
.ImmutableList
;
28 import com
.google
.common
.collect
.ImmutableList
.Builder
;
31 * A CTF array declaration
33 * Arrays are fixed-length. Their length is declared in the type declaration
34 * within the meta-data. They contain an array of "inner type" elements, which
35 * can refer to any type not containing the type of the array being declared (no
36 * circular dependency). The length is the number of elements in an array.
38 * @author Matthew Khouzam
41 public final class ArrayDeclaration
extends CompoundDeclaration
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private final int fLength
;
48 private final IDeclaration fElemType
;
52 * Cache where we can pre-generate the children names
53 * Key: parent name
54 * Value: children names
55 * ex: field → {field[0], field[1], … field[n]}
58 * TODO: investigate performance
60 private final ArrayListMultimap
<String
, String
> fChildrenNames
= ArrayListMultimap
.create();
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
70 * how many elements in the array
72 * what type of element is in the array
74 public ArrayDeclaration(int length
, IDeclaration elemType
) {
79 // ------------------------------------------------------------------------
80 // Getters/Setters/Predicates
81 // ------------------------------------------------------------------------
84 public IDeclaration
getElementType() {
89 * Get the length of the array
91 * @return the length of the array
93 public int getLength() {
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 public AbstractArrayDefinition
createDefinition(IDefinitionScope definitionScope
,
103 @NonNull String fieldName
, BitBuffer input
) throws CTFReaderException
{
106 byte[] data
= new byte[fLength
];
108 return new ByteArrayDefinition(this, definitionScope
, fieldName
, data
);
110 List
<Definition
> definitions
= read(input
, definitionScope
, fieldName
);
111 return new ArrayDefinition(this, definitionScope
, fieldName
, definitions
);
115 public String
toString() {
116 /* Only used for debugging */
117 return "[declaration] array[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
121 private List
<Definition
> read(@NonNull BitBuffer input
, IDefinitionScope definitionScope
, String fieldName
) throws CTFReaderException
{
122 Builder
<Definition
> definitions
= new ImmutableList
.Builder
<>();
123 if (!fChildrenNames
.containsKey(fieldName
)) {
124 for (int i
= 0; i
< fLength
; i
++) {
125 fChildrenNames
.put(fieldName
, fieldName
+ '[' + i
+ ']');
128 List
<String
> elemNames
= fChildrenNames
.get(fieldName
);
129 for (int i
= 0; i
< fLength
; i
++) {
130 String name
= elemNames
.get(i
);
132 throw new IllegalStateException();
134 definitions
.add(fElemType
.createDefinition(definitionScope
, name
, input
));
136 @SuppressWarnings("null")
137 @NonNull ImmutableList
<Definition
> ret
= definitions
.build();
142 public int getMaximumSize() {
143 long val
= (long) fLength
* fElemType
.getMaximumSize();
144 return (int) Math
.min(Integer
.MAX_VALUE
, val
);