5 #include <ltt/LTTTypes.h>
7 #include <ltt/facility.h>
9 /* search for the (named) type in the table, if it does not exist
11 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
);
13 /* construct directed acyclic graph for types, and tree for fields */
14 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
17 /* generate the facility according to the events belongin to it */
18 void generateFacility(LttFacility
* f
, facility
* fac
,
19 LttChecksum checksum
);
21 /* functions to release the memory occupied by a facility */
22 void freeFacility(LttFacility
* facility
);
23 void freeEventtype(LttEventType
* evType
);
24 void freeLttType(LttType
** type
);
25 void freeLttField(LttField
* fld
);
26 void freeLttNamedType(LttType
* type
);
29 /*****************************************************************************
31 * ltt_facility_open : open facilities
33 * t : the trace containing the facilities
34 * pathname : the path name of the facility
35 ****************************************************************************/
37 void ltt_facility_open(LttTrace
* t
, char * pathname
)
41 char buffer
[BUFFER_SIZE
];
48 in
.error
= error_callback
;
51 in
.fp
= fopen(in
.name
, "r");
52 if(!in
.fp
) in
.error(&in
,"cannot open input file");
55 token
= getToken(&in
);
56 if(in
.type
== ENDFILE
) break;
58 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
61 if(strcmp("facility",token
) == 0) {
62 fac
= g_new(facility
, 1);
64 fac
->description
= NULL
;
65 sequence_init(&(fac
->events
));
66 table_init(&(fac
->named_types
));
67 sequence_init(&(fac
->unnamed_types
));
69 parseFacility(&in
, fac
);
71 //check if any namedType is not defined
72 checkNamedTypesImplemented(&fac
->named_types
);
74 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
76 f
= g_new(LttFacility
,1);
77 generateFacility(f
, fac
, checksum
);
80 g_ptr_array_add(t
->facilities
,f
);
83 free(fac
->description
);
84 freeEvents(&fac
->events
);
85 sequence_dispose(&fac
->events
);
86 freeNamedType(&fac
->named_types
);
87 table_dispose(&fac
->named_types
);
88 freeTypes(&fac
->unnamed_types
);
89 sequence_dispose(&fac
->unnamed_types
);
92 else in
.error(&in
,"facility token was expected");
98 /*****************************************************************************
100 * generateFacility : generate facility, internal function
102 * facility : LttFacilty structure
103 * fac : facility structure
104 * checksum : checksum of the facility
105 ****************************************************************************/
107 void generateFacility(LttFacility
*f
, facility
*fac
,LttChecksum checksum
)
109 char * facilityName
= fac
->name
;
110 sequence
* events
= &fac
->events
;
112 LttEventType
* evType
;
116 f
->name
= g_strdup(facilityName
);
117 f
->event_number
= events
->position
;
118 f
->checksum
= checksum
;
120 //initialize inner structures
121 f
->events
= g_new(LttEventType
*,f
->event_number
);
122 f
->named_types_number
= fac
->named_types
.keys
.position
;
123 f
->named_types
= g_new(LttType
*, fac
->named_types
.keys
.position
);
124 for(i
=0;i
<fac
->named_types
.keys
.position
;i
++) f
->named_types
[i
] = NULL
;
126 //for each event, construct field tree and type graph
127 for(i
=0;i
<events
->position
;i
++){
128 evType
= g_new(LttEventType
,1);
129 f
->events
[i
] = evType
;
131 evType
->name
= g_strdup(((event
*)(events
->array
[i
]))->name
);
132 evType
->description
=g_strdup(((event
*)(events
->array
[i
]))->description
);
134 field
= g_new(LttField
, 1);
135 evType
->root_field
= field
;
136 evType
->facility
= f
;
139 if(((event
*)(events
->array
[i
]))->type
!= NULL
){
140 field
->field_pos
= 0;
141 type
= lookup_named_type(f
,((event
*)(events
->array
[i
]))->type
);
142 field
->field_type
= type
;
143 field
->offset_root
= 0;
144 field
->fixed_root
= 1;
145 field
->offset_parent
= 0;
146 field
->fixed_parent
= 1;
147 // field->base_address = NULL;
148 field
->field_size
= 0;
149 field
->field_fixed
= -1;
150 field
->parent
= NULL
;
152 field
->current_element
= 0;
154 //construct field tree and type graph
155 constructTypeAndFields(f
,((event
*)(events
->array
[i
]))->type
,field
);
157 evType
->root_field
= NULL
;
164 /*****************************************************************************
166 * constructTypeAndFields : construct field tree and type graph,
167 * internal recursion function
169 * fac : facility struct
170 * td : type descriptor
171 * root_field : root field of the event
172 ****************************************************************************/
174 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
178 type_descriptor
* tmpTd
;
180 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
181 // fld->field_size = 0;
182 // else fld->field_size = -1;
184 if(td
->type
== LTT_ENUM
){
185 fld
->field_type
->element_number
= td
->labels
.position
;
186 fld
->field_type
->enum_strings
= g_new(char*,td
->labels
.position
);
187 for(i
=0;i
<td
->labels
.position
;i
++){
188 fld
->field_type
->enum_strings
[i
]
189 = g_strdup(((char*)(td
->labels
.array
[i
])));
191 }else if(td
->type
== LTT_ARRAY
|| td
->type
== LTT_SEQUENCE
){
192 if(td
->type
== LTT_ARRAY
)
193 fld
->field_type
->element_number
= (unsigned)td
->size
;
194 fld
->field_type
->element_type
= g_new(LttType
*,1);
195 tmpTd
= td
->nested_type
;
196 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
197 fld
->child
= g_new(LttField
*, 1);
198 fld
->child
[0] = g_new(LttField
, 1);
200 fld
->child
[0]->field_pos
= 0;
201 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
202 fld
->child
[0]->offset_root
= fld
->offset_root
;
203 fld
->child
[0]->fixed_root
= fld
->fixed_root
;
204 fld
->child
[0]->offset_parent
= 0;
205 fld
->child
[0]->fixed_parent
= 1;
206 // fld->child[0]->base_address = NULL;
207 fld
->child
[0]->field_size
= 0;
208 fld
->child
[0]->field_fixed
= -1;
209 fld
->child
[0]->parent
= fld
;
210 fld
->child
[0]->child
= NULL
;
211 fld
->child
[0]->current_element
= 0;
212 constructTypeAndFields(fac
, tmpTd
, fld
->child
[0]);
213 }else if(td
->type
== LTT_STRUCT
){
214 fld
->field_type
->element_number
= td
->fields
.position
;
216 if(fld
->field_type
->element_type
== NULL
){
217 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
223 fld
->child
= g_new(LttField
*, td
->fields
.position
);
224 for(i
=0;i
<td
->fields
.position
;i
++){
225 tmpTd
= ((field
*)(td
->fields
.array
[i
]))->type
;
228 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
229 fld
->child
[i
] = g_new(LttField
,1);
231 fld
->child
[i
]->field_pos
= i
;
232 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
235 fld
->child
[i
]->field_type
->element_name
236 = g_strdup(((field
*)(td
->fields
.array
[i
]))->name
);
239 fld
->child
[i
]->offset_root
= -1;
240 fld
->child
[i
]->fixed_root
= -1;
241 fld
->child
[i
]->offset_parent
= -1;
242 fld
->child
[i
]->fixed_parent
= -1;
243 // fld->child[i]->base_address = NULL;
244 fld
->child
[i
]->field_size
= 0;
245 fld
->child
[i
]->field_fixed
= -1;
246 fld
->child
[i
]->parent
= fld
;
247 fld
->child
[i
]->child
= NULL
;
248 fld
->child
[i
]->current_element
= 0;
249 constructTypeAndFields(fac
, tmpTd
, fld
->child
[i
]);
255 /*****************************************************************************
257 * lookup_named_type: search named type in the table
260 * fac : facility struct
261 * td : type descriptor
263 * : either find the named type, or create a new LttType
264 ****************************************************************************/
266 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
)
268 LttType
* lttType
= NULL
;
272 for(i
=0;i
<fac
->named_types_number
; i
++){
273 if(fac
->named_types
[i
] == NULL
) break;
274 name
= fac
->named_types
[i
]->type_name
;
275 if(strcmp(name
, td
->type_name
)==0){
276 lttType
= fac
->named_types
[i
];
277 // if(lttType->element_name) g_free(lttType->element_name);
278 // lttType->element_name = NULL;
285 lttType
= g_new(LttType
,1);
286 lttType
->type_class
= td
->type
;
287 if(td
->fmt
) lttType
->fmt
= g_strdup(td
->fmt
);
288 else lttType
->fmt
= NULL
;
289 lttType
->size
= td
->size
;
290 lttType
->enum_strings
= NULL
;
291 lttType
->element_type
= NULL
;
292 lttType
->element_number
= 0;
293 lttType
->element_name
= NULL
;
295 lttType
->type_name
= g_strdup(td
->type_name
);
296 fac
->named_types
[i
] = lttType
;
299 lttType
->type_name
= NULL
;
307 /*****************************************************************************
309 * ltt_facility_close : close a facility, decrease its usage count,
310 * if usage count = 0, release the memory
312 * f : facility that will be closed
314 * int : usage count ?? status
315 ****************************************************************************/
317 int ltt_facility_close(LttFacility
*f
)
319 //release the memory it occupied
325 /*****************************************************************************
326 * Functions to release the memory occupied by the facility
327 ****************************************************************************/
329 void freeFacility(LttFacility
* fac
)
332 g_free(fac
->name
); //free facility name
335 for(i
=0;i
<fac
->event_number
;i
++){
336 freeEventtype(fac
->events
[i
]);
340 //free all named types
341 for(i
=0;i
<fac
->named_types_number
;i
++){
342 freeLttNamedType(fac
->named_types
[i
]);
343 fac
->named_types
[i
] = NULL
;
345 g_free(fac
->named_types
);
347 //free the facility itself
351 void freeEventtype(LttEventType
* evType
)
354 g_free(evType
->name
);
355 if(evType
->description
)
356 g_free(evType
->description
);
357 if(evType
->root_field
){
358 root_type
= evType
->root_field
->field_type
;
359 freeLttField(evType
->root_field
);
360 freeLttType(&root_type
);
366 void freeLttNamedType(LttType
* type
)
369 g_free(type
->type_name
);
370 type
->type_name
= NULL
;
374 void freeLttType(LttType
** type
)
377 if(*type
== NULL
) return;
378 if((*type
)->type_name
){
379 return; //this is a named type
381 if((*type
)->element_name
)
382 g_free((*type
)->element_name
);
384 g_free((*type
)->fmt
);
385 if((*type
)->enum_strings
){
386 for(i
=0;i
<(*type
)->element_number
;i
++)
387 g_free((*type
)->enum_strings
[i
]);
388 g_free((*type
)->enum_strings
);
391 if((*type
)->element_type
){
392 for(i
=0;i
<(*type
)->element_number
;i
++)
393 freeLttType(&((*type
)->element_type
[i
]));
394 g_free((*type
)->element_type
);
400 void freeLttField(LttField
* fld
)
406 if(fld
->field_type
->type_class
== LTT_ARRAY
||
407 fld
->field_type
->type_class
== LTT_SEQUENCE
){
409 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
410 size
= fld
->field_type
->element_number
;
415 for(i
=0; i
<size
; i
++){
416 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
423 /*****************************************************************************
425 * ltt_facility_name : obtain the facility's name
427 * f : the facility that will be closed
429 * char * : the facility's name
430 ****************************************************************************/
432 char *ltt_facility_name(LttFacility
*f
)
437 /*****************************************************************************
439 * ltt_facility_checksum : obtain the facility's checksum
441 * f : the facility that will be closed
443 * LttChecksum : the checksum of the facility
444 ****************************************************************************/
446 LttChecksum
ltt_facility_checksum(LttFacility
*f
)
451 /*****************************************************************************
453 * ltt_facility_base_id : obtain the facility base id
457 * : the base id of the facility
458 ****************************************************************************/
460 unsigned ltt_facility_base_id(LttFacility
*f
)
465 /*****************************************************************************
467 * ltt_facility_eventtype_number: obtain the number of the event types
469 * f : the facility that will be closed
471 * unsigned : the number of the event types
472 ****************************************************************************/
474 unsigned ltt_facility_eventtype_number(LttFacility
*f
)
476 return (unsigned)(f
->event_number
);
479 /*****************************************************************************
481 * ltt_facility_eventtype_get: obtain the event type according to event id
482 * from 0 to event_number - 1
484 * f : the facility that will be closed
486 * LttEventType * : the event type required
487 ****************************************************************************/
489 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, unsigned i
)
494 /*****************************************************************************
496 * ltt_facility_eventtype_get_by_name
497 * : obtain the event type according to event name
498 * event name is unique in the facility
500 * f : the facility that will be closed
501 * name : the name of the event
503 * LttEventType * : the event type required
504 ****************************************************************************/
506 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, char *name
)
510 for(i
=0;i
<f
->event_number
;i
++){
512 if(strcmp(ev
->name
, name
) == 0)break;
515 if(i
==f
->event_number
) return NULL
;
This page took 0.054778 seconds and 4 git commands to generate.