+ LttEventType *et, *first_et;
+
+ GArray *facilities;
+
+ guint i, fac_id, ev_id;
+
+ LttvTraceHookByFacility *thf, *first_thf;
+
+ facilities = ltt_trace_facility_get_by_name(t, facility);
+
+ if(unlikely(facilities == NULL)) goto facility_error;
+
+ th->fac_index = g_array_sized_new(FALSE, TRUE,
+ sizeof(LttvTraceHookByFacility),
+ NUM_FACILITIES);
+ th->fac_index = g_array_set_size(th->fac_index, NUM_FACILITIES);
+
+ th->fac_list = g_array_sized_new(FALSE, TRUE,
+ sizeof(LttvTraceHookByFacility*),
+ facilities->len);
+ th->fac_list = g_array_set_size(th->fac_list, facilities->len);
+
+ fac_id = g_array_index(facilities, guint, 0);
+ f = ltt_trace_get_facility_by_num(t, fac_id);
+
+ et = ltt_facility_eventtype_get_by_name(f, event);
+ if(unlikely(et == NULL)) goto event_error;
+
+ thf = &g_array_index(th->fac_index, LttvTraceHookByFacility, fac_id);
+ g_array_index(th->fac_list, LttvTraceHookByFacility*, 0) = thf;
+
+ ev_id = ltt_eventtype_id(et);
+
+ thf->h = h;
+ thf->id = GET_HOOK_ID(fac_id, ev_id);
+ thf->f1 = find_field(et, field1);
+ thf->f2 = find_field(et, field2);
+ thf->f3 = find_field(et, field3);
+
+ first_thf = thf;
+
+ /* Check for type compatibility too */
+ for(i=1;i<facilities->len;i++) {
+ fac_id = g_array_index(facilities, guint, i);
+ f = ltt_trace_get_facility_by_num(t, fac_id);
+
+ et = ltt_facility_eventtype_get_by_name(f, ltt_eventtype_name(et));
+ if(unlikely(et == NULL)) goto event_error;
+
+ thf = &g_array_index(th->fac_index, LttvTraceHookByFacility, fac_id);
+ g_array_index(th->fac_list, LttvTraceHookByFacility*, i) = thf;
+ ev_id = ltt_eventtype_id(et);
+ thf->h = h;
+ thf->id = GET_HOOK_ID(fac_id, ev_id);
+ thf->f1 = find_field(et, field1);
+ if(check_fields_compatibility(first_et, et,
+ first_thf->f1, thf->f1))
+ goto type_error;
+
+ thf->f2 = find_field(et, field2);
+ if(check_fields_compatibility(first_et, et,
+ first_thf->f2, thf->f2))
+ goto type_error;
+
+ thf->f3 = find_field(et, field3);
+ if(check_fields_compatibility(first_et, et,
+ first_thf->f3, thf->f3))
+ goto type_error;
+ }
+
+ return 0;
+
+type_error:
+ goto free;
+event_error:
+ g_error("Event type %s does not exist",
+ g_quark_to_string(ltt_eventtype_name(et)));
+ goto free;
+facility_error:
+ g_error("No %s facility", g_quark_to_string(facility));
+ goto free;
+free:
+ g_array_free(th->fac_index, TRUE);
+ g_array_free(th->fac_list, TRUE);
+ th->fac_index = NULL;
+ th->fac_list = NULL;
+ return -1;
+}
+
+void lttv_trace_hook_destroy(LttvTraceHook *th)
+{
+ g_array_free(th->fac_index, TRUE);
+ g_array_free(th->fac_list, TRUE);
+}
+
+
+LttvTracesetContextPosition *lttv_traceset_context_position_new()
+{
+ LttvTracesetContextPosition *pos = g_new(LttvTracesetContextPosition,1);
+ pos->ep = g_array_sized_new(FALSE, TRUE, sizeof(LttEventPosition*),
+ 10);
+ pos->tfc = g_array_sized_new(FALSE, TRUE, sizeof(LttvTracefileContext*),
+ 10);
+ pos->timestamp = ltt_time_infinite;
+ return pos;
+}
+
+/* Save all positions, the ones not in the pqueue will have NULL
+ * ep. */
+void lttv_traceset_context_position_save(const LttvTracesetContext *self,
+ LttvTracesetContextPosition *pos)
+{
+ guint i;
+ guint num_traces = lttv_traceset_number(self->ts);
+
+ for(i=0; i<num_traces;i++) {
+ GArray * tracefiles = self->traces[i]->tracefiles;
+ guint j;
+ guint num_tracefiles = tracefiles->len;
+
+ for(j=0;j<num_tracefiles;j++) {
+ LttvTracefileContext **tfc = &g_array_index(tracefiles,
+ LttvTracefileContext*, j);
+
+ LttEvent *event = ltt_tracefile_get_event((*tfc)->tf);
+ LttEventPosition *ep;
+
+ if(ltt_time_compare((*tfc)->timestamp, ltt_time_infinite) != 0) {
+ ep = ltt_event_position_new();
+ ltt_event_position(event, ep);
+ if(ltt_time_compare((*tfc)->timestamp, pos->timestamp) < 0)
+ pos->timestamp = (*tfc)->timestamp;
+ } else {
+ ep = NULL;
+ }
+ g_array_append_val(pos->tfc, *tfc);
+ g_array_append_val(pos->ep, ep);
+ }
+
+ }
+}
+
+void lttv_traceset_context_position_destroy(LttvTracesetContextPosition *pos)
+{
+ int i;
+ LttEventPosition **ep;
+
+ for(i=0;i<pos->ep->len;i++) {
+ ep = &g_array_index(pos->ep, LttEventPosition*, i);
+ if(*ep != NULL)
+ g_free(*ep);
+ }
+ g_array_free(pos->ep, TRUE);
+ g_array_free(pos->tfc, TRUE);
+ g_free(pos);
+}
+
+void lttv_traceset_context_position_copy(LttvTracesetContextPosition *dest,
+ const LttvTracesetContextPosition *src)
+{
+ int i;
+ LttEventPosition **src_ep, **dest_ep;
+
+ g_array_set_size(dest->ep, src->ep->len);
+ g_array_set_size(dest->tfc, src->tfc->len);
+
+ for(i=0;i<src->ep->len;i++) {
+ src_ep = &g_array_index(src->ep, LttEventPosition*, i);
+ dest_ep = &g_array_index(dest->ep, LttEventPosition*, i);
+ if(*src_ep != NULL) {
+ *dest_ep = ltt_event_position_new();
+ ltt_event_position_copy(
+ *dest_ep,
+ *src_ep);
+ } else
+ *dest_ep = NULL;
+ }
+ for(i=0;i<src->tfc->len;i++) {
+ g_array_index(dest->tfc, LttvTracefileContext*, i) =
+ g_array_index(src->tfc, LttvTracefileContext*, i);
+ }
+ dest->timestamp = src->timestamp;
+}
+
+gint lttv_traceset_context_ctx_pos_compare(const LttvTracesetContext *self,
+ const LttvTracesetContextPosition *pos)
+{
+ int i;
+ int ret;
+
+ for(i=0;i<pos->ep->len;i++) {
+ LttEventPosition *ep = g_array_index(pos->ep, LttEventPosition*, i);
+ LttvTracefileContext *tfc =
+ g_array_index(pos->tfc, LttvTracefileContext*, i);
+
+ if(ep == NULL) {
+ if(ltt_time_compare(tfc->timestamp, ltt_time_infinite) != 0) {
+ ret = -1;
+ }
+ } else {
+ if(ltt_time_compare(tfc->timestamp, ltt_time_infinite) == 0) {
+ ret = 1;
+ } else {
+ LttEvent *event = ltt_tracefile_get_event(tfc->tf);
+
+ ret = ltt_event_position_compare((LttEventPosition*)event,
+ ep);
+ }
+ }
+ if(ret != 0) return ret;
+
+ }
+ return 0;
+}