From 67591657185ad223a5f95aadee54c33900730cd0 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 22 Jun 2017 18:13:10 +0100 Subject: [PATCH 23/49] bus/containers: Each connection to a container holds a reference Signed-off-by: Simon McVittie --- bus/containers.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/bus/containers.c b/bus/containers.c index 33184d68..7771ea6c 100644 --- a/bus/containers.c +++ b/bus/containers.c @@ -71,15 +71,26 @@ struct BusContainers dbus_uint64_t next_container_id; }; +/* Data slot on DBusConnection, holding BusContainerInstance */ +static dbus_int32_t contained_data_slot = -1; + BusContainers * bus_containers_new (void) { /* We allocate the hash table lazily, expecting that the common case will * be a connection where this feature is never used */ - BusContainers *self = dbus_new0 (BusContainers, 1); + BusContainers *self = NULL; DBusString dir; const char *tmpdir; + /* One reference per BusContainers, unless we ran out of memory the first + * time we tried to allocate it, in which case it will be -1 when we + * free the BusContainers */ + if (!dbus_connection_allocate_data_slot (&contained_data_slot)) + goto oom; + + self = dbus_new0 (BusContainers, 1); + if (self == NULL) goto oom; @@ -123,7 +134,15 @@ bus_containers_new (void) oom: if (self != NULL) - bus_containers_unref (self); + { + /* This will free the data slot too */ + bus_containers_unref (self); + } + else + { + if (contained_data_slot != -1) + dbus_connection_free_data_slot (&contained_data_slot); + } return NULL; } @@ -149,6 +168,9 @@ bus_containers_unref (BusContainers *self) _dbus_string_free (&self->address_template); dbus_free (self); + + if (contained_data_slot != -1) + dbus_connection_free_data_slot (&contained_data_slot); } } @@ -307,6 +329,14 @@ new_connection_cb (DBusServer *server, { BusContainerInstance *instance = data; + if (!dbus_connection_set_data (new_connection, contained_data_slot, + bus_container_instance_ref (instance), + (DBusFreeFunction) bus_container_instance_unref)) + { + bus_container_instance_unref (instance); + return; + } + if (!bus_context_add_incoming_connection (instance->context, new_connection)) return; -- 2.11.0