From 44e0a4f5a816e471f1e74278704a6e52f94ab975 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 9 Apr 2015 11:45:10 -0400 Subject: [PATCH] Move bt_ctf_attributes API to internal headers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/Makefile.am | 1 + formats/ctf/ir/attributes.c | 294 ++++++++++++++++++ formats/ctf/ir/event.c | 1 + formats/ctf/ir/trace.c | 1 + formats/ctf/ir/utils.c | 267 ---------------- include/Makefile.am | 1 + .../babeltrace/ctf-ir/attributes-internal.h | 70 +++++ include/babeltrace/ctf-ir/utils.h | 29 -- 8 files changed, 368 insertions(+), 296 deletions(-) create mode 100644 formats/ctf/ir/attributes.c create mode 100644 include/babeltrace/ctf-ir/attributes-internal.h diff --git a/formats/ctf/ir/Makefile.am b/formats/ctf/ir/Makefile.am index b3609176..299c17ff 100644 --- a/formats/ctf/ir/Makefile.am +++ b/formats/ctf/ir/Makefile.am @@ -3,6 +3,7 @@ AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include noinst_LTLIBRARIES = libctf-ir.la libctf_ir_la_SOURCES = \ + attributes.c \ clock.c \ event.c \ event-fields.c \ diff --git a/formats/ctf/ir/attributes.c b/formats/ctf/ir/attributes.c new file mode 100644 index 00000000..619b28ed --- /dev/null +++ b/formats/ctf/ir/attributes.c @@ -0,0 +1,294 @@ +/* + * attributes.c + * + * Babeltrace CTF IR - Attributes + * + * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation + * Copyright (c) 2015 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include + +#define BT_CTF_ATTR_NAME_INDEX 0 +#define BT_CTF_ATTR_VALUE_INDEX 1 + +BT_HIDDEN +struct bt_object *bt_ctf_attributes_create(void) +{ + /* + * Attributes: array object of array objects, each one + * containing two entries: a string object (attributes field + * name), and an object (attributes field value). + * + * Example (JSON representation): + * + * [ + * ["hostname", "eeppdesk"], + * ["sysname", "Linux"], + * ["tracer_major", 2], + * ["tracer_minor", 5] + * ] + */ + return bt_object_array_create(); +} + +BT_HIDDEN +void bt_ctf_attributes_destroy(struct bt_object *attr_obj) +{ + bt_object_put(attr_obj); +} + +BT_HIDDEN +int bt_ctf_attributes_get_count(struct bt_object *attr_obj) +{ + return bt_object_array_size(attr_obj); +} + +BT_HIDDEN +const char *bt_ctf_attributes_get_field_name(struct bt_object *attr_obj, + int index) +{ + int rc; + const char *ret = NULL; + struct bt_object *attr_field_obj = NULL; + struct bt_object *attr_field_name_obj = NULL; + + if (!attr_obj || index < 0) { + goto end; + } + + attr_field_obj = bt_object_array_get(attr_obj, index); + + if (!attr_field_obj) { + goto end; + } + + attr_field_name_obj = bt_object_array_get(attr_field_obj, + BT_CTF_ATTR_NAME_INDEX); + + if (!attr_field_name_obj) { + goto end; + } + + rc = bt_object_string_get(attr_field_name_obj, &ret); + + if (rc) { + ret = NULL; + } + +end: + BT_OBJECT_PUT(attr_field_name_obj); + BT_OBJECT_PUT(attr_field_obj); + + return ret; +} + +BT_HIDDEN +struct bt_object *bt_ctf_attributes_get_field_value(struct bt_object *attr_obj, + int index) +{ + struct bt_object *value_obj = NULL; + struct bt_object *attr_field_obj = NULL; + + if (!attr_obj || index < 0) { + goto end; + } + + attr_field_obj = bt_object_array_get(attr_obj, index); + + if (!attr_field_obj) { + goto end; + } + + value_obj = bt_object_array_get(attr_field_obj, + BT_CTF_ATTR_VALUE_INDEX); + +end: + BT_OBJECT_PUT(attr_field_obj); + + return value_obj; +} + +static +struct bt_object *bt_ctf_attributes_get_field_by_name( + struct bt_object *attr_obj, const char *name) +{ + int i; + int attr_size; + struct bt_object *value_obj = NULL; + struct bt_object *attr_field_name_obj = NULL; + + attr_size = bt_object_array_size(attr_obj); + + if (attr_size < 0) { + goto error; + } + + for (i = 0; i < attr_size; ++i) { + int ret; + const char *field_name; + + value_obj = bt_object_array_get(attr_obj, i); + + if (!value_obj) { + goto error; + } + + attr_field_name_obj = bt_object_array_get(value_obj, 0); + + if (!attr_field_name_obj) { + goto error; + } + + ret = bt_object_string_get(attr_field_name_obj, &field_name); + if (ret) { + goto error; + } + + if (!strcmp(field_name, name)) { + BT_OBJECT_PUT(attr_field_name_obj); + break; + } + + BT_OBJECT_PUT(attr_field_name_obj); + BT_OBJECT_PUT(value_obj); + } + + return value_obj; + +error: + BT_OBJECT_PUT(attr_field_name_obj); + BT_OBJECT_PUT(value_obj); + + return value_obj; +} + +BT_HIDDEN +int bt_ctf_attributes_set_field_value(struct bt_object *attr_obj, + const char *name, struct bt_object *value_obj) +{ + int ret = 0; + struct bt_object *attr_field_obj = NULL; + + if (!attr_obj || !name || !value_obj) { + ret = -1; + goto end; + } + + attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); + + if (attr_field_obj) { + ret = bt_object_array_set(attr_field_obj, + BT_CTF_ATTR_VALUE_INDEX, value_obj); + goto end; + } + + attr_field_obj = bt_object_array_create(); + + if (!attr_field_obj) { + ret = -1; + goto end; + } + + ret = bt_object_array_append_string(attr_field_obj, name); + ret |= bt_object_array_append(attr_field_obj, value_obj); + + if (ret) { + goto end; + } + + ret = bt_object_array_append(attr_obj, attr_field_obj); + +end: + BT_OBJECT_PUT(attr_field_obj); + + return ret; +} + +BT_HIDDEN +struct bt_object *bt_ctf_attributes_get_field_value_by_name( + struct bt_object *attr_obj, const char *name) +{ + struct bt_object *value_obj = NULL; + struct bt_object *attr_field_obj = NULL; + + if (!attr_obj || !name) { + goto end; + } + + attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); + + if (!attr_field_obj) { + goto end; + } + + value_obj = bt_object_array_get(attr_field_obj, + BT_CTF_ATTR_VALUE_INDEX); + +end: + BT_OBJECT_PUT(attr_field_obj); + + return value_obj; +} + +BT_HIDDEN +int bt_ctf_attributes_freeze(struct bt_object *attr_obj) +{ + int i; + int count; + int ret = 0; + + if (!attr_obj) { + ret = -1; + goto end; + } + + count = bt_object_array_size(attr_obj); + + if (count < 0) { + ret = -1; + goto end; + } + + /* + * We do not freeze the array itself here, since internal + * stuff could need to modify/add attributes. Each attribute + * is frozen one by one. + */ + for (i = 0; i < count; ++i) { + struct bt_object *obj = NULL; + + obj = bt_ctf_attributes_get_field_value(attr_obj, i); + + if (!obj) { + ret = -1; + goto end; + } + + bt_object_freeze(obj); + BT_OBJECT_PUT(obj); + } + +end: + + return ret; +} diff --git a/formats/ctf/ir/event.c b/formats/ctf/ir/event.c index 543ecb7f..2d15c834 100644 --- a/formats/ctf/ir/event.c +++ b/formats/ctf/ir/event.c @@ -36,6 +36,7 @@ #include #include #include +#include #include static diff --git a/formats/ctf/ir/trace.c b/formats/ctf/ir/trace.c index 476d62c3..ca74d170 100644 --- a/formats/ctf/ir/trace.c +++ b/formats/ctf/ir/trace.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include diff --git a/formats/ctf/ir/utils.c b/formats/ctf/ir/utils.c index b8e8ac23..318b12da 100644 --- a/formats/ctf/ir/utils.c +++ b/formats/ctf/ir/utils.c @@ -29,11 +29,6 @@ #include #include #include -#include -#include - -#define BT_CTF_ATTR_NAME_INDEX 0 -#define BT_CTF_ATTR_VALUE_INDEX 1 static const char * const reserved_keywords_str[] = {"align", "callsite", @@ -109,265 +104,3 @@ end: free(string); return ret; } - -BT_HIDDEN -struct bt_object *bt_ctf_attributes_create(void) -{ - /* - * Attributes: array object of array objects, each one - * containing two entries: a string object (attributes field - * name), and an object (attributes field value). - * - * Example (JSON representation): - * - * [ - * ["hostname", "eeppdesk"], - * ["sysname", "Linux"], - * ["tracer_major", 2], - * ["tracer_minor", 5] - * ] - */ - return bt_object_array_create(); -} - -BT_HIDDEN -void bt_ctf_attributes_destroy(struct bt_object *attr_obj) -{ - bt_object_put(attr_obj); -} - -BT_HIDDEN -int bt_ctf_attributes_get_count(struct bt_object *attr_obj) -{ - return bt_object_array_size(attr_obj); -} - -BT_HIDDEN -const char *bt_ctf_attributes_get_field_name(struct bt_object *attr_obj, - int index) -{ - int rc; - const char *ret = NULL; - struct bt_object *attr_field_obj = NULL; - struct bt_object *attr_field_name_obj = NULL; - - if (!attr_obj || index < 0) { - goto end; - } - - attr_field_obj = bt_object_array_get(attr_obj, index); - - if (!attr_field_obj) { - goto end; - } - - attr_field_name_obj = bt_object_array_get(attr_field_obj, - BT_CTF_ATTR_NAME_INDEX); - - if (!attr_field_name_obj) { - goto end; - } - - rc = bt_object_string_get(attr_field_name_obj, &ret); - - if (rc) { - ret = NULL; - } - -end: - BT_OBJECT_PUT(attr_field_name_obj); - BT_OBJECT_PUT(attr_field_obj); - - return ret; -} - -BT_HIDDEN -struct bt_object *bt_ctf_attributes_get_field_value(struct bt_object *attr_obj, - int index) -{ - struct bt_object *value_obj = NULL; - struct bt_object *attr_field_obj = NULL; - - if (!attr_obj || index < 0) { - goto end; - } - - attr_field_obj = bt_object_array_get(attr_obj, index); - - if (!attr_field_obj) { - goto end; - } - - value_obj = bt_object_array_get(attr_field_obj, - BT_CTF_ATTR_VALUE_INDEX); - -end: - BT_OBJECT_PUT(attr_field_obj); - - return value_obj; -} - -static -struct bt_object *bt_ctf_attributes_get_field_by_name( - struct bt_object *attr_obj, const char *name) -{ - int i; - int attr_size; - struct bt_object *value_obj = NULL; - struct bt_object *attr_field_name_obj = NULL; - - attr_size = bt_object_array_size(attr_obj); - - if (attr_size < 0) { - goto error; - } - - for (i = 0; i < attr_size; ++i) { - int ret; - const char *field_name; - - value_obj = bt_object_array_get(attr_obj, i); - - if (!value_obj) { - goto error; - } - - attr_field_name_obj = bt_object_array_get(value_obj, 0); - - if (!attr_field_name_obj) { - goto error; - } - - ret = bt_object_string_get(attr_field_name_obj, &field_name); - if (ret) { - goto error; - } - - if (!strcmp(field_name, name)) { - BT_OBJECT_PUT(attr_field_name_obj); - break; - } - - BT_OBJECT_PUT(attr_field_name_obj); - BT_OBJECT_PUT(value_obj); - } - - return value_obj; - -error: - BT_OBJECT_PUT(attr_field_name_obj); - BT_OBJECT_PUT(value_obj); - - return value_obj; -} - -BT_HIDDEN -int bt_ctf_attributes_set_field_value(struct bt_object *attr_obj, - const char *name, struct bt_object *value_obj) -{ - int ret = 0; - struct bt_object *attr_field_obj = NULL; - - if (!attr_obj || !name || !value_obj) { - ret = -1; - goto end; - } - - attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); - - if (attr_field_obj) { - ret = bt_object_array_set(attr_field_obj, - BT_CTF_ATTR_VALUE_INDEX, value_obj); - goto end; - } - - attr_field_obj = bt_object_array_create(); - - if (!attr_field_obj) { - ret = -1; - goto end; - } - - ret = bt_object_array_append_string(attr_field_obj, name); - ret |= bt_object_array_append(attr_field_obj, value_obj); - - if (ret) { - goto end; - } - - ret = bt_object_array_append(attr_obj, attr_field_obj); - -end: - BT_OBJECT_PUT(attr_field_obj); - - return ret; -} - -BT_HIDDEN -struct bt_object *bt_ctf_attributes_get_field_value_by_name( - struct bt_object *attr_obj, const char *name) -{ - struct bt_object *value_obj = NULL; - struct bt_object *attr_field_obj = NULL; - - if (!attr_obj || !name) { - goto end; - } - - attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name); - - if (!attr_field_obj) { - goto end; - } - - value_obj = bt_object_array_get(attr_field_obj, - BT_CTF_ATTR_VALUE_INDEX); - -end: - BT_OBJECT_PUT(attr_field_obj); - - return value_obj; -} - -BT_HIDDEN -int bt_ctf_attributes_freeze(struct bt_object *attr_obj) -{ - int i; - int count; - int ret = 0; - - if (!attr_obj) { - ret = -1; - goto end; - } - - count = bt_object_array_size(attr_obj); - - if (count < 0) { - ret = -1; - goto end; - } - - /* - * We do not freeze the array itself here, since internal - * stuff could need to modify/add attributes. Each attribute - * is frozen one by one. - */ - for (i = 0; i < count; ++i) { - struct bt_object *obj = NULL; - - obj = bt_ctf_attributes_get_field_value(attr_obj, i); - - if (!obj) { - ret = -1; - goto end; - } - - bt_object_freeze(obj); - BT_OBJECT_PUT(obj); - } - -end: - - return ret; -} diff --git a/include/Makefile.am b/include/Makefile.am index 2928f757..17d4faae 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -53,6 +53,7 @@ noinst_HEADERS = \ babeltrace/ctf/ctf-index.h \ babeltrace/ctf-writer/ref-internal.h \ babeltrace/ctf-writer/writer-internal.h \ + babeltrace/ctf-ir/attributes-internal.h \ babeltrace/ctf-ir/event-types-internal.h \ babeltrace/ctf-ir/event-fields-internal.h \ babeltrace/ctf-ir/event-internal.h \ diff --git a/include/babeltrace/ctf-ir/attributes-internal.h b/include/babeltrace/ctf-ir/attributes-internal.h new file mode 100644 index 00000000..e383c28d --- /dev/null +++ b/include/babeltrace/ctf-ir/attributes-internal.h @@ -0,0 +1,70 @@ +#ifndef BABELTRACE_CTF_IR_ATTRIBUTES_H +#define BABELTRACE_CTF_IR_ATTRIBUTES_H + +/* + * attributes.c + * + * Babeltrace - CTF IR: Attributes internal + * + * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation + * Copyright (c) 2015 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +BT_HIDDEN +struct bt_object *bt_ctf_attributes_create(void); + +BT_HIDDEN +void bt_ctf_attributes_destroy(struct bt_object *attr_obj); + +BT_HIDDEN +int bt_ctf_attributes_get_count(struct bt_object *attr_obj); + +BT_HIDDEN +const char *bt_ctf_attributes_get_field_name(struct bt_object *attr_obj, + int index); + +BT_HIDDEN +struct bt_object *bt_ctf_attributes_get_field_value(struct bt_object *attr_obj, + int index); + +BT_HIDDEN +int bt_ctf_attributes_set_field_value(struct bt_object *attr_obj, + const char *name, struct bt_object *value_obj); + +BT_HIDDEN +struct bt_object *bt_ctf_attributes_get_field_value_by_name( + struct bt_object *attr_obj, const char *name); + +BT_HIDDEN +int bt_ctf_attributes_freeze(struct bt_object *attr_obj); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE_CTF_IR_ATTRIBUTES_H */ diff --git a/include/babeltrace/ctf-ir/utils.h b/include/babeltrace/ctf-ir/utils.h index de4a07ef..e574bd60 100644 --- a/include/babeltrace/ctf-ir/utils.h +++ b/include/babeltrace/ctf-ir/utils.h @@ -34,7 +34,6 @@ extern "C" { #endif -#include #include /* @@ -52,34 +51,6 @@ extern "C" { */ extern int bt_ctf_validate_identifier(const char *identifier); -BT_HIDDEN -struct bt_object *bt_ctf_attributes_create(void); - -BT_HIDDEN -void bt_ctf_attributes_destroy(struct bt_object *attr_obj); - -BT_HIDDEN -int bt_ctf_attributes_get_count(struct bt_object *attr_obj); - -BT_HIDDEN -const char *bt_ctf_attributes_get_field_name(struct bt_object *attr_obj, - int index); - -BT_HIDDEN -struct bt_object *bt_ctf_attributes_get_field_value(struct bt_object *attr_obj, - int index); - -BT_HIDDEN -int bt_ctf_attributes_set_field_value(struct bt_object *attr_obj, - const char *name, struct bt_object *value_obj); - -BT_HIDDEN -struct bt_object *bt_ctf_attributes_get_field_value_by_name( - struct bt_object *attr_obj, const char *name); - -BT_HIDDEN -int bt_ctf_attributes_freeze(struct bt_object *attr_obj); - #ifdef __cplusplus } #endif -- 2.34.1