Standardize *get_*_count() functions
[babeltrace.git] / lib / ctf-ir / trace.c
index c16686a086b4ff9728c51b50f5561d6e5064fb0b..49c8308a5b32ef864ba36d8aa643ad20e9f2b611 100644 (file)
 #include <babeltrace/ctf-ir/validation-internal.h>
 #include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
-#include <babeltrace/graph/notification-schema.h>
-#include <babeltrace/compiler.h>
+#include <babeltrace/compiler-internal.h>
 #include <babeltrace/values.h>
 #include <babeltrace/ref.h>
-#include <babeltrace/endian.h>
+#include <babeltrace/endian-internal.h>
 #include <inttypes.h>
+#include <stdint.h>
 
 #define DEFAULT_IDENTIFIER_SIZE 128
 #define DEFAULT_METADATA_STRING_SIZE 4096
@@ -89,7 +89,7 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
                goto error;
        }
 
-       bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
+       trace->native_byte_order = BT_CTF_BYTE_ORDER_NATIVE;
        bt_object_init(trace, bt_ctf_trace_destroy);
        trace->clocks = g_ptr_array_new_with_free_func(
                (GDestroyNotify) bt_put);
@@ -323,9 +323,9 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
+int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
 {
-       int ret = 0;
+       int64_t ret = 0;
 
        if (!trace) {
                ret = -1;
@@ -389,16 +389,15 @@ int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
                struct bt_ctf_clock_class *clock_class)
 {
        int ret = 0;
-       struct search_query query = { .value = clock_class, .found = 0 };
 
-       if (!trace || !bt_ctf_clock_class_is_valid(clock_class)) {
+       if (!trace || trace->is_static ||
+                       !bt_ctf_clock_class_is_valid(clock_class)) {
                ret = -1;
                goto end;
        }
 
        /* Check for duplicate clock classes */
-       g_ptr_array_foreach(trace->clocks, value_exists, &query);
-       if (query.found) {
+       if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
                ret = -1;
                goto end;
        }
@@ -413,9 +412,9 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
+int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
 {
-       int ret = -1;
+       int64_t ret = -1;
 
        if (!trace) {
                goto end;
@@ -444,7 +443,8 @@ end:
 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                struct bt_ctf_stream_class *stream_class)
 {
-       int ret, i;
+       int ret;
+       int64_t i;
        int64_t stream_id;
        struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
        struct bt_ctf_validation_output *ec_validation_outputs = NULL;
@@ -457,10 +457,19 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        struct bt_ctf_field_type *packet_context_type = NULL;
        struct bt_ctf_field_type *event_header_type = NULL;
        struct bt_ctf_field_type *stream_event_ctx_type = NULL;
-       int event_class_count;
+       int64_t event_class_count;
        struct bt_ctf_trace *current_parent_trace = NULL;
 
-       if (!trace || !stream_class) {
+       if (!trace || !stream_class || trace->is_static) {
+               ret = -1;
+               goto end;
+       }
+
+       /*
+        * At the end of this function we freeze the trace, so its
+        * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
+        */
+       if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
                ret = -1;
                goto end;
        }
@@ -709,9 +718,39 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
+int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace)
 {
-       int ret;
+       int64_t ret;
+
+       if (!trace) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = trace->streams->len;
+
+end:
+       return ret;
+}
+
+struct bt_ctf_stream *bt_ctf_trace_get_stream(struct bt_ctf_trace *trace,
+               int index)
+{
+       struct bt_ctf_stream *stream = NULL;
+
+       if (!trace || index >= trace->streams->len) {
+               goto end;
+       }
+
+       stream = bt_get(g_ptr_array_index(trace->streams, index));
+
+end:
+       return stream;
+}
+
+int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
+{
+       int64_t ret;
 
        if (!trace) {
                ret = -1;
@@ -796,6 +835,19 @@ end:
        return clock_class;
 }
 
+BT_HIDDEN
+bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace,
+               struct bt_ctf_clock_class *clock_class)
+{
+       struct search_query query = { .value = clock_class, .found = 0 };
+
+       assert(trace);
+       assert(clock_class);
+
+       g_ptr_array_foreach(trace->clocks, value_exists, &query);
+       return query.found;
+}
+
 BT_HIDDEN
 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
 {
@@ -860,8 +912,8 @@ static
 void append_env_metadata(struct bt_ctf_trace *trace,
                struct metadata_context *context)
 {
-       int i;
-       int env_size;
+       int64_t i;
+       int64_t env_size;
 
        env_size = bt_ctf_attributes_get_count(trace->environment);
 
@@ -995,7 +1047,7 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
+int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
                enum bt_ctf_byte_order byte_order)
 {
        int ret = 0;
@@ -1046,7 +1098,7 @@ int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
        /* packet_header_type must be a structure. */
        if (packet_header_type &&
                        bt_ctf_field_type_get_type_id(packet_header_type) !=
-                               BT_CTF_TYPE_ID_STRUCT) {
+                               BT_CTF_FIELD_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -1058,7 +1110,7 @@ end:
 }
 
 static
-int get_stream_class_count(void *element)
+int64_t get_stream_class_count(void *element)
 {
        return bt_ctf_trace_get_stream_class_count(
                        (struct bt_ctf_trace *) element);
@@ -1251,3 +1303,33 @@ end:
        bt_put(trace_packet_header_type);
        return ret;
 }
+
+bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace)
+{
+       bool is_static = false;
+
+       if (!trace) {
+               goto end;
+       }
+
+       is_static = trace->is_static;
+
+end:
+       return is_static;
+}
+
+int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace)
+{
+       int ret = 0;
+
+       if (!trace) {
+               ret = -1;
+               goto end;
+       }
+
+       trace->is_static = true;
+       bt_ctf_trace_freeze(trace);
+
+end:
+       return ret;
+}
This page took 0.026175 seconds and 4 git commands to generate.