X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fvalue.c;h=6ec7497dac3b83f6fa28343dd6f5959852e5e438;hb=3fadfbc0c91f82c46bd36e6e0657ea93570c9db1;hp=e4a0d1e4dc6420eaf7ce670c2dc28b5254092f33;hpb=c6bd8523ba4a37b61a1591c03e23614112b155ba;p=babeltrace.git diff --git a/lib/value.c b/lib/value.c index e4a0d1e4..6ec7497d 100644 --- a/lib/value.c +++ b/lib/value.c @@ -1,6 +1,5 @@ /* - * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation - * Copyright (c) 2015 Philippe Proulx + * Copyright (c) 2015-2018 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 @@ -22,25 +21,22 @@ */ #define BT_LOG_TAG "VALUES" -#include +#include #include #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete)) +#include +#include +#include +#include +#include +#include +#include +#include +#include + #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_REAL(_base) ((struct bt_value_real *) (_base)) @@ -63,12 +59,6 @@ "Index is out of bound: " \ "index=%" PRIu64 ", count=%u", (_index), (_count)); -struct bt_value { - struct bt_object base; - enum bt_value_type type; - bt_bool frozen; -}; - static void bt_value_null_instance_release_func(struct bt_object *obj) { @@ -89,37 +79,7 @@ struct bt_value bt_value_null_instance = { .frozen = BT_TRUE, }; -struct bt_value *bt_value_null = &bt_value_null_instance; - -struct bt_value_bool { - struct bt_value base; - bt_bool value; -}; - -struct bt_value_integer { - struct bt_value base; - int64_t value; -}; - -struct bt_value_real { - 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; -}; +struct bt_value *const bt_value_null = &bt_value_null_instance; static void bt_value_destroy(struct bt_object *obj); @@ -156,13 +116,14 @@ void bt_value_map_destroy(struct bt_value *object) 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_REAL] = 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, + [BT_VALUE_TYPE_NULL] = NULL, + [BT_VALUE_TYPE_BOOL] = NULL, + [BT_VALUE_TYPE_UNSIGNED_INTEGER] = NULL, + [BT_VALUE_TYPE_SIGNED_INTEGER] = NULL, + [BT_VALUE_TYPE_REAL] = 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 @@ -178,12 +139,16 @@ struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj) BT_VALUE_TO_BOOL(bool_obj)->value); } +static inline +struct bt_value *bt_value_integer_create_init(enum bt_value_type type, + uint64_t uval); + 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); + return bt_value_integer_create_init(integer_obj->type, + BT_VALUE_TO_INTEGER(integer_obj)->value.u); } static @@ -307,13 +272,14 @@ end: 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_REAL] = bt_value_real_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, + [BT_VALUE_TYPE_NULL] = bt_value_null_copy, + [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy, + [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_copy, + [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_copy, + [BT_VALUE_TYPE_REAL] = bt_value_real_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 @@ -348,12 +314,20 @@ static bt_bool bt_value_integer_compare(const struct bt_value *object_a, const struct bt_value *object_b) { - if (BT_VALUE_TO_INTEGER(object_a)->value != - BT_VALUE_TO_INTEGER(object_b)->value) { - BT_LOGV("Integer value objects are different: " - "int-a-val=%" PRId64 ", int-b-val=%" PRId64, - BT_VALUE_TO_INTEGER(object_a)->value, - BT_VALUE_TO_INTEGER(object_b)->value); + if (BT_VALUE_TO_INTEGER(object_a)->value.u != + BT_VALUE_TO_INTEGER(object_b)->value.u) { + if (object_a->type == BT_VALUE_TYPE_UNSIGNED_INTEGER) { + BT_LOGV("Unsigned integer value objects are different: " + "int-a-val=%" PRIu64 ", int-b-val=%" PRIu64, + BT_VALUE_TO_INTEGER(object_a)->value.u, + BT_VALUE_TO_INTEGER(object_b)->value.u); + } else { + BT_LOGV("Signed integer value objects are different: " + "int-a-val=%" PRId64 ", int-b-val=%" PRId64, + BT_VALUE_TO_INTEGER(object_a)->value.i, + BT_VALUE_TO_INTEGER(object_b)->value.i); + } + return BT_FALSE; } @@ -481,13 +455,14 @@ end: static bt_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_REAL] = bt_value_real_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_VALUE_TYPE_NULL] = bt_value_null_compare, + [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare, + [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_compare, + [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_compare, + [BT_VALUE_TYPE_REAL] = bt_value_real_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, }; static @@ -533,13 +508,14 @@ void bt_value_map_freeze(struct bt_value *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_REAL] = 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, + [BT_VALUE_TYPE_NULL] = bt_value_null_freeze, + [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze, + [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_generic_freeze, + [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_generic_freeze, + [BT_VALUE_TYPE_REAL] = 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 @@ -614,7 +590,7 @@ struct bt_value *bt_value_bool_create_init(bt_bool val) BT_LOGD("Created boolean value object: addr=%p", bool_obj); end: - return (void *) BT_VALUE_FROM_CONCRETE(bool_obj); + return (void *) bool_obj; } struct bt_value *bt_value_bool_create(void) @@ -622,29 +598,59 @@ struct bt_value *bt_value_bool_create(void) return bt_value_bool_create_init(BT_FALSE); } -struct bt_value *bt_value_integer_create_init(int64_t val) +static inline +struct bt_value *bt_value_integer_create_init(enum bt_value_type type, + uint64_t uval) { struct bt_value_integer *integer_obj; - BT_LOGD("Creating integer value object: val=%" PRId64, val); + BT_ASSERT(type == BT_VALUE_TYPE_UNSIGNED_INTEGER || + type == BT_VALUE_TYPE_SIGNED_INTEGER); + + if (type == BT_VALUE_TYPE_UNSIGNED_INTEGER) { + BT_LOGD("Creating unsigned integer value object: val=%" PRIu64, + uval); + } else { + BT_LOGD("Creating signed integer value object: val=%" PRId64, + (int64_t) uval); + } + integer_obj = g_new0(struct bt_value_integer, 1); if (!integer_obj) { BT_LOGE_STR("Failed to allocate one integer value object."); goto end; } - integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER); - integer_obj->value = val; - BT_LOGD("Created integer value object: addr=%p", + integer_obj->base = bt_value_create_base(type); + integer_obj->value.u = uval; + BT_LOGD("Created %ssigned integer value object: addr=%p", + type == BT_VALUE_TYPE_UNSIGNED_INTEGER ? "un" : "", integer_obj); end: - return (void *) BT_VALUE_FROM_CONCRETE(integer_obj); + return (void *) integer_obj; } -struct bt_value *bt_value_integer_create(void) +struct bt_value *bt_value_unsigned_integer_create_init(uint64_t val) { - return bt_value_integer_create_init(0); + return bt_value_integer_create_init(BT_VALUE_TYPE_UNSIGNED_INTEGER, + val); +} + +struct bt_value *bt_value_unsigned_integer_create(void) +{ + return bt_value_unsigned_integer_create_init(0); +} + +struct bt_value *bt_value_signed_integer_create_init(int64_t val) +{ + return bt_value_integer_create_init(BT_VALUE_TYPE_SIGNED_INTEGER, + (uint64_t) val); +} + +struct bt_value *bt_value_signed_integer_create(void) +{ + return bt_value_signed_integer_create_init(0); } struct bt_value *bt_value_real_create_init(double val) @@ -664,7 +670,7 @@ struct bt_value *bt_value_real_create_init(double val) real_obj); end: - return (void *) BT_VALUE_FROM_CONCRETE(real_obj); + return (void *) real_obj; } struct bt_value *bt_value_real_create(void) @@ -701,7 +707,7 @@ struct bt_value *bt_value_string_create_init(const char *val) string_obj); end: - return (void *) BT_VALUE_FROM_CONCRETE(string_obj); + return (void *) string_obj; } struct bt_value *bt_value_string_create(void) @@ -734,7 +740,7 @@ struct bt_value *bt_value_array_create(void) array_obj); end: - return (void *) BT_VALUE_FROM_CONCRETE(array_obj); + return (void *) array_obj; } struct bt_value *bt_value_map_create(void) @@ -762,7 +768,7 @@ struct bt_value *bt_value_map_create(void) map_obj); end: - return (void *) BT_VALUE_FROM_CONCRETE(map_obj); + return (void *) map_obj; } bt_bool bt_value_bool_get(const struct bt_value *bool_obj) @@ -782,22 +788,47 @@ void bt_value_bool_set(struct bt_value *bool_obj, bt_bool val) bool_obj, val); } -int64_t bt_value_integer_get(const struct bt_value *integer_obj) +uint64_t bt_value_unsigned_integer_get(const struct bt_value *integer_obj) +{ + BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); + BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, + BT_VALUE_TYPE_UNSIGNED_INTEGER); + return BT_VALUE_TO_INTEGER(integer_obj)->value.u; +} + +int64_t bt_value_signed_integer_get(const struct bt_value *integer_obj) { BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); - BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER); - return BT_VALUE_TO_INTEGER(integer_obj)->value; + BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, + BT_VALUE_TYPE_SIGNED_INTEGER); + return BT_VALUE_TO_INTEGER(integer_obj)->value.i; } +static inline void bt_value_integer_set(struct bt_value *integer_obj, - int64_t val) + enum bt_value_type expected_type, uint64_t uval) { BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); - BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER); + BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, expected_type); BT_ASSERT_PRE_VALUE_HOT(integer_obj, "Value object"); - BT_VALUE_TO_INTEGER(integer_obj)->value = val; - BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64, - integer_obj, val); + BT_VALUE_TO_INTEGER(integer_obj)->value.u = uval; +} + +void bt_value_unsigned_integer_set(struct bt_value *integer_obj, + uint64_t val) +{ + bt_value_integer_set(integer_obj, BT_VALUE_TYPE_UNSIGNED_INTEGER, val); + BT_LOGV("Set unsigned integer value's raw value: " + "value-addr=%p, value=%" PRIu64, integer_obj, val); +} + +void bt_value_signed_integer_set(struct bt_value *integer_obj, + int64_t val) +{ + bt_value_integer_set(integer_obj, BT_VALUE_TYPE_SIGNED_INTEGER, + (uint64_t) val); + BT_LOGV("Set signed integer value's raw value: " + "value-addr=%p, value=%" PRId64, integer_obj, val); } double bt_value_real_get(const struct bt_value *real_obj) @@ -896,13 +927,26 @@ enum bt_value_status bt_value_array_append_bool_element( return ret; } -enum bt_value_status bt_value_array_append_integer_element( +enum bt_value_status bt_value_array_append_unsigned_integer_element( + struct bt_value *array_obj, uint64_t val) +{ + enum bt_value_status ret; + struct bt_value *integer_obj = NULL; + + integer_obj = bt_value_unsigned_integer_create_init(val); + ret = bt_value_array_append_element(array_obj, + (void *) integer_obj); + bt_object_put_ref(integer_obj); + return ret; +} + +enum bt_value_status bt_value_array_append_signed_integer_element( 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); + integer_obj = bt_value_signed_integer_create_init(val); ret = bt_value_array_append_element(array_obj, (void *) integer_obj); bt_object_put_ref(integer_obj); @@ -1046,13 +1090,26 @@ enum bt_value_status bt_value_map_insert_bool_entry( return ret; } -enum bt_value_status bt_value_map_insert_integer_entry( +enum bt_value_status bt_value_map_insert_unsigned_integer_entry( + struct bt_value *map_obj, const char *key, uint64_t val) +{ + enum bt_value_status ret; + struct bt_value *integer_obj = NULL; + + integer_obj = bt_value_unsigned_integer_create_init(val); + ret = bt_value_map_insert_entry(map_obj, key, + (void *) integer_obj); + bt_object_put_ref(integer_obj); + return ret; +} + +enum bt_value_status bt_value_map_insert_signed_integer_entry( 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); + integer_obj = bt_value_signed_integer_create_init(val); ret = bt_value_map_insert_entry(map_obj, key, (void *) integer_obj); bt_object_put_ref(integer_obj); @@ -1300,3 +1357,13 @@ bt_bool bt_value_compare(const struct bt_value *object_a, end: return ret; } + +void bt_value_get_ref(const struct bt_value *value) +{ + bt_object_get_ref(value); +} + +void bt_value_put_ref(const struct bt_value *value) +{ + bt_object_put_ref(value); +}