Fix: get_declaration_* should not cast to field
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 8 Sep 2012 23:37:46 +0000 (19:37 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 8 Sep 2012 23:37:46 +0000 (19:37 -0400)
Also add const typing.

Signed-off-by: Julien Desfossez <jdesfossez@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
formats/ctf/events.c

index c281db54a2844feb249b39b7ee9958a7b697d286..d3488b3f9d7b8e3cb010d2c6d8497e16afbaa4ff 100644 (file)
@@ -41,8 +41,8 @@ __thread int bt_ctf_last_field_error = 0;
 const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
                enum bt_ctf_scope scope)
 {
-       struct definition *tmp = NULL;
-       struct ctf_event_definition *event;
+       const struct definition *tmp = NULL;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return NULL;
@@ -92,7 +92,7 @@ const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
                const struct definition *scope,
                const char *field)
 {
-       struct definition *def;
+       const struct definition *def;
        char *field_underscore;
 
        if (!ctf_event || !scope || !field)
@@ -111,9 +111,9 @@ const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
                g_free(field_underscore);
        }
        if (bt_ctf_field_type(bt_ctf_get_field_decl(def)) == CTF_TYPE_VARIANT) {
-               struct definition_variant *variant_definition;
+               const struct definition_variant *variant_definition;
                variant_definition = container_of(def,
-                               struct definition_variant, p);
+                               const struct definition_variant, p);
                return variant_definition->current_field;
        }
        return def;
@@ -144,9 +144,9 @@ const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
 
 const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_declaration *event_class;
-       struct ctf_stream_declaration *stream_class;
-       struct ctf_event_definition *event;
+       const struct ctf_event_declaration *event_class;
+       const struct ctf_stream_declaration *stream_class;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return NULL;
@@ -270,15 +270,15 @@ error:
 struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event)
 {
        struct bt_context *ret = NULL;
-       struct ctf_file_stream *cfs;
-       struct ctf_trace *trace;
-       struct ctf_event_definition *event;
+       const struct ctf_file_stream *cfs;
+       const struct ctf_trace *trace;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return NULL;
 
        event = ctf_event->parent;
-       cfs = container_of(event->stream, struct ctf_file_stream,
+       cfs = container_of(event->stream, const struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
        if (trace->ctx)
@@ -290,15 +290,15 @@ struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event
 int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
 {
        int ret = -1;
-       struct ctf_file_stream *cfs;
-       struct ctf_trace *trace;
-       struct ctf_event_definition *event;
+       const struct ctf_file_stream *cfs;
+       const struct ctf_trace *trace;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return -EINVAL;
 
        event = ctf_event->parent;
-       cfs = container_of(event->stream, struct ctf_file_stream,
+       cfs = container_of(event->stream, const struct ctf_file_stream,
                        parent);
        trace = cfs->parent.stream_class->trace;
        if (trace->handle)
@@ -309,7 +309,7 @@ int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
 
 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_definition *event;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return -1ULL;
@@ -323,7 +323,7 @@ uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
 
 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event)
 {
-       struct ctf_event_definition *event;
+       const struct ctf_event_definition *event;
 
        if (!ctf_event)
                return -1ULL;
@@ -349,125 +349,94 @@ int bt_ctf_field_get_error(void)
        return ret;
 }
 
-static struct declaration_integer *get_declaration_integer(const struct declaration *decl)
+static const struct declaration_integer *
+get_declaration_integer(const struct declaration *decl)
 {
-       struct declaration_field *field_decl;
-       struct declaration_integer *ret = NULL;
-
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_INTEGER) {
-               field_decl = (struct declaration_field *) decl;
-               ret = ((struct declaration_integer *) field_decl->declaration);
-       }
-
-       return ret;
+       if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_INTEGER)
+               return NULL;
+       return container_of(decl, const struct declaration_integer, p);
 }
 
-static struct declaration_string *get_declaration_string(const struct declaration *decl)
+static const struct declaration_string *
+get_declaration_string(const struct declaration *decl)
 {
-       struct declaration_field *field_decl;
-       struct declaration_string *ret = NULL;
-
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_STRING) {
-               field_decl = (struct declaration_field *) decl;
-               ret = ((struct declaration_string *) field_decl->declaration);
-       }
-
-       return ret;
+       if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_STRING)
+               return NULL;
+       return container_of(decl, const struct declaration_string, p);
 }
 
-static struct declaration_array *get_declaration_array(const struct declaration *decl)
+static const struct declaration_array *
+get_declaration_array(const struct declaration *decl)
 {
-       struct declaration_field *field_decl;
-       struct declaration_array *ret = NULL;
-
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_ARRAY) {
-               field_decl = (struct declaration_field *) decl;
-               ret = ((struct declaration_array *) field_decl->declaration);
-       }
-
-       return ret;
+       if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_ARRAY)
+               return NULL;
+       return container_of(decl, const struct declaration_array, p);
 }
 
 int bt_ctf_get_int_signedness(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = integer->signedness;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return integer->signedness;
 }
 
 int bt_ctf_get_int_base(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = integer->base;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return integer->base;
 }
 
 int bt_ctf_get_int_byte_order(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = integer->byte_order;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return integer->byte_order;
 }
 
 ssize_t bt_ctf_get_int_len(const struct declaration *decl)
 {
-       ssize_t ret;
-       struct declaration_integer *integer;
+       const struct declaration_integer *integer;
 
        integer = get_declaration_integer(decl);
-       if (integer) {
-               ret = (ssize_t) integer->len;
-       } else {
-               ret = -EINVAL;
+       if (!integer) {
                bt_ctf_field_set_error(-EINVAL);
+               return -EINVAL;
        }
-
-       return ret;
+       return (ssize_t) integer->len;
 }
 
 const struct definition *bt_ctf_get_enum_int(const struct definition *field)
 {
-       struct definition_enum *def_enum;
+       const struct definition_enum *def_enum;
 
        if (!field || bt_ctf_field_type(bt_ctf_get_field_decl(field)) != CTF_TYPE_ENUM) {
                bt_ctf_field_set_error(-EINVAL);
                return NULL;
        }
-       def_enum = container_of(field, struct definition_enum, p);
+       def_enum = container_of(field, const struct definition_enum, p);
        return &def_enum->integer->p;
 }
 
 const char *bt_ctf_get_enum_str(const struct definition *field)
 {
-       struct definition_enum *def_enum;
-       struct declaration_enum *decl_enum;
+       const struct definition_enum *def_enum;
+       const struct declaration_enum *decl_enum;
        GArray *array;
        const char *ret;
 
@@ -475,7 +444,7 @@ const char *bt_ctf_get_enum_str(const struct definition *field)
                bt_ctf_field_set_error(-EINVAL);
                return NULL;
        }
-       def_enum = container_of(field, struct definition_enum, p);
+       def_enum = container_of(field, const struct definition_enum, p);
        decl_enum = def_enum->declaration;
        if (get_int_signedness(&def_enum->integer->p)) {
                array = enum_int_to_quark_set(decl_enum,
@@ -503,8 +472,8 @@ const char *bt_ctf_get_enum_str(const struct definition *field)
 enum ctf_string_encoding bt_ctf_get_encoding(const struct declaration *decl)
 {
        enum ctf_string_encoding ret = 0;
-       struct declaration_integer *integer;
-       struct declaration_string *string;
+       const struct declaration_integer *integer;
+       const struct declaration_string *string;
 
        if (!decl)
                goto error;
@@ -535,21 +504,12 @@ error:
 
 int bt_ctf_get_array_len(const struct declaration *decl)
 {
-       int ret;
-       struct declaration_array *array;
+       const struct declaration_array *array;
 
-       if (decl && bt_ctf_field_type(decl) == CTF_TYPE_ARRAY) {
-               array = get_declaration_array(decl);
-               if (array) {
-                       ret = array->len;
-               } else {
-                       goto error;
-               }
-       } else {
+       array = get_declaration_array(decl);
+       if (!array)
                goto error;
-       }
-
-       return ret;
+       return array->len;
 
 error:
        bt_ctf_field_set_error(-EINVAL);
@@ -578,7 +538,6 @@ int64_t bt_ctf_get_int64(const struct definition *field)
                bt_ctf_field_set_error(-EINVAL);
 
        return ret;
-
 }
 
 char *bt_ctf_get_char_array(const struct definition *field)
This page took 0.029523 seconds and 4 git commands to generate.