#undef __BT_FUNC_STATUS_NOT_FOUND
#undef __BT_FUNC_STATUS_OK
#undef __BT_FUNC_STATUS_OVERFLOW_ERROR
+#undef __BT_FUNC_STATUS_USER_ERROR
#undef __BT_IN_BABELTRACE_H
#undef __BT_UPCAST
#undef __BT_UPCAST_CONST
# define __BT_FUNC_STATUS_MEMORY_ERROR -12
#endif
+/* User function error */
+#ifndef __BT_FUNC_STATUS_USER_ERROR
+# define __BT_FUNC_STATUS_USER_ERROR -2
+#endif
+
/* General error */
#ifndef __BT_FUNC_STATUS_ERROR
# define __BT_FUNC_STATUS_ERROR -1
extern const bt_value *bt_value_map_borrow_entry_value_const(
const bt_value *map_obj, const char *key);
-typedef bt_bool (* bt_value_map_foreach_entry_const_func)(const char *key,
- const bt_value *object, void *data);
+typedef enum bt_value_map_foreach_entry_const_func_status {
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK = __BT_FUNC_STATUS_OK,
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR = __BT_FUNC_STATUS_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_MEMORY_ERROR = __BT_FUNC_STATUS_MEMORY_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_INTERRUPT = __BT_FUNC_STATUS_INTERRUPTED,
+} bt_value_map_foreach_entry_const_func_status;
+
+typedef bt_value_map_foreach_entry_const_func_status
+ (* bt_value_map_foreach_entry_const_func)(const char *key,
+ const bt_value *object, void *data);
typedef enum bt_value_map_foreach_entry_const_status {
BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK = __BT_FUNC_STATUS_OK,
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR = __BT_FUNC_STATUS_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_MEMORY_ERROR = __BT_FUNC_STATUS_MEMORY_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR = __BT_FUNC_STATUS_USER_ERROR,
BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_INTERRUPTED = __BT_FUNC_STATUS_INTERRUPTED,
} bt_value_map_foreach_entry_const_status;
extern bt_value *bt_value_map_borrow_entry_value(
bt_value *map_obj, const char *key);
-typedef bt_bool (* bt_value_map_foreach_entry_func)(const char *key,
- bt_value *object, void *data);
+typedef enum bt_value_map_foreach_entry_func_status {
+ BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK = __BT_FUNC_STATUS_OK,
+ BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR = __BT_FUNC_STATUS_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_MEMORY_ERROR = __BT_FUNC_STATUS_MEMORY_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_INTERRUPT = __BT_FUNC_STATUS_INTERRUPTED,
+} bt_value_map_foreach_entry_func_status;
+
+typedef bt_value_map_foreach_entry_func_status
+ (* bt_value_map_foreach_entry_func)(const char *key,
+ bt_value *object, void *data);
typedef enum bt_value_map_foreach_entry_status {
BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK = __BT_FUNC_STATUS_OK,
+ BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR = __BT_FUNC_STATUS_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_STATUS_MEMORY_ERROR = __BT_FUNC_STATUS_MEMORY_ERROR,
+ BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR = __BT_FUNC_STATUS_USER_ERROR,
BT_VALUE_MAP_FOREACH_ENTRY_STATUS_INTERRUPTED = __BT_FUNC_STATUS_INTERRUPTED,
} bt_value_map_foreach_entry_status;
struct bt_value *keys;
};
-static int bt_value_map_get_keys_cb(const char *key, const struct bt_value *object, void *data)
+static bt_value_map_foreach_entry_const_func_status bt_value_map_get_keys_cb(
+ const char *key, const struct bt_value *object, void *data)
{
- bt_value_array_append_element_status status;
+ int status;
struct bt_value_map_get_keys_data *priv_data = data;
status = bt_value_array_append_string_element(priv_data->keys, key);
- if (status != __BT_FUNC_STATUS_OK) {
- return BT_FALSE;
- }
-
- return BT_TRUE;
+ BT_ASSERT(status == __BT_FUNC_STATUS_OK ||
+ status == __BT_FUNC_STATUS_MEMORY_ERROR);
+ return status;
}
static struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj)
}
static
-bt_bool collect_map_keys(const char *key, const bt_value *object, void *data)
+bt_value_map_foreach_entry_const_func_status collect_map_keys(
+ const char *key, const bt_value *object, void *data)
{
GPtrArray *map_keys = data;
g_ptr_array_add(map_keys, (gpointer *) key);
- return BT_TRUE;
+ return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK;
}
static
return "UNKNOWN_OBJECT";
case __BT_FUNC_STATUS_MEMORY_ERROR:
return "MEMORY_ERROR";
+ case __BT_FUNC_STATUS_USER_ERROR:
+ return "USER_ERROR";
case __BT_FUNC_STATUS_ERROR:
return "ERROR";
case __BT_FUNC_STATUS_OK:
*/
#define BT_FUNC_STATUS_AGAIN __BT_FUNC_STATUS_AGAIN
#define BT_FUNC_STATUS_END __BT_FUNC_STATUS_END
+#define BT_FUNC_STATUS_USER_ERROR __BT_FUNC_STATUS_USER_ERROR
#define BT_FUNC_STATUS_ERROR __BT_FUNC_STATUS_ERROR
#define BT_FUNC_STATUS_INTERRUPTED __BT_FUNC_STATUS_INTERRUPTED
#define BT_FUNC_STATUS_UNKNOWN_OBJECT __BT_FUNC_STATUS_UNKNOWN_OBJECT
#include "common/common.h"
#include "compat/glib.h"
#include "lib/assert-pre.h"
+#include "lib/assert-post.h"
#include "lib/value.h"
#include "common/assert.h"
#include "func-status.h"
struct bt_value *map_obj, bt_value_map_foreach_entry_func func,
void *data)
{
- enum bt_value_map_foreach_entry_status ret = BT_FUNC_STATUS_OK;
+ int status = BT_FUNC_STATUS_OK;
gpointer key, element_obj;
GHashTableIter iter;
struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
- if (!func(key_str, element_obj, data)) {
- BT_LOGT("User interrupted the loop: key=\"%s\", "
- "value-addr=%p, data=%p",
- key_str, element_obj, data);
- ret = BT_FUNC_STATUS_INTERRUPTED;
+ status = func(key_str, element_obj, data);
+ BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
+ if (status != BT_FUNC_STATUS_OK) {
+ if (status < 0) {
+ BT_LIB_LOGE_APPEND_CAUSE(
+ "User function failed while iterating "
+ "map value entries: "
+ "status=%s, key=\"%s\", "
+ "value-addr=%p, data=%p",
+ bt_common_func_status_string(status),
+ key_str, element_obj, data);
+
+ if (status == BT_FUNC_STATUS_ERROR) {
+ /*
+ * User function error becomes a
+ * user error from this
+ * function's caller's
+ * perspective.
+ */
+ status = BT_FUNC_STATUS_USER_ERROR;
+ }
+ } else {
+ BT_ASSERT(status == BT_FUNC_STATUS_INTERRUPTED);
+ BT_LOGT("User interrupted the loop: status=%s, "
+ "key=\"%s\", value-addr=%p, data=%p",
+ bt_common_func_status_string(status),
+ key_str, element_obj, data);
+ }
+
break;
}
}
- return ret;
+ return status;
}
enum bt_value_map_foreach_entry_const_status bt_value_map_foreach_entry_const(
struct extend_map_element_data {
struct bt_value *base_obj;
- int status;
};
static
-bt_bool extend_map_element(const char *key,
- const struct bt_value *extension_obj_elem, void *data)
+bt_value_map_foreach_entry_const_func_status extend_map_element(
+ const char *key, const struct bt_value *extension_obj_elem,
+ void *data)
{
- bt_bool ret = BT_TRUE;
+ int status;
struct extend_map_element_data *extend_data = data;
struct bt_value *extension_obj_elem_copy = NULL;
/* Copy object which is to replace the current one */
- extend_data->status = bt_value_copy(extension_obj_elem,
- &extension_obj_elem_copy);
- if (extend_data->status) {
+ status = bt_value_copy(extension_obj_elem, &extension_obj_elem_copy);
+ if (status) {
BT_LIB_LOGE_APPEND_CAUSE("Cannot copy map element: %!+v",
extension_obj_elem);
goto error;
BT_ASSERT(extension_obj_elem_copy);
/* Replace in base map value. */
- extend_data->status = bt_value_map_insert_entry(
- extend_data->base_obj, key,
+ status = bt_value_map_insert_entry(extend_data->base_obj, key,
(void *) extension_obj_elem_copy);
- if (extend_data->status) {
+ if (status) {
BT_LIB_LOGE_APPEND_CAUSE(
"Cannot replace value in base map value: key=\"%s\", "
"%![base-map-value-]+v, %![element-value-]+v",
- key, extend_data->base_obj,
- extension_obj_elem_copy);
+ key, extend_data->base_obj, extension_obj_elem_copy);
goto error;
}
goto end;
error:
- BT_ASSERT(extend_data->status != BT_FUNC_STATUS_OK);
- ret = BT_FALSE;
+ BT_ASSERT(status < 0);
end:
BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy);
- return ret;
+ BT_ASSERT(status == BT_FUNC_STATUS_OK ||
+ status == BT_FUNC_STATUS_MEMORY_ERROR);
+ return status;
}
enum bt_value_map_extend_status bt_value_map_extend(
struct bt_value *base_map_obj,
const struct bt_value *extension_obj)
{
+ int status = BT_FUNC_STATUS_OK;
struct extend_map_element_data extend_data = {
.base_obj = NULL,
- .status = BT_FUNC_STATUS_OK,
};
BT_ASSERT_PRE_NO_ERROR();
* in the base map object.
*/
extend_data.base_obj = base_map_obj;
-
- if (bt_value_map_foreach_entry_const(extension_obj, extend_map_element,
- &extend_data)) {
+ status = bt_value_map_foreach_entry_const(extension_obj,
+ extend_map_element, &extend_data);
+ if (status != BT_FUNC_STATUS_OK) {
+ BT_ASSERT(status == BT_FUNC_STATUS_MEMORY_ERROR);
BT_LIB_LOGE_APPEND_CAUSE(
"Cannot iterate on the extension object's elements: "
"%![extension-value-]+v", extension_obj);
}
- return extend_data.status;
+ return status;
}
enum bt_value_copy_status bt_value_copy(const struct bt_value *object,
struct bt_param_validation_context *ctx);
static
-bt_bool validate_map_value_entry(const char *key,
- const bt_value *value, void *v_data)
+bt_value_map_foreach_entry_const_func_status validate_map_value_entry(
+ const char *key, const bt_value *value, void *v_data)
{
struct validate_map_value_data *data = v_data;
const struct bt_param_validation_map_value_entry_descr *entry = NULL;
}
/* Continue iterating if everything is good so far. */
- return data->status == BT_PARAM_VALIDATION_STATUS_OK;
+ return data->status == BT_PARAM_VALIDATION_STATUS_OK ?
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK :
+ BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_INTERRUPT;
}
static
}
static
-bt_bool map_value_foreach_add_key_to_array(const char *key,
- const bt_value *object, void *data)
+bt_value_map_foreach_entry_const_func_status map_value_foreach_add_key_to_array(
+ const char *key, const bt_value *object, void *data)
{
GPtrArray *keys = data;
BT_ASSERT_DBG(keys);
g_ptr_array_add(keys, (void *) key);
- return BT_TRUE;
+ return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK;
}
static
#include <string.h>
#include "tap/tap.h"
-#define NR_TESTS 188
+#define NR_TESTS 190
static
void test_null(void)
}
static
-bt_bool test_map_foreach_cb_count(const char *key, bt_value *object,
+bt_value_map_foreach_entry_func_status test_map_foreach_cb_count(
+ const char *key, bt_value *object,
void *data)
{
int *count = data;
if (*count == 3) {
- return BT_FALSE;
+ return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_INTERRUPT;
+ } else if (*count == 4) {
+ return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR;
+ } else if (*count == 5) {
+ return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_MEMORY_ERROR;
}
(*count)++;
- return BT_TRUE;
+ return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK;
}
struct map_foreach_checklist {
};
static
-bt_bool test_map_foreach_cb_check(const char *key, bt_value *object,
- void *data)
+bt_value_map_foreach_entry_func_status test_map_foreach_cb_check(
+ const char *key, bt_value *object, void *data)
{
struct map_foreach_checklist *checklist = data;
key);
}
- return BT_TRUE;
+ return BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK;
}
static
ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count,
&count);
ok(ret == BT_VALUE_MAP_FOREACH_ENTRY_STATUS_INTERRUPTED && count == 3,
- "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_FALSE");
+ "bt_value_map_foreach_entry() breaks the loop when the user function returns BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_INTERRUPT");
+
+ count = 4;
+ ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count,
+ &count);
+ ok(ret == BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR,
+ "bt_value_map_foreach_entry() fails when the user function returns BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR");
+ bt_current_thread_clear_error();
+
+ count = 5;
+ ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_count,
+ &count);
+ ok(ret == BT_VALUE_MAP_FOREACH_ENTRY_STATUS_MEMORY_ERROR,
+ "bt_value_map_foreach_entry() fails when the user function returns BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_MEMORY_ERROR");
+ bt_current_thread_clear_error();
memset(&checklist, 0, sizeof(checklist));
ret = bt_value_map_foreach_entry(map_obj, test_map_foreach_cb_check,