summaryrefslogtreecommitdiffstats
path: root/hcid/dbus-common.c
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@nokia.com>2006-03-09 21:14:24 +0000
committerJohan Hedberg <johan.hedberg@nokia.com>2006-03-09 21:14:24 +0000
commit3b53ed0f319e17bccabd3a2a8c4fb4730057a38b (patch)
tree613d12b456796b7655d9918ec4dbbf05cc5ddea3 /hcid/dbus-common.c
parentbe42c25c8001e606c19c7214a843a86183608f60 (diff)
Rework name_data_add function
Diffstat (limited to 'hcid/dbus-common.c')
-rw-r--r--hcid/dbus-common.c66
1 files changed, 36 insertions, 30 deletions
diff --git a/hcid/dbus-common.c b/hcid/dbus-common.c
index c4e1cbf8..45cc6246 100644
--- a/hcid/dbus-common.c
+++ b/hcid/dbus-common.c
@@ -78,52 +78,58 @@ static struct name_callback *name_callback_find(struct slist *callbacks,
return NULL;
}
+static void name_data_free(struct name_data *data)
+{
+ struct slist *l;
+
+ for (l = data->callbacks; l != NULL; l = l->next)
+ free(l->data);
+
+ free(data->name);
+ free(data);
+}
+
static int name_data_add(const char *name, name_cb_t func, void *user_data)
{
- int first = 0;
- struct name_data *data;
- struct name_callback *cb;
+ int first = 1;
+ struct name_data *data = NULL;
+ struct name_callback *cb = NULL;
cb = malloc(sizeof(struct name_callback));
if (!cb)
- return 0;
+ goto failed;
cb->func = func;
cb->user_data = user_data;
data = name_data_find(name);
- if (!data) {
- data = malloc(sizeof(struct name_data));
- if (!data) {
- free(cb);
- return 0;
- }
-
- data->name = strdup(name);
- if (!data->name) {
- free(cb);
- return 0;
- }
-
- data->callbacks = NULL;
- name_listeners = slist_append(name_listeners, data);
- first = 1;
+ if (data) {
+ first = 0;
+ goto done;
}
- data->callbacks = slist_append(data->callbacks, cb);
+ data = malloc(sizeof(struct name_data));
+ if (!data)
+ goto failed;
- return first;
-}
+ memset(data, sizeof(struct name_data), 0);
-static void name_data_free(struct name_data *data)
-{
- struct slist *l;
+ data->name = strdup(name);
+ if (!data->name)
+ goto failed;
- for (l = data->callbacks; l != NULL; l = l->next)
- free(l->data);
+ name_listeners = slist_append(name_listeners, data);
- free(data->name);
- free(data);
+done:
+ data->callbacks = slist_append(data->callbacks, cb);
+ return first;
+
+failed:
+ if (data)
+ name_data_free(data);
+ if (cb)
+ free(cb);
+ return 0;
}
static void name_data_remove(const char *name, name_cb_t func, void *user_data)