From: Jérémie Galarneau Date: Sat, 22 Oct 2016 21:06:41 +0000 (-0400) Subject: Hide bt_values and bt_attributes in libbabeltrace-ctf X-Git-Tag: v1.5.0-rc1~18 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=81eb86805c5fe2e59503e045994ad0d4b055156c Hide bt_values and bt_attributes in libbabeltrace-ctf Signed-off-by: Jérémie Galarneau --- diff --git a/formats/ctf/ir/Makefile.am b/formats/ctf/ir/Makefile.am index b9048bc5..1b7b5d89 100644 --- a/formats/ctf/ir/Makefile.am +++ b/formats/ctf/ir/Makefile.am @@ -16,7 +16,9 @@ libctf_ir_la_SOURCES = \ trace.c \ utils.c \ resolve.c \ - validation.c + validation.c \ + values.c \ + ref.c libctf_ir_la_LIBADD = \ $(top_builddir)/lib/libbabeltrace.la diff --git a/formats/ctf/ir/ref.c b/formats/ctf/ir/ref.c new file mode 100644 index 00000000..ac058013 --- /dev/null +++ b/formats/ctf/ir/ref.c @@ -0,0 +1,55 @@ +/* + * ref.c: reference counting + * + * Babeltrace Library + * + * Copyright (c) 2015 Jérémie Galarneau + * + * 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 + +void *bt_get(void *ptr) +{ + struct bt_object *obj = ptr; + + if (!obj) { + goto end; + } + + if (obj->parent && bt_object_get_ref_count(obj) == 0) { + bt_get(obj->parent); + } + bt_ref_get(&obj->ref_count); +end: + return obj; +} + +void bt_put(void *ptr) +{ + struct bt_object *obj = ptr; + + if (!obj) { + return; + } + + bt_ref_put(&obj->ref_count); +} diff --git a/formats/ctf/ir/values.c b/formats/ctf/ir/values.c new file mode 100644 index 00000000..61aa1522 --- /dev/null +++ b/formats/ctf/ir/values.c @@ -0,0 +1,1252 @@ +/* + * Values.c: value objects + * + * Babeltrace Library + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +#define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete)) +#define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base)) +#define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base)) +#define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base)) +#define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base)) +#define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base)) +#define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base)) + +struct bt_value { + struct bt_object base; + enum bt_value_type type; + bool is_frozen; +}; + +static +struct bt_value bt_value_null_instance = { + .type = BT_VALUE_TYPE_NULL, + .is_frozen = true, +}; + +struct bt_value *bt_value_null = &bt_value_null_instance; + +struct bt_value_bool { + struct bt_value base; + bool value; +}; + +struct bt_value_integer { + struct bt_value base; + int64_t value; +}; + +struct bt_value_float { + struct bt_value base; + double value; +}; + +struct bt_value_string { + struct bt_value base; + GString *gstr; +}; + +struct bt_value_array { + struct bt_value base; + GPtrArray *garray; +}; + +struct bt_value_map { + struct bt_value base; + GHashTable *ght; +}; + +static +void bt_value_destroy(struct bt_object *obj); + +static +void bt_value_string_destroy(struct bt_value *object) +{ + g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE); +} + +static +void bt_value_array_destroy(struct bt_value *object) +{ + /* + * Pointer array's registered value destructor will take care + * of putting each contained object. + */ + g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE); +} + +static +void bt_value_map_destroy(struct bt_value *object) +{ + /* + * Hash table's registered value destructor will take care of + * putting each contained object. Keys are GQuarks and cannot + * be destroyed anyway. + */ + g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght); +} + +static +void (* const destroy_funcs[])(struct bt_value *) = { + [BT_VALUE_TYPE_NULL] = NULL, + [BT_VALUE_TYPE_BOOL] = NULL, + [BT_VALUE_TYPE_INTEGER] = NULL, + [BT_VALUE_TYPE_FLOAT] = NULL, + [BT_VALUE_TYPE_STRING] = bt_value_string_destroy, + [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy, + [BT_VALUE_TYPE_MAP] = bt_value_map_destroy, +}; + +static +struct bt_value *bt_value_null_copy(const struct bt_value *null_obj) +{ + return bt_value_null; +} + +static +struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj) +{ + return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj)->value); +} + +static +struct bt_value *bt_value_integer_copy(const struct bt_value *integer_obj) +{ + return bt_value_integer_create_init( + BT_VALUE_TO_INTEGER(integer_obj)->value); +} + +static +struct bt_value *bt_value_float_copy(const struct bt_value *float_obj) +{ + return bt_value_float_create_init( + BT_VALUE_TO_FLOAT(float_obj)->value); +} + +static +struct bt_value *bt_value_string_copy(const struct bt_value *string_obj) +{ + return bt_value_string_create_init( + BT_VALUE_TO_STRING(string_obj)->gstr->str); +} + +static +struct bt_value *bt_value_array_copy(const struct bt_value *array_obj) +{ + int i; + int ret; + struct bt_value *copy_obj; + struct bt_value_array *typed_array_obj; + + typed_array_obj = BT_VALUE_TO_ARRAY(array_obj); + copy_obj = bt_value_array_create(); + + if (!copy_obj) { + goto end; + } + + for (i = 0; i < typed_array_obj->garray->len; ++i) { + struct bt_value *element_obj_copy; + struct bt_value *element_obj = bt_value_array_get(array_obj, i); + + if (!element_obj) { + BT_PUT(copy_obj); + goto end; + } + + element_obj_copy = bt_value_copy(element_obj); + BT_PUT(element_obj); + + if (!element_obj_copy) { + BT_PUT(copy_obj); + goto end; + } + + ret = bt_value_array_append(copy_obj, element_obj_copy); + BT_PUT(element_obj_copy); + + if (ret) { + BT_PUT(copy_obj); + goto end; + } + } + +end: + return copy_obj; +} + +static +struct bt_value *bt_value_map_copy(const struct bt_value *map_obj) +{ + int ret; + GHashTableIter iter; + gpointer key, element_obj; + struct bt_value *copy_obj; + struct bt_value *element_obj_copy; + struct bt_value_map *typed_map_obj; + + typed_map_obj = BT_VALUE_TO_MAP(map_obj); + copy_obj = bt_value_map_create(); + + if (!copy_obj) { + goto end; + } + + g_hash_table_iter_init(&iter, typed_map_obj->ght); + + while (g_hash_table_iter_next(&iter, &key, &element_obj)) { + const char *key_str = g_quark_to_string((unsigned long) key); + + element_obj_copy = bt_value_copy(element_obj); + + if (!element_obj_copy) { + BT_PUT(copy_obj); + goto end; + } + + ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy); + BT_PUT(element_obj_copy); + + if (ret) { + BT_PUT(copy_obj); + goto end; + } + } + +end: + return copy_obj; +} + +static +struct bt_value *(* const copy_funcs[])(const struct bt_value *) = { + [BT_VALUE_TYPE_NULL] = bt_value_null_copy, + [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy, + [BT_VALUE_TYPE_INTEGER] = bt_value_integer_copy, + [BT_VALUE_TYPE_FLOAT] = bt_value_float_copy, + [BT_VALUE_TYPE_STRING] = bt_value_string_copy, + [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy, + [BT_VALUE_TYPE_MAP] = bt_value_map_copy, +}; + +static +bool bt_value_null_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + /* + * Always true since bt_value_compare() already checks if both + * object_a and object_b have the same type, and in the case of + * null value objects, they're always the same if it is so. + */ + return true; +} + +static +bool bt_value_bool_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + return BT_VALUE_TO_BOOL(object_a)->value == + BT_VALUE_TO_BOOL(object_b)->value; +} + +static +bool bt_value_integer_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + return BT_VALUE_TO_INTEGER(object_a)->value == + BT_VALUE_TO_INTEGER(object_b)->value; +} + +static +bool bt_value_float_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + return BT_VALUE_TO_FLOAT(object_a)->value == + BT_VALUE_TO_FLOAT(object_b)->value; +} + +static +bool bt_value_string_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + return !strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str, + BT_VALUE_TO_STRING(object_b)->gstr->str); +} + +static +bool bt_value_array_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + int i; + bool ret = true; + const struct bt_value_array *array_obj_a = + BT_VALUE_TO_ARRAY(object_a); + + if (bt_value_array_size(object_a) != bt_value_array_size(object_b)) { + ret = false; + goto end; + } + + for (i = 0; i < array_obj_a->garray->len; ++i) { + struct bt_value *element_obj_a; + struct bt_value *element_obj_b; + + element_obj_a = bt_value_array_get(object_a, i); + element_obj_b = bt_value_array_get(object_b, i); + + if (!bt_value_compare(element_obj_a, element_obj_b)) { + BT_PUT(element_obj_a); + BT_PUT(element_obj_b); + ret = false; + goto end; + } + + BT_PUT(element_obj_a); + BT_PUT(element_obj_b); + } + +end: + return ret; +} + +static +bool bt_value_map_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + bool ret = true; + GHashTableIter iter; + gpointer key, element_obj_a; + const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a); + + if (bt_value_map_size(object_a) != bt_value_map_size(object_b)) { + ret = false; + goto end; + } + + g_hash_table_iter_init(&iter, map_obj_a->ght); + + while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) { + struct bt_value *element_obj_b; + const char *key_str = g_quark_to_string((unsigned long) key); + + element_obj_b = bt_value_map_get(object_b, key_str); + + if (!bt_value_compare(element_obj_a, element_obj_b)) { + BT_PUT(element_obj_b); + ret = false; + goto end; + } + + BT_PUT(element_obj_b); + } + +end: + return ret; +} + +static +bool (* const compare_funcs[])(const struct bt_value *, + const struct bt_value *) = { + [BT_VALUE_TYPE_NULL] = bt_value_null_compare, + [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare, + [BT_VALUE_TYPE_INTEGER] = bt_value_integer_compare, + [BT_VALUE_TYPE_FLOAT] = bt_value_float_compare, + [BT_VALUE_TYPE_STRING] = bt_value_string_compare, + [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare, + [BT_VALUE_TYPE_MAP] = bt_value_map_compare, +}; + +BT_HIDDEN +void bt_value_null_freeze(struct bt_value *object) +{ +} + +BT_HIDDEN +void bt_value_generic_freeze(struct bt_value *object) +{ + object->is_frozen = true; +} + +BT_HIDDEN +void bt_value_array_freeze(struct bt_value *object) +{ + int i; + struct bt_value_array *typed_array_obj = + BT_VALUE_TO_ARRAY(object); + + for (i = 0; i < typed_array_obj->garray->len; ++i) { + struct bt_value *element_obj = + g_ptr_array_index(typed_array_obj->garray, i); + + bt_value_freeze(element_obj); + } + + bt_value_generic_freeze(object); +} + +BT_HIDDEN +void bt_value_map_freeze(struct bt_value *object) +{ + GHashTableIter iter; + gpointer key, element_obj; + const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object); + + g_hash_table_iter_init(&iter, map_obj->ght); + + while (g_hash_table_iter_next(&iter, &key, &element_obj)) { + bt_value_freeze(element_obj); + } + + bt_value_generic_freeze(object); +} + +static +void (* const freeze_funcs[])(struct bt_value *) = { + [BT_VALUE_TYPE_NULL] = bt_value_null_freeze, + [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze, + [BT_VALUE_TYPE_INTEGER] = bt_value_generic_freeze, + [BT_VALUE_TYPE_FLOAT] = bt_value_generic_freeze, + [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze, + [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze, + [BT_VALUE_TYPE_MAP] = bt_value_map_freeze, +}; + +static +void bt_value_destroy(struct bt_object *obj) +{ + struct bt_value *value; + + value = container_of(obj, struct bt_value, base); + assert(value->type != BT_VALUE_TYPE_UNKNOWN); + + if (bt_value_is_null(value)) { + return; + } + + if (destroy_funcs[value->type]) { + destroy_funcs[value->type](value); + } + + g_free(value); +} + +BT_HIDDEN +enum bt_value_status bt_value_freeze(struct bt_value *object) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + + if (!object) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + freeze_funcs[object->type](object); + +end: + return ret; +} + +BT_HIDDEN +bool bt_value_is_frozen(const struct bt_value *object) +{ + return object && object->is_frozen; +} + +BT_HIDDEN +enum bt_value_type bt_value_get_type(const struct bt_value *object) +{ + if (!object) { + return BT_VALUE_TYPE_UNKNOWN; + } + + return object->type; +} + +static +struct bt_value bt_value_create_base(enum bt_value_type type) +{ + struct bt_value base; + + base.type = type; + base.is_frozen = false; + bt_object_init(&base, bt_value_destroy); + + return base; +} + +BT_HIDDEN +struct bt_value *bt_value_bool_create_init(bool val) +{ + struct bt_value_bool *bool_obj; + + bool_obj = g_new0(struct bt_value_bool, 1); + + if (!bool_obj) { + goto end; + } + + bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL); + bool_obj->value = val; + +end: + return BT_VALUE_FROM_CONCRETE(bool_obj); +} + +BT_HIDDEN +struct bt_value *bt_value_bool_create(void) +{ + return bt_value_bool_create_init(false); +} + +BT_HIDDEN +struct bt_value *bt_value_integer_create_init(int64_t val) +{ + struct bt_value_integer *integer_obj; + + integer_obj = g_new0(struct bt_value_integer, 1); + + if (!integer_obj) { + goto end; + } + + integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER); + integer_obj->value = val; + +end: + return BT_VALUE_FROM_CONCRETE(integer_obj); +} + +BT_HIDDEN +struct bt_value *bt_value_integer_create(void) +{ + return bt_value_integer_create_init(0); +} + +BT_HIDDEN +struct bt_value *bt_value_float_create_init(double val) +{ + struct bt_value_float *float_obj; + + float_obj = g_new0(struct bt_value_float, 1); + + if (!float_obj) { + goto end; + } + + float_obj->base = bt_value_create_base(BT_VALUE_TYPE_FLOAT); + float_obj->value = val; + +end: + return BT_VALUE_FROM_CONCRETE(float_obj); +} + +BT_HIDDEN +struct bt_value *bt_value_float_create(void) +{ + return bt_value_float_create_init(0.); +} + +BT_HIDDEN +struct bt_value *bt_value_string_create_init(const char *val) +{ + struct bt_value_string *string_obj = NULL; + + if (!val) { + goto end; + } + + string_obj = g_new0(struct bt_value_string, 1); + + if (!string_obj) { + goto end; + } + + string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING); + string_obj->gstr = g_string_new(val); + + if (!string_obj->gstr) { + g_free(string_obj); + string_obj = NULL; + goto end; + } + +end: + return BT_VALUE_FROM_CONCRETE(string_obj); +} + +BT_HIDDEN +struct bt_value *bt_value_string_create(void) +{ + return bt_value_string_create_init(""); +} + +BT_HIDDEN +struct bt_value *bt_value_array_create(void) +{ + struct bt_value_array *array_obj; + + array_obj = g_new0(struct bt_value_array, 1); + + if (!array_obj) { + goto end; + } + + array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY); + array_obj->garray = babeltrace_g_ptr_array_new_full(0, + (GDestroyNotify) bt_put); + + if (!array_obj->garray) { + g_free(array_obj); + array_obj = NULL; + goto end; + } + +end: + return BT_VALUE_FROM_CONCRETE(array_obj); +} + +BT_HIDDEN +struct bt_value *bt_value_map_create(void) +{ + struct bt_value_map *map_obj; + + map_obj = g_new0(struct bt_value_map, 1); + + if (!map_obj) { + goto end; + } + + map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP); + map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal, + NULL, (GDestroyNotify) bt_put); + + if (!map_obj->ght) { + g_free(map_obj); + map_obj = NULL; + goto end; + } + +end: + return BT_VALUE_FROM_CONCRETE(map_obj); +} + +BT_HIDDEN +enum bt_value_status bt_value_bool_get(const struct bt_value *bool_obj, + bool *val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj); + + if (!bool_obj || !bt_value_is_bool(bool_obj) || !val) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + *val = typed_bool_obj->value; + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bool val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj); + + if (!bool_obj || !bt_value_is_bool(bool_obj)) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + if (bool_obj->is_frozen) { + ret = BT_VALUE_STATUS_FROZEN; + goto end; + } + + typed_bool_obj->value = val; + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj, + int64_t *val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_integer *typed_integer_obj = + BT_VALUE_TO_INTEGER(integer_obj); + + if (!integer_obj || !bt_value_is_integer(integer_obj) || !val) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + *val = typed_integer_obj->value; + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj, + int64_t val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_integer *typed_integer_obj = + BT_VALUE_TO_INTEGER(integer_obj); + + if (!integer_obj || !bt_value_is_integer(integer_obj)) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + if (integer_obj->is_frozen) { + ret = BT_VALUE_STATUS_FROZEN; + goto end; + } + + typed_integer_obj->value = val; + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_float_get(const struct bt_value *float_obj, + double *val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_float *typed_float_obj = + BT_VALUE_TO_FLOAT(float_obj); + + if (!float_obj || !bt_value_is_float(float_obj) || !val) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + *val = typed_float_obj->value; + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_float_set(struct bt_value *float_obj, + double val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_float *typed_float_obj = + BT_VALUE_TO_FLOAT(float_obj); + + if (!float_obj || !bt_value_is_float(float_obj)) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + if (float_obj->is_frozen) { + ret = BT_VALUE_STATUS_FROZEN; + goto end; + } + + typed_float_obj->value = val; + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_string_get(const struct bt_value *string_obj, + const char **val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_string *typed_string_obj = + BT_VALUE_TO_STRING(string_obj); + + if (!string_obj || !bt_value_is_string(string_obj) || !val) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + *val = typed_string_obj->gstr->str; + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_string_set(struct bt_value *string_obj, + const char *val) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_string *typed_string_obj = + BT_VALUE_TO_STRING(string_obj); + + if (!string_obj || !bt_value_is_string(string_obj) || !val) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + if (string_obj->is_frozen) { + ret = BT_VALUE_STATUS_FROZEN; + goto end; + } + + g_string_assign(typed_string_obj->gstr, val); + +end: + return ret; +} + +BT_HIDDEN +int bt_value_array_size(const struct bt_value *array_obj) +{ + int ret; + struct bt_value_array *typed_array_obj = + BT_VALUE_TO_ARRAY(array_obj); + + if (!array_obj || !bt_value_is_array(array_obj)) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + ret = (int) typed_array_obj->garray->len; + +end: + return ret; +} + +BT_HIDDEN +bool bt_value_array_is_empty(const struct bt_value *array_obj) +{ + return bt_value_array_size(array_obj) == 0; +} + +BT_HIDDEN +struct bt_value *bt_value_array_get(const struct bt_value *array_obj, + size_t index) +{ + struct bt_value *ret; + struct bt_value_array *typed_array_obj = + BT_VALUE_TO_ARRAY(array_obj); + + if (!array_obj || !bt_value_is_array(array_obj) || + index >= typed_array_obj->garray->len) { + ret = NULL; + goto end; + } + + ret = g_ptr_array_index(typed_array_obj->garray, index); + bt_get(ret); + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_append(struct bt_value *array_obj, + struct bt_value *element_obj) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_array *typed_array_obj = + BT_VALUE_TO_ARRAY(array_obj); + + if (!array_obj || !bt_value_is_array(array_obj) || !element_obj) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + if (array_obj->is_frozen) { + ret = BT_VALUE_STATUS_FROZEN; + goto end; + } + + g_ptr_array_add(typed_array_obj->garray, element_obj); + bt_get(element_obj); + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj, + bool val) +{ + enum bt_value_status ret; + struct bt_value *bool_obj = NULL; + + bool_obj = bt_value_bool_create_init(val); + ret = bt_value_array_append(array_obj, bool_obj); + bt_put(bool_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_append_integer( + struct bt_value *array_obj, int64_t val) +{ + enum bt_value_status ret; + struct bt_value *integer_obj = NULL; + + integer_obj = bt_value_integer_create_init(val); + ret = bt_value_array_append(array_obj, integer_obj); + bt_put(integer_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_append_float(struct bt_value *array_obj, + double val) +{ + enum bt_value_status ret; + struct bt_value *float_obj = NULL; + + float_obj = bt_value_float_create_init(val); + ret = bt_value_array_append(array_obj, float_obj); + bt_put(float_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj, + const char *val) +{ + enum bt_value_status ret; + struct bt_value *string_obj = NULL; + + string_obj = bt_value_string_create_init(val); + ret = bt_value_array_append(array_obj, string_obj); + bt_put(string_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_append_empty_array( + struct bt_value *array_obj) +{ + enum bt_value_status ret; + struct bt_value *empty_array_obj = NULL; + + empty_array_obj = bt_value_array_create(); + ret = bt_value_array_append(array_obj, empty_array_obj); + bt_put(empty_array_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj) +{ + enum bt_value_status ret; + struct bt_value *map_obj = NULL; + + map_obj = bt_value_map_create(); + ret = bt_value_array_append(array_obj, map_obj); + bt_put(map_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_array_set(struct bt_value *array_obj, + size_t index, struct bt_value *element_obj) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_array *typed_array_obj = + BT_VALUE_TO_ARRAY(array_obj); + + if (!array_obj || !bt_value_is_array(array_obj) || !element_obj || + index >= typed_array_obj->garray->len) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + if (array_obj->is_frozen) { + ret = BT_VALUE_STATUS_FROZEN; + goto end; + } + + bt_put(g_ptr_array_index(typed_array_obj->garray, index)); + g_ptr_array_index(typed_array_obj->garray, index) = element_obj; + bt_get(element_obj); + +end: + return ret; +} + +BT_HIDDEN +int bt_value_map_size(const struct bt_value *map_obj) +{ + int ret; + struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); + + if (!map_obj || !bt_value_is_map(map_obj)) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + ret = (int) g_hash_table_size(typed_map_obj->ght); + +end: + return ret; +} + +BT_HIDDEN +bool bt_value_map_is_empty(const struct bt_value *map_obj) +{ + return bt_value_map_size(map_obj) == 0; +} + +BT_HIDDEN +struct bt_value *bt_value_map_get(const struct bt_value *map_obj, + const char *key) +{ + GQuark quark; + struct bt_value *ret; + struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); + + if (!map_obj || !bt_value_is_map(map_obj) || !key) { + ret = NULL; + goto end; + } + + quark = g_quark_from_string(key); + ret = g_hash_table_lookup(typed_map_obj->ght, GUINT_TO_POINTER(quark)); + + if (ret) { + bt_get(ret); + } + +end: + return ret; +} + +BT_HIDDEN +bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key) +{ + bool ret; + GQuark quark; + struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); + + if (!map_obj || !bt_value_is_map(map_obj) || !key) { + ret = false; + goto end; + } + + quark = g_quark_from_string(key); + ret = babeltrace_g_hash_table_contains(typed_map_obj->ght, + GUINT_TO_POINTER(quark)); + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_insert(struct bt_value *map_obj, + const char *key, struct bt_value *element_obj) +{ + GQuark quark; + enum bt_value_status ret = BT_VALUE_STATUS_OK; + struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); + + if (!map_obj || !bt_value_is_map(map_obj) || !key || !element_obj) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + if (map_obj->is_frozen) { + ret = BT_VALUE_STATUS_FROZEN; + goto end; + } + + quark = g_quark_from_string(key); + g_hash_table_insert(typed_map_obj->ght, + GUINT_TO_POINTER(quark), element_obj); + bt_get(element_obj); + +end: + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj, + const char *key, bool val) +{ + enum bt_value_status ret; + struct bt_value *bool_obj = NULL; + + bool_obj = bt_value_bool_create_init(val); + ret = bt_value_map_insert(map_obj, key, bool_obj); + bt_put(bool_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj, + const char *key, int64_t val) +{ + enum bt_value_status ret; + struct bt_value *integer_obj = NULL; + + integer_obj = bt_value_integer_create_init(val); + ret = bt_value_map_insert(map_obj, key, integer_obj); + bt_put(integer_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj, + const char *key, double val) +{ + enum bt_value_status ret; + struct bt_value *float_obj = NULL; + + float_obj = bt_value_float_create_init(val); + ret = bt_value_map_insert(map_obj, key, float_obj); + bt_put(float_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj, + const char *key, const char *val) +{ + enum bt_value_status ret; + struct bt_value *string_obj = NULL; + + string_obj = bt_value_string_create_init(val); + ret = bt_value_map_insert(map_obj, key, string_obj); + bt_put(string_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj, + const char *key) +{ + enum bt_value_status ret; + struct bt_value *array_obj = NULL; + + array_obj = bt_value_array_create(); + ret = bt_value_map_insert(map_obj, key, array_obj); + bt_put(array_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj, + const char *key) +{ + enum bt_value_status ret; + struct bt_value *empty_map_obj = NULL; + + empty_map_obj = bt_value_map_create(); + ret = bt_value_map_insert(map_obj, key, empty_map_obj); + bt_put(empty_map_obj); + + return ret; +} + +BT_HIDDEN +enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj, + bt_value_map_foreach_cb cb, void *data) +{ + enum bt_value_status ret = BT_VALUE_STATUS_OK; + gpointer key, element_obj; + GHashTableIter iter; + struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); + + if (!map_obj || !bt_value_is_map(map_obj) || !cb) { + ret = BT_VALUE_STATUS_INVAL; + goto end; + } + + g_hash_table_iter_init(&iter, typed_map_obj->ght); + + while (g_hash_table_iter_next(&iter, &key, &element_obj)) { + const char *key_str = g_quark_to_string((unsigned long) key); + + if (!cb(key_str, element_obj, data)) { + ret = BT_VALUE_STATUS_CANCELLED; + break; + } + } + +end: + return ret; +} + +BT_HIDDEN +struct bt_value *bt_value_copy(const struct bt_value *object) +{ + struct bt_value *copy_obj = NULL; + + if (!object) { + goto end; + } + + copy_obj = copy_funcs[object->type](object); + +end: + return copy_obj; +} + +BT_HIDDEN +bool bt_value_compare(const struct bt_value *object_a, + const struct bt_value *object_b) +{ + bool ret = false; + + if (!object_a || !object_b) { + goto end; + } + + if (object_a->type != object_b->type) { + goto end; + } + + ret = compare_funcs[object_a->type](object_a, object_b); + +end: + return ret; +} diff --git a/include/Makefile.am b/include/Makefile.am index fcbd6a3d..b6905cf6 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -5,9 +5,7 @@ babeltraceinclude_HEADERS = \ babeltrace/iterator.h \ babeltrace/trace-handle.h \ babeltrace/list.h \ - babeltrace/clock-types.h \ - babeltrace/values.h \ - babeltrace/ref.h + babeltrace/clock-types.h babeltracectfinclude_HEADERS = \ babeltrace/ctf/events.h \ @@ -92,4 +90,6 @@ noinst_HEADERS = \ babeltrace/compat/stdio.h \ babeltrace/compat/mman.h \ babeltrace/endian.h \ - babeltrace/mmap-align.h + babeltrace/mmap-align.h \ + babeltrace/values.h \ + babeltrace/ref.h diff --git a/include/babeltrace/values.h b/include/babeltrace/values.h index e13cc202..31b2e2a0 100644 --- a/include/babeltrace/values.h +++ b/include/babeltrace/values.h @@ -75,6 +75,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -152,7 +153,8 @@ struct bt_value; * null value object (of type #BT_VALUE_TYPE_NULL), whereas \c NULL * means an error of some sort. */ -extern struct bt_value *bt_value_null; +BT_HIDDEN +struct bt_value *bt_value_null; /** * User function type for bt_value_map_foreach(). @@ -185,7 +187,8 @@ typedef bool (* bt_value_map_foreach_cb)(const char *key, * * @see bt_value_is_frozen() */ -extern enum bt_value_status bt_value_freeze(struct bt_value *object); +BT_HIDDEN +enum bt_value_status bt_value_freeze(struct bt_value *object); /** * Checks whether \p object is frozen or not. @@ -195,7 +198,8 @@ extern enum bt_value_status bt_value_freeze(struct bt_value *object); * * @see bt_value_freeze() */ -extern bool bt_value_is_frozen(const struct bt_value *object); +BT_HIDDEN +bool bt_value_is_frozen(const struct bt_value *object); /** * Returns the type of \p object. @@ -213,7 +217,8 @@ extern bool bt_value_is_frozen(const struct bt_value *object); * @see bt_value_is_array() * @see bt_value_is_map() */ -extern enum bt_value_type bt_value_get_type(const struct bt_value *object); +BT_HIDDEN +enum bt_value_type bt_value_get_type(const struct bt_value *object); /** * Checks whether \p object is a null value object. The only valid null @@ -326,7 +331,8 @@ bool bt_value_is_map(const struct bt_value *object) * @see bt_value_bool_create_init() (creates an initialized * boolean value object) */ -extern struct bt_value *bt_value_bool_create(void); +BT_HIDDEN +struct bt_value *bt_value_bool_create(void); /** * Creates a boolean value object with its initial raw value set to @@ -337,7 +343,8 @@ extern struct bt_value *bt_value_bool_create(void); * @param val Initial raw value * @returns Created value object on success, or \c NULL on error */ -extern struct bt_value *bt_value_bool_create_init(bool val); +BT_HIDDEN +struct bt_value *bt_value_bool_create_init(bool val); /** * Creates an integer value object. The created integer value object's @@ -350,7 +357,8 @@ extern struct bt_value *bt_value_bool_create_init(bool val); * @see bt_value_integer_create_init() (creates an initialized * integer value object) */ -extern struct bt_value *bt_value_integer_create(void); +BT_HIDDEN +struct bt_value *bt_value_integer_create(void); /** * Creates an integer value object with its initial raw value set to @@ -361,7 +369,8 @@ extern struct bt_value *bt_value_integer_create(void); * @param val Initial raw value * @returns Created value object on success, or \c NULL on error */ -extern struct bt_value *bt_value_integer_create_init(int64_t val); +BT_HIDDEN +struct bt_value *bt_value_integer_create_init(int64_t val); /** * Creates a floating point number value object. The created floating @@ -374,7 +383,8 @@ extern struct bt_value *bt_value_integer_create_init(int64_t val); * @see bt_value_float_create_init() (creates an initialized floating * point number value object) */ -extern struct bt_value *bt_value_float_create(void); +BT_HIDDEN +struct bt_value *bt_value_float_create(void); /** * Creates a floating point number value object with its initial raw @@ -385,7 +395,8 @@ extern struct bt_value *bt_value_float_create(void); * @param val Initial raw value * @returns Created value object on success, or \c NULL on error */ -extern struct bt_value *bt_value_float_create_init(double val); +BT_HIDDEN +struct bt_value *bt_value_float_create_init(double val); /** * Creates a string value object. The string value object is initially @@ -398,7 +409,8 @@ extern struct bt_value *bt_value_float_create_init(double val); * @see bt_value_string_create_init() (creates an initialized * string value object) */ -extern struct bt_value *bt_value_string_create(void); +BT_HIDDEN +struct bt_value *bt_value_string_create(void); /** * Creates a string value object with its initial raw value set to @@ -411,7 +423,8 @@ extern struct bt_value *bt_value_string_create(void); * @param val Initial raw value (copied on success) * @returns Created value object on success, or \c NULL on error */ -extern struct bt_value *bt_value_string_create_init(const char *val); +BT_HIDDEN +struct bt_value *bt_value_string_create_init(const char *val); /** * Creates an empty array value object. @@ -420,7 +433,8 @@ extern struct bt_value *bt_value_string_create_init(const char *val); * * @returns Created value object on success, or \c NULL on error */ -extern struct bt_value *bt_value_array_create(void); +BT_HIDDEN +struct bt_value *bt_value_array_create(void); /** * Creates an empty map value object. @@ -429,7 +443,8 @@ extern struct bt_value *bt_value_array_create(void); * * @returns Created value object on success, or \c NULL on error */ -extern struct bt_value *bt_value_map_create(void); +BT_HIDDEN +struct bt_value *bt_value_map_create(void); /** * Gets the boolean raw value of the boolean value object \p bool_obj. @@ -440,7 +455,8 @@ extern struct bt_value *bt_value_map_create(void); * * @see bt_value_bool_set() */ -extern enum bt_value_status bt_value_bool_get( +BT_HIDDEN +enum bt_value_status bt_value_bool_get( const struct bt_value *bool_obj, bool *val); /** @@ -453,7 +469,8 @@ extern enum bt_value_status bt_value_bool_get( * * @see bt_value_bool_get() */ -extern enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, +BT_HIDDEN +enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bool val); /** @@ -466,7 +483,8 @@ extern enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, * * @see bt_value_integer_set() */ -extern enum bt_value_status bt_value_integer_get( +BT_HIDDEN +enum bt_value_status bt_value_integer_get( const struct bt_value *integer_obj, int64_t *val); /** @@ -479,7 +497,8 @@ extern enum bt_value_status bt_value_integer_get( * * @see bt_value_integer_get() */ -extern enum bt_value_status bt_value_integer_set( +BT_HIDDEN +enum bt_value_status bt_value_integer_set( struct bt_value *integer_obj, int64_t val); /** @@ -492,7 +511,8 @@ extern enum bt_value_status bt_value_integer_set( * * @see bt_value_float_set() */ -extern enum bt_value_status bt_value_float_get( +BT_HIDDEN +enum bt_value_status bt_value_float_get( const struct bt_value *float_obj, double *val); /** @@ -505,7 +525,8 @@ extern enum bt_value_status bt_value_float_get( * * @see bt_value_float_get() */ -extern enum bt_value_status bt_value_float_set( +BT_HIDDEN +enum bt_value_status bt_value_float_set( struct bt_value *float_obj, double val); /** @@ -520,7 +541,8 @@ extern enum bt_value_status bt_value_float_set( * * @see bt_value_string_set() */ -extern enum bt_value_status bt_value_string_get( +BT_HIDDEN +enum bt_value_status bt_value_string_get( const struct bt_value *string_obj, const char **val); /** @@ -535,7 +557,8 @@ extern enum bt_value_status bt_value_string_get( * * @see bt_value_string_get() */ -extern enum bt_value_status bt_value_string_set(struct bt_value *string_obj, +BT_HIDDEN +enum bt_value_status bt_value_string_set(struct bt_value *string_obj, const char *val); /** @@ -549,7 +572,8 @@ extern enum bt_value_status bt_value_string_set(struct bt_value *string_obj, * * @see bt_value_array_is_empty() */ -extern int bt_value_array_size(const struct bt_value *array_obj); +BT_HIDDEN +int bt_value_array_size(const struct bt_value *array_obj); /** * Returns \c true if the array value object \p array_obj is empty. @@ -559,7 +583,8 @@ extern int bt_value_array_size(const struct bt_value *array_obj); * * @see bt_value_array_size() */ -extern bool bt_value_array_is_empty(const struct bt_value *array_obj); +BT_HIDDEN +bool bt_value_array_is_empty(const struct bt_value *array_obj); /** * Gets the value object of the array value object \p array_obj at the @@ -573,7 +598,8 @@ extern bool bt_value_array_is_empty(const struct bt_value *array_obj); * @returns Value object at index \p index on * success, or \c NULL on error */ -extern struct bt_value *bt_value_array_get(const struct bt_value *array_obj, +BT_HIDDEN +struct bt_value *bt_value_array_get(const struct bt_value *array_obj, size_t index); /** @@ -594,7 +620,8 @@ extern struct bt_value *bt_value_array_get(const struct bt_value *array_obj, * @see bt_value_array_append_empty_array() * @see bt_value_array_append_empty_map() */ -extern enum bt_value_status bt_value_array_append(struct bt_value *array_obj, +BT_HIDDEN +enum bt_value_status bt_value_array_append(struct bt_value *array_obj, struct bt_value *element_obj); /** @@ -610,7 +637,8 @@ extern enum bt_value_status bt_value_array_append(struct bt_value *array_obj, * * @see bt_value_array_append() */ -extern enum bt_value_status bt_value_array_append_bool( +BT_HIDDEN +enum bt_value_status bt_value_array_append_bool( struct bt_value *array_obj, bool val); /** @@ -626,7 +654,8 @@ extern enum bt_value_status bt_value_array_append_bool( * * @see bt_value_array_append() */ -extern enum bt_value_status bt_value_array_append_integer( +BT_HIDDEN +enum bt_value_status bt_value_array_append_integer( struct bt_value *array_obj, int64_t val); /** @@ -643,7 +672,8 @@ extern enum bt_value_status bt_value_array_append_integer( * * @see bt_value_array_append() */ -extern enum bt_value_status bt_value_array_append_float( +BT_HIDDEN +enum bt_value_status bt_value_array_append_float( struct bt_value *array_obj, double val); /** @@ -661,7 +691,8 @@ extern enum bt_value_status bt_value_array_append_float( * * @see bt_value_array_append() */ -extern enum bt_value_status bt_value_array_append_string( +BT_HIDDEN +enum bt_value_status bt_value_array_append_string( struct bt_value *array_obj, const char *val); /** @@ -676,7 +707,8 @@ extern enum bt_value_status bt_value_array_append_string( * * @see bt_value_array_append() */ -extern enum bt_value_status bt_value_array_append_empty_array( +BT_HIDDEN +enum bt_value_status bt_value_array_append_empty_array( struct bt_value *array_obj); /** @@ -691,7 +723,8 @@ extern enum bt_value_status bt_value_array_append_empty_array( * * @see bt_value_array_append() */ -extern enum bt_value_status bt_value_array_append_empty_map( +BT_HIDDEN +enum bt_value_status bt_value_array_append_empty_map( struct bt_value *array_obj); /** @@ -708,7 +741,8 @@ extern enum bt_value_status bt_value_array_append_empty_map( * \p array_obj * @returns One of #bt_value_status values */ -extern enum bt_value_status bt_value_array_set(struct bt_value *array_obj, +BT_HIDDEN +enum bt_value_status bt_value_array_set(struct bt_value *array_obj, size_t index, struct bt_value *element_obj); /** @@ -722,7 +756,8 @@ extern enum bt_value_status bt_value_array_set(struct bt_value *array_obj, * * @see bt_value_map_is_empty() */ -extern int bt_value_map_size(const struct bt_value *map_obj); +BT_HIDDEN +int bt_value_map_size(const struct bt_value *map_obj); /** * Returns \c true if the map value object \p map_obj is empty. @@ -732,7 +767,8 @@ extern int bt_value_map_size(const struct bt_value *map_obj); * * @see bt_value_map_size() */ -extern bool bt_value_map_is_empty(const struct bt_value *map_obj); +BT_HIDDEN +bool bt_value_map_is_empty(const struct bt_value *map_obj); /** * Gets the value object associated with the key \p key within the @@ -746,7 +782,8 @@ extern bool bt_value_map_is_empty(const struct bt_value *map_obj); * @returns Value object associated with the key \p key * on success, or \c NULL on error */ -extern struct bt_value *bt_value_map_get(const struct bt_value *map_obj, +BT_HIDDEN +struct bt_value *bt_value_map_get(const struct bt_value *map_obj, const char *key); /** @@ -771,7 +808,8 @@ extern struct bt_value *bt_value_map_get(const struct bt_value *map_obj, * returned if the loop was cancelled by the user * function */ -extern enum bt_value_status bt_value_map_foreach( +BT_HIDDEN +enum bt_value_status bt_value_map_foreach( const struct bt_value *map_obj, bt_value_map_foreach_cb cb, void *data); @@ -785,7 +823,8 @@ extern enum bt_value_status bt_value_map_foreach( * or \c false if it doesn't have \p key or * on error */ -extern bool bt_value_map_has_key(const struct bt_value *map_obj, +BT_HIDDEN +bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key); /** @@ -812,7 +851,8 @@ extern bool bt_value_map_has_key(const struct bt_value *map_obj, * @see bt_value_map_insert_empty_array() * @see bt_value_map_insert_empty_map() */ -extern enum bt_value_status bt_value_map_insert( +BT_HIDDEN +enum bt_value_status bt_value_map_insert( struct bt_value *map_obj, const char *key, struct bt_value *element_obj); @@ -835,7 +875,8 @@ extern enum bt_value_status bt_value_map_insert( * * @see bt_value_map_insert() */ -extern enum bt_value_status bt_value_map_insert_bool( +BT_HIDDEN +enum bt_value_status bt_value_map_insert_bool( struct bt_value *map_obj, const char *key, bool val); /** @@ -856,7 +897,8 @@ extern enum bt_value_status bt_value_map_insert_bool( * * @see bt_value_map_insert() */ -extern enum bt_value_status bt_value_map_insert_integer( +BT_HIDDEN +enum bt_value_status bt_value_map_insert_integer( struct bt_value *map_obj, const char *key, int64_t val); /** @@ -879,7 +921,8 @@ extern enum bt_value_status bt_value_map_insert_integer( * * @see bt_value_map_insert() */ -extern enum bt_value_status bt_value_map_insert_float( +BT_HIDDEN +enum bt_value_status bt_value_map_insert_float( struct bt_value *map_obj, const char *key, double val); /** @@ -900,7 +943,8 @@ extern enum bt_value_status bt_value_map_insert_float( * * @see bt_value_map_insert() */ -extern enum bt_value_status bt_value_map_insert_string( +BT_HIDDEN +enum bt_value_status bt_value_map_insert_string( struct bt_value *map_obj, const char *key, const char *val); /** @@ -919,7 +963,8 @@ extern enum bt_value_status bt_value_map_insert_string( * * @see bt_value_map_insert() */ -extern enum bt_value_status bt_value_map_insert_empty_array( +BT_HIDDEN +enum bt_value_status bt_value_map_insert_empty_array( struct bt_value *map_obj, const char *key); /** @@ -938,7 +983,8 @@ extern enum bt_value_status bt_value_map_insert_empty_array( * * @see bt_value_map_insert() */ -extern enum bt_value_status bt_value_map_insert_empty_map( +BT_HIDDEN +enum bt_value_status bt_value_map_insert_empty_map( struct bt_value *map_obj, const char *key); /** @@ -954,7 +1000,8 @@ extern enum bt_value_status bt_value_map_insert_empty_map( * @returns Deep copy of \p object on success, or \c NULL * on error */ -extern struct bt_value *bt_value_copy(const struct bt_value *object); +BT_HIDDEN +struct bt_value *bt_value_copy(const struct bt_value *object); /** * Compares the value objects \p object_a and \p object_b and returns @@ -966,7 +1013,8 @@ extern struct bt_value *bt_value_copy(const struct bt_value *object); * same content, or \c false if they differ or on * error */ -extern bool bt_value_compare(const struct bt_value *object_a, +BT_HIDDEN +bool bt_value_compare(const struct bt_value *object_a, const struct bt_value *object_b); #ifdef __cplusplus diff --git a/lib/Makefile.am b/lib/Makefile.am index 8096d789..007abb78 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -9,9 +9,7 @@ libbabeltrace_la_SOURCES = babeltrace.c \ context.c \ trace-handle.c \ trace-collection.c \ - registry.c \ - values.c \ - ref.c + registry.c libbabeltrace_la_LDFLAGS = -version-info $(BABELTRACE_LIBRARY_VERSION) diff --git a/lib/ref.c b/lib/ref.c deleted file mode 100644 index ac058013..00000000 --- a/lib/ref.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * ref.c: reference counting - * - * Babeltrace Library - * - * Copyright (c) 2015 Jérémie Galarneau - * - * 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 - -void *bt_get(void *ptr) -{ - struct bt_object *obj = ptr; - - if (!obj) { - goto end; - } - - if (obj->parent && bt_object_get_ref_count(obj) == 0) { - bt_get(obj->parent); - } - bt_ref_get(&obj->ref_count); -end: - return obj; -} - -void bt_put(void *ptr) -{ - struct bt_object *obj = ptr; - - if (!obj) { - return; - } - - bt_ref_put(&obj->ref_count); -} diff --git a/lib/values.c b/lib/values.c deleted file mode 100644 index 5e7f55c6..00000000 --- a/lib/values.c +++ /dev/null @@ -1,1202 +0,0 @@ -/* - * Values.c: value objects - * - * Babeltrace Library - * - * 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 -#include -#include -#include -#include -#include -#include -#include - -#define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete)) -#define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base)) -#define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base)) -#define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base)) -#define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base)) -#define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base)) -#define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base)) - -struct bt_value { - struct bt_object base; - enum bt_value_type type; - bool is_frozen; -}; - -static -struct bt_value bt_value_null_instance = { - .type = BT_VALUE_TYPE_NULL, - .is_frozen = true, -}; - -struct bt_value *bt_value_null = &bt_value_null_instance; - -struct bt_value_bool { - struct bt_value base; - bool value; -}; - -struct bt_value_integer { - struct bt_value base; - int64_t value; -}; - -struct bt_value_float { - struct bt_value base; - double value; -}; - -struct bt_value_string { - struct bt_value base; - GString *gstr; -}; - -struct bt_value_array { - struct bt_value base; - GPtrArray *garray; -}; - -struct bt_value_map { - struct bt_value base; - GHashTable *ght; -}; - -static -void bt_value_destroy(struct bt_object *obj); - -static -void bt_value_string_destroy(struct bt_value *object) -{ - g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE); -} - -static -void bt_value_array_destroy(struct bt_value *object) -{ - /* - * Pointer array's registered value destructor will take care - * of putting each contained object. - */ - g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE); -} - -static -void bt_value_map_destroy(struct bt_value *object) -{ - /* - * Hash table's registered value destructor will take care of - * putting each contained object. Keys are GQuarks and cannot - * be destroyed anyway. - */ - g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght); -} - -static -void (* const destroy_funcs[])(struct bt_value *) = { - [BT_VALUE_TYPE_NULL] = NULL, - [BT_VALUE_TYPE_BOOL] = NULL, - [BT_VALUE_TYPE_INTEGER] = NULL, - [BT_VALUE_TYPE_FLOAT] = NULL, - [BT_VALUE_TYPE_STRING] = bt_value_string_destroy, - [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy, - [BT_VALUE_TYPE_MAP] = bt_value_map_destroy, -}; - -static -struct bt_value *bt_value_null_copy(const struct bt_value *null_obj) -{ - return bt_value_null; -} - -static -struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj) -{ - return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj)->value); -} - -static -struct bt_value *bt_value_integer_copy(const struct bt_value *integer_obj) -{ - return bt_value_integer_create_init( - BT_VALUE_TO_INTEGER(integer_obj)->value); -} - -static -struct bt_value *bt_value_float_copy(const struct bt_value *float_obj) -{ - return bt_value_float_create_init( - BT_VALUE_TO_FLOAT(float_obj)->value); -} - -static -struct bt_value *bt_value_string_copy(const struct bt_value *string_obj) -{ - return bt_value_string_create_init( - BT_VALUE_TO_STRING(string_obj)->gstr->str); -} - -static -struct bt_value *bt_value_array_copy(const struct bt_value *array_obj) -{ - int i; - int ret; - struct bt_value *copy_obj; - struct bt_value_array *typed_array_obj; - - typed_array_obj = BT_VALUE_TO_ARRAY(array_obj); - copy_obj = bt_value_array_create(); - - if (!copy_obj) { - goto end; - } - - for (i = 0; i < typed_array_obj->garray->len; ++i) { - struct bt_value *element_obj_copy; - struct bt_value *element_obj = bt_value_array_get(array_obj, i); - - if (!element_obj) { - BT_PUT(copy_obj); - goto end; - } - - element_obj_copy = bt_value_copy(element_obj); - BT_PUT(element_obj); - - if (!element_obj_copy) { - BT_PUT(copy_obj); - goto end; - } - - ret = bt_value_array_append(copy_obj, element_obj_copy); - BT_PUT(element_obj_copy); - - if (ret) { - BT_PUT(copy_obj); - goto end; - } - } - -end: - return copy_obj; -} - -static -struct bt_value *bt_value_map_copy(const struct bt_value *map_obj) -{ - int ret; - GHashTableIter iter; - gpointer key, element_obj; - struct bt_value *copy_obj; - struct bt_value *element_obj_copy; - struct bt_value_map *typed_map_obj; - - typed_map_obj = BT_VALUE_TO_MAP(map_obj); - copy_obj = bt_value_map_create(); - - if (!copy_obj) { - goto end; - } - - g_hash_table_iter_init(&iter, typed_map_obj->ght); - - while (g_hash_table_iter_next(&iter, &key, &element_obj)) { - const char *key_str = g_quark_to_string((unsigned long) key); - - element_obj_copy = bt_value_copy(element_obj); - - if (!element_obj_copy) { - BT_PUT(copy_obj); - goto end; - } - - ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy); - BT_PUT(element_obj_copy); - - if (ret) { - BT_PUT(copy_obj); - goto end; - } - } - -end: - return copy_obj; -} - -static -struct bt_value *(* const copy_funcs[])(const struct bt_value *) = { - [BT_VALUE_TYPE_NULL] = bt_value_null_copy, - [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy, - [BT_VALUE_TYPE_INTEGER] = bt_value_integer_copy, - [BT_VALUE_TYPE_FLOAT] = bt_value_float_copy, - [BT_VALUE_TYPE_STRING] = bt_value_string_copy, - [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy, - [BT_VALUE_TYPE_MAP] = bt_value_map_copy, -}; - -static -bool bt_value_null_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - /* - * Always true since bt_value_compare() already checks if both - * object_a and object_b have the same type, and in the case of - * null value objects, they're always the same if it is so. - */ - return true; -} - -static -bool bt_value_bool_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - return BT_VALUE_TO_BOOL(object_a)->value == - BT_VALUE_TO_BOOL(object_b)->value; -} - -static -bool bt_value_integer_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - return BT_VALUE_TO_INTEGER(object_a)->value == - BT_VALUE_TO_INTEGER(object_b)->value; -} - -static -bool bt_value_float_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - return BT_VALUE_TO_FLOAT(object_a)->value == - BT_VALUE_TO_FLOAT(object_b)->value; -} - -static -bool bt_value_string_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - return !strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str, - BT_VALUE_TO_STRING(object_b)->gstr->str); -} - -static -bool bt_value_array_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - int i; - bool ret = true; - const struct bt_value_array *array_obj_a = - BT_VALUE_TO_ARRAY(object_a); - - if (bt_value_array_size(object_a) != bt_value_array_size(object_b)) { - ret = false; - goto end; - } - - for (i = 0; i < array_obj_a->garray->len; ++i) { - struct bt_value *element_obj_a; - struct bt_value *element_obj_b; - - element_obj_a = bt_value_array_get(object_a, i); - element_obj_b = bt_value_array_get(object_b, i); - - if (!bt_value_compare(element_obj_a, element_obj_b)) { - BT_PUT(element_obj_a); - BT_PUT(element_obj_b); - ret = false; - goto end; - } - - BT_PUT(element_obj_a); - BT_PUT(element_obj_b); - } - -end: - return ret; -} - -static -bool bt_value_map_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - bool ret = true; - GHashTableIter iter; - gpointer key, element_obj_a; - const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a); - - if (bt_value_map_size(object_a) != bt_value_map_size(object_b)) { - ret = false; - goto end; - } - - g_hash_table_iter_init(&iter, map_obj_a->ght); - - while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) { - struct bt_value *element_obj_b; - const char *key_str = g_quark_to_string((unsigned long) key); - - element_obj_b = bt_value_map_get(object_b, key_str); - - if (!bt_value_compare(element_obj_a, element_obj_b)) { - BT_PUT(element_obj_b); - ret = false; - goto end; - } - - BT_PUT(element_obj_b); - } - -end: - return ret; -} - -static -bool (* const compare_funcs[])(const struct bt_value *, - const struct bt_value *) = { - [BT_VALUE_TYPE_NULL] = bt_value_null_compare, - [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare, - [BT_VALUE_TYPE_INTEGER] = bt_value_integer_compare, - [BT_VALUE_TYPE_FLOAT] = bt_value_float_compare, - [BT_VALUE_TYPE_STRING] = bt_value_string_compare, - [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare, - [BT_VALUE_TYPE_MAP] = bt_value_map_compare, -}; - -void bt_value_null_freeze(struct bt_value *object) -{ -} - -void bt_value_generic_freeze(struct bt_value *object) -{ - object->is_frozen = true; -} - -void bt_value_array_freeze(struct bt_value *object) -{ - int i; - struct bt_value_array *typed_array_obj = - BT_VALUE_TO_ARRAY(object); - - for (i = 0; i < typed_array_obj->garray->len; ++i) { - struct bt_value *element_obj = - g_ptr_array_index(typed_array_obj->garray, i); - - bt_value_freeze(element_obj); - } - - bt_value_generic_freeze(object); -} - -void bt_value_map_freeze(struct bt_value *object) -{ - GHashTableIter iter; - gpointer key, element_obj; - const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object); - - g_hash_table_iter_init(&iter, map_obj->ght); - - while (g_hash_table_iter_next(&iter, &key, &element_obj)) { - bt_value_freeze(element_obj); - } - - bt_value_generic_freeze(object); -} - -static -void (* const freeze_funcs[])(struct bt_value *) = { - [BT_VALUE_TYPE_NULL] = bt_value_null_freeze, - [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze, - [BT_VALUE_TYPE_INTEGER] = bt_value_generic_freeze, - [BT_VALUE_TYPE_FLOAT] = bt_value_generic_freeze, - [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze, - [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze, - [BT_VALUE_TYPE_MAP] = bt_value_map_freeze, -}; - -static -void bt_value_destroy(struct bt_object *obj) -{ - struct bt_value *value; - - value = container_of(obj, struct bt_value, base); - assert(value->type != BT_VALUE_TYPE_UNKNOWN); - - if (bt_value_is_null(value)) { - return; - } - - if (destroy_funcs[value->type]) { - destroy_funcs[value->type](value); - } - - g_free(value); -} - -enum bt_value_status bt_value_freeze(struct bt_value *object) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - - if (!object) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - freeze_funcs[object->type](object); - -end: - return ret; -} - -bool bt_value_is_frozen(const struct bt_value *object) -{ - return object && object->is_frozen; -} - -enum bt_value_type bt_value_get_type(const struct bt_value *object) -{ - if (!object) { - return BT_VALUE_TYPE_UNKNOWN; - } - - return object->type; -} - -static -struct bt_value bt_value_create_base(enum bt_value_type type) -{ - struct bt_value base; - - base.type = type; - base.is_frozen = false; - bt_object_init(&base, bt_value_destroy); - - return base; -} - -struct bt_value *bt_value_bool_create_init(bool val) -{ - struct bt_value_bool *bool_obj; - - bool_obj = g_new0(struct bt_value_bool, 1); - - if (!bool_obj) { - goto end; - } - - bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL); - bool_obj->value = val; - -end: - return BT_VALUE_FROM_CONCRETE(bool_obj); -} - -struct bt_value *bt_value_bool_create(void) -{ - return bt_value_bool_create_init(false); -} - -struct bt_value *bt_value_integer_create_init(int64_t val) -{ - struct bt_value_integer *integer_obj; - - integer_obj = g_new0(struct bt_value_integer, 1); - - if (!integer_obj) { - goto end; - } - - integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER); - integer_obj->value = val; - -end: - return BT_VALUE_FROM_CONCRETE(integer_obj); -} - -struct bt_value *bt_value_integer_create(void) -{ - return bt_value_integer_create_init(0); -} - -struct bt_value *bt_value_float_create_init(double val) -{ - struct bt_value_float *float_obj; - - float_obj = g_new0(struct bt_value_float, 1); - - if (!float_obj) { - goto end; - } - - float_obj->base = bt_value_create_base(BT_VALUE_TYPE_FLOAT); - float_obj->value = val; - -end: - return BT_VALUE_FROM_CONCRETE(float_obj); -} - -struct bt_value *bt_value_float_create(void) -{ - return bt_value_float_create_init(0.); -} - -struct bt_value *bt_value_string_create_init(const char *val) -{ - struct bt_value_string *string_obj = NULL; - - if (!val) { - goto end; - } - - string_obj = g_new0(struct bt_value_string, 1); - - if (!string_obj) { - goto end; - } - - string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING); - string_obj->gstr = g_string_new(val); - - if (!string_obj->gstr) { - g_free(string_obj); - string_obj = NULL; - goto end; - } - -end: - return BT_VALUE_FROM_CONCRETE(string_obj); -} - -struct bt_value *bt_value_string_create(void) -{ - return bt_value_string_create_init(""); -} - -struct bt_value *bt_value_array_create(void) -{ - struct bt_value_array *array_obj; - - array_obj = g_new0(struct bt_value_array, 1); - - if (!array_obj) { - goto end; - } - - array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY); - array_obj->garray = babeltrace_g_ptr_array_new_full(0, - (GDestroyNotify) bt_put); - - if (!array_obj->garray) { - g_free(array_obj); - array_obj = NULL; - goto end; - } - -end: - return BT_VALUE_FROM_CONCRETE(array_obj); -} - -struct bt_value *bt_value_map_create(void) -{ - struct bt_value_map *map_obj; - - map_obj = g_new0(struct bt_value_map, 1); - - if (!map_obj) { - goto end; - } - - map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP); - map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal, - NULL, (GDestroyNotify) bt_put); - - if (!map_obj->ght) { - g_free(map_obj); - map_obj = NULL; - goto end; - } - -end: - return BT_VALUE_FROM_CONCRETE(map_obj); -} - -enum bt_value_status bt_value_bool_get(const struct bt_value *bool_obj, - bool *val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj); - - if (!bool_obj || !bt_value_is_bool(bool_obj) || !val) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - *val = typed_bool_obj->value; - -end: - return ret; -} - -enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bool val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj); - - if (!bool_obj || !bt_value_is_bool(bool_obj)) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - if (bool_obj->is_frozen) { - ret = BT_VALUE_STATUS_FROZEN; - goto end; - } - - typed_bool_obj->value = val; - -end: - return ret; -} - -enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj, - int64_t *val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_integer *typed_integer_obj = - BT_VALUE_TO_INTEGER(integer_obj); - - if (!integer_obj || !bt_value_is_integer(integer_obj) || !val) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - *val = typed_integer_obj->value; - -end: - return ret; -} - -enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj, - int64_t val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_integer *typed_integer_obj = - BT_VALUE_TO_INTEGER(integer_obj); - - if (!integer_obj || !bt_value_is_integer(integer_obj)) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - if (integer_obj->is_frozen) { - ret = BT_VALUE_STATUS_FROZEN; - goto end; - } - - typed_integer_obj->value = val; - -end: - return ret; -} - -enum bt_value_status bt_value_float_get(const struct bt_value *float_obj, - double *val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_float *typed_float_obj = - BT_VALUE_TO_FLOAT(float_obj); - - if (!float_obj || !bt_value_is_float(float_obj) || !val) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - *val = typed_float_obj->value; - -end: - return ret; -} - -enum bt_value_status bt_value_float_set(struct bt_value *float_obj, - double val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_float *typed_float_obj = - BT_VALUE_TO_FLOAT(float_obj); - - if (!float_obj || !bt_value_is_float(float_obj)) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - if (float_obj->is_frozen) { - ret = BT_VALUE_STATUS_FROZEN; - goto end; - } - - typed_float_obj->value = val; - -end: - return ret; -} - -enum bt_value_status bt_value_string_get(const struct bt_value *string_obj, - const char **val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_string *typed_string_obj = - BT_VALUE_TO_STRING(string_obj); - - if (!string_obj || !bt_value_is_string(string_obj) || !val) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - *val = typed_string_obj->gstr->str; - -end: - return ret; -} - -enum bt_value_status bt_value_string_set(struct bt_value *string_obj, - const char *val) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_string *typed_string_obj = - BT_VALUE_TO_STRING(string_obj); - - if (!string_obj || !bt_value_is_string(string_obj) || !val) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - if (string_obj->is_frozen) { - ret = BT_VALUE_STATUS_FROZEN; - goto end; - } - - g_string_assign(typed_string_obj->gstr, val); - -end: - return ret; -} - -int bt_value_array_size(const struct bt_value *array_obj) -{ - int ret; - struct bt_value_array *typed_array_obj = - BT_VALUE_TO_ARRAY(array_obj); - - if (!array_obj || !bt_value_is_array(array_obj)) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - ret = (int) typed_array_obj->garray->len; - -end: - return ret; -} - -bool bt_value_array_is_empty(const struct bt_value *array_obj) -{ - return bt_value_array_size(array_obj) == 0; -} - -struct bt_value *bt_value_array_get(const struct bt_value *array_obj, - size_t index) -{ - struct bt_value *ret; - struct bt_value_array *typed_array_obj = - BT_VALUE_TO_ARRAY(array_obj); - - if (!array_obj || !bt_value_is_array(array_obj) || - index >= typed_array_obj->garray->len) { - ret = NULL; - goto end; - } - - ret = g_ptr_array_index(typed_array_obj->garray, index); - bt_get(ret); - -end: - return ret; -} - -enum bt_value_status bt_value_array_append(struct bt_value *array_obj, - struct bt_value *element_obj) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_array *typed_array_obj = - BT_VALUE_TO_ARRAY(array_obj); - - if (!array_obj || !bt_value_is_array(array_obj) || !element_obj) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - if (array_obj->is_frozen) { - ret = BT_VALUE_STATUS_FROZEN; - goto end; - } - - g_ptr_array_add(typed_array_obj->garray, element_obj); - bt_get(element_obj); - -end: - return ret; -} - -enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj, - bool val) -{ - enum bt_value_status ret; - struct bt_value *bool_obj = NULL; - - bool_obj = bt_value_bool_create_init(val); - ret = bt_value_array_append(array_obj, bool_obj); - bt_put(bool_obj); - - return ret; -} - -enum bt_value_status bt_value_array_append_integer( - struct bt_value *array_obj, int64_t val) -{ - enum bt_value_status ret; - struct bt_value *integer_obj = NULL; - - integer_obj = bt_value_integer_create_init(val); - ret = bt_value_array_append(array_obj, integer_obj); - bt_put(integer_obj); - - return ret; -} - -enum bt_value_status bt_value_array_append_float(struct bt_value *array_obj, - double val) -{ - enum bt_value_status ret; - struct bt_value *float_obj = NULL; - - float_obj = bt_value_float_create_init(val); - ret = bt_value_array_append(array_obj, float_obj); - bt_put(float_obj); - - return ret; -} - -enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj, - const char *val) -{ - enum bt_value_status ret; - struct bt_value *string_obj = NULL; - - string_obj = bt_value_string_create_init(val); - ret = bt_value_array_append(array_obj, string_obj); - bt_put(string_obj); - - return ret; -} - -enum bt_value_status bt_value_array_append_empty_array( - struct bt_value *array_obj) -{ - enum bt_value_status ret; - struct bt_value *empty_array_obj = NULL; - - empty_array_obj = bt_value_array_create(); - ret = bt_value_array_append(array_obj, empty_array_obj); - bt_put(empty_array_obj); - - return ret; -} - -enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj) -{ - enum bt_value_status ret; - struct bt_value *map_obj = NULL; - - map_obj = bt_value_map_create(); - ret = bt_value_array_append(array_obj, map_obj); - bt_put(map_obj); - - return ret; -} - -enum bt_value_status bt_value_array_set(struct bt_value *array_obj, - size_t index, struct bt_value *element_obj) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_array *typed_array_obj = - BT_VALUE_TO_ARRAY(array_obj); - - if (!array_obj || !bt_value_is_array(array_obj) || !element_obj || - index >= typed_array_obj->garray->len) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - if (array_obj->is_frozen) { - ret = BT_VALUE_STATUS_FROZEN; - goto end; - } - - bt_put(g_ptr_array_index(typed_array_obj->garray, index)); - g_ptr_array_index(typed_array_obj->garray, index) = element_obj; - bt_get(element_obj); - -end: - return ret; -} - -int bt_value_map_size(const struct bt_value *map_obj) -{ - int ret; - struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); - - if (!map_obj || !bt_value_is_map(map_obj)) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - ret = (int) g_hash_table_size(typed_map_obj->ght); - -end: - return ret; -} - -bool bt_value_map_is_empty(const struct bt_value *map_obj) -{ - return bt_value_map_size(map_obj) == 0; -} - -struct bt_value *bt_value_map_get(const struct bt_value *map_obj, - const char *key) -{ - GQuark quark; - struct bt_value *ret; - struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); - - if (!map_obj || !bt_value_is_map(map_obj) || !key) { - ret = NULL; - goto end; - } - - quark = g_quark_from_string(key); - ret = g_hash_table_lookup(typed_map_obj->ght, GUINT_TO_POINTER(quark)); - - if (ret) { - bt_get(ret); - } - -end: - return ret; -} - -bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key) -{ - bool ret; - GQuark quark; - struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); - - if (!map_obj || !bt_value_is_map(map_obj) || !key) { - ret = false; - goto end; - } - - quark = g_quark_from_string(key); - ret = babeltrace_g_hash_table_contains(typed_map_obj->ght, - GUINT_TO_POINTER(quark)); - -end: - return ret; -} - -enum bt_value_status bt_value_map_insert(struct bt_value *map_obj, - const char *key, struct bt_value *element_obj) -{ - GQuark quark; - enum bt_value_status ret = BT_VALUE_STATUS_OK; - struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); - - if (!map_obj || !bt_value_is_map(map_obj) || !key || !element_obj) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - if (map_obj->is_frozen) { - ret = BT_VALUE_STATUS_FROZEN; - goto end; - } - - quark = g_quark_from_string(key); - g_hash_table_insert(typed_map_obj->ght, - GUINT_TO_POINTER(quark), element_obj); - bt_get(element_obj); - -end: - return ret; -} - -enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj, - const char *key, bool val) -{ - enum bt_value_status ret; - struct bt_value *bool_obj = NULL; - - bool_obj = bt_value_bool_create_init(val); - ret = bt_value_map_insert(map_obj, key, bool_obj); - bt_put(bool_obj); - - return ret; -} - -enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj, - const char *key, int64_t val) -{ - enum bt_value_status ret; - struct bt_value *integer_obj = NULL; - - integer_obj = bt_value_integer_create_init(val); - ret = bt_value_map_insert(map_obj, key, integer_obj); - bt_put(integer_obj); - - return ret; -} - -enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj, - const char *key, double val) -{ - enum bt_value_status ret; - struct bt_value *float_obj = NULL; - - float_obj = bt_value_float_create_init(val); - ret = bt_value_map_insert(map_obj, key, float_obj); - bt_put(float_obj); - - return ret; -} - -enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj, - const char *key, const char *val) -{ - enum bt_value_status ret; - struct bt_value *string_obj = NULL; - - string_obj = bt_value_string_create_init(val); - ret = bt_value_map_insert(map_obj, key, string_obj); - bt_put(string_obj); - - return ret; -} - -enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj, - const char *key) -{ - enum bt_value_status ret; - struct bt_value *array_obj = NULL; - - array_obj = bt_value_array_create(); - ret = bt_value_map_insert(map_obj, key, array_obj); - bt_put(array_obj); - - return ret; -} - -enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj, - const char *key) -{ - enum bt_value_status ret; - struct bt_value *empty_map_obj = NULL; - - empty_map_obj = bt_value_map_create(); - ret = bt_value_map_insert(map_obj, key, empty_map_obj); - bt_put(empty_map_obj); - - return ret; -} - -enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj, - bt_value_map_foreach_cb cb, void *data) -{ - enum bt_value_status ret = BT_VALUE_STATUS_OK; - gpointer key, element_obj; - GHashTableIter iter; - struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); - - if (!map_obj || !bt_value_is_map(map_obj) || !cb) { - ret = BT_VALUE_STATUS_INVAL; - goto end; - } - - g_hash_table_iter_init(&iter, typed_map_obj->ght); - - while (g_hash_table_iter_next(&iter, &key, &element_obj)) { - const char *key_str = g_quark_to_string((unsigned long) key); - - if (!cb(key_str, element_obj, data)) { - ret = BT_VALUE_STATUS_CANCELLED; - break; - } - } - -end: - return ret; -} - -struct bt_value *bt_value_copy(const struct bt_value *object) -{ - struct bt_value *copy_obj = NULL; - - if (!object) { - goto end; - } - - copy_obj = copy_funcs[object->type](object); - -end: - return copy_obj; -} - -bool bt_value_compare(const struct bt_value *object_a, - const struct bt_value *object_b) -{ - bool ret = false; - - if (!object_a || !object_b) { - goto end; - } - - if (object_a->type != object_b->type) { - goto end; - } - - ret = compare_funcs[object_a->type](object_a, object_b); - -end: - return ret; -}