From 6843ad31769c088ca259020fd9ea8dfb3a51f68e Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Sun, 22 Jun 2003 19:39:47 +0000 Subject: 2003-06-22 Havoc Pennington * dbus/dbus-dataslot.c (_dbus_data_slot_allocator_unref) (_dbus_data_slot_allocator_alloc): rework these to keep a reference count on each slot and automatically manage a global slot ID variable passed in by address * bus/bus.c: convert to new dataslot API * dbus/dbus-bus.c: convert to new dataslot API * dbus/dbus-connection.c: convert to new dataslot API * dbus/dbus-server.c: convert to new dataslot API * glib/dbus-gmain.c: ditto * bus/test.c: ditto * bus/connection.c: ditto --- bus/bus.c | 51 +++++++++------------------------------------------ bus/connection.c | 42 ++++-------------------------------------- bus/dispatch.c | 47 ++++++----------------------------------------- bus/test.c | 42 ++++-------------------------------------- 4 files changed, 23 insertions(+), 159 deletions(-) (limited to 'bus') diff --git a/bus/bus.c b/bus/bus.c index e86243a6..a5530974 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -48,8 +48,7 @@ struct BusContext BusLimits limits; }; -static int server_data_slot = -1; -static int server_data_slot_refcount = 0; +static dbus_int32_t server_data_slot = -1; typedef struct { @@ -58,57 +57,25 @@ typedef struct #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot)) -static dbus_bool_t -server_data_slot_ref (void) -{ - if (server_data_slot < 0) - { - server_data_slot = dbus_server_allocate_data_slot (); - - if (server_data_slot < 0) - return FALSE; - - _dbus_assert (server_data_slot_refcount == 0); - } - - server_data_slot_refcount += 1; - - return TRUE; -} - -static void -server_data_slot_unref (void) -{ - _dbus_assert (server_data_slot_refcount > 0); - - server_data_slot_refcount -= 1; - - if (server_data_slot_refcount == 0) - { - dbus_server_free_data_slot (server_data_slot); - server_data_slot = -1; - } -} - static BusContext* server_get_context (DBusServer *server) { BusContext *context; BusServerData *bd; - if (!server_data_slot_ref ()) + if (!dbus_server_allocate_data_slot (&server_data_slot)) return NULL; bd = BUS_SERVER_DATA (server); if (bd == NULL) { - server_data_slot_unref (); + dbus_server_free_data_slot (&server_data_slot); return NULL; } context = bd->context; - server_data_slot_unref (); + dbus_server_free_data_slot (&server_data_slot); return context; } @@ -303,7 +270,7 @@ bus_context_new (const DBusString *config_file, return NULL; } - if (!server_data_slot_ref ()) + if (!dbus_server_allocate_data_slot (&server_data_slot)) { BUS_SET_OOM (error); _dbus_string_free (&full_address); @@ -358,7 +325,7 @@ bus_context_new (const DBusString *config_file, /* we need another ref of the server data slot for the context * to own */ - if (!server_data_slot_ref ()) + if (!dbus_server_allocate_data_slot (&server_data_slot)) _dbus_assert_not_reached ("second ref of server data slot failed"); context->user_database = _dbus_user_database_new (); @@ -633,7 +600,7 @@ bus_context_new (const DBusString *config_file, bus_config_parser_unref (parser); _dbus_string_free (&full_address); dbus_free_string_array (auth_mechanisms); - server_data_slot_unref (); + dbus_server_free_data_slot (&server_data_slot); return context; @@ -647,7 +614,7 @@ bus_context_new (const DBusString *config_file, _dbus_string_free (&full_address); dbus_free_string_array (auth_mechanisms); - server_data_slot_unref (); + dbus_server_free_data_slot (&server_data_slot); return NULL; } @@ -769,7 +736,7 @@ bus_context_unref (BusContext *context) dbus_free (context); - server_data_slot_unref (); + dbus_server_free_data_slot (&server_data_slot); } } diff --git a/bus/connection.c b/bus/connection.c index 70a0eb15..5121658d 100644 --- a/bus/connection.c +++ b/bus/connection.c @@ -43,8 +43,7 @@ struct BusConnections DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */ }; -static int connection_data_slot = -1; -static int connection_data_slot_refcount = 0; +static dbus_int32_t connection_data_slot = -1; typedef struct { @@ -67,39 +66,6 @@ static dbus_bool_t expire_incomplete_timeout (void *data); #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot)) -static dbus_bool_t -connection_data_slot_ref (void) -{ - if (connection_data_slot < 0) - { - connection_data_slot = dbus_connection_allocate_data_slot (); - - if (connection_data_slot < 0) - return FALSE; - - _dbus_assert (connection_data_slot_refcount == 0); - } - - connection_data_slot_refcount += 1; - - return TRUE; - -} - -static void -connection_data_slot_unref (void) -{ - _dbus_assert (connection_data_slot_refcount > 0); - - connection_data_slot_refcount -= 1; - - if (connection_data_slot_refcount == 0) - { - dbus_connection_free_data_slot (connection_data_slot); - connection_data_slot = -1; - } -} - static DBusLoop* connection_get_loop (DBusConnection *connection) { @@ -419,7 +385,7 @@ bus_connections_new (BusContext *context) { BusConnections *connections; - if (!connection_data_slot_ref ()) + if (!dbus_connection_allocate_data_slot (&connection_data_slot)) goto failed_0; connections = dbus_new0 (BusConnections, 1); @@ -456,7 +422,7 @@ bus_connections_new (BusContext *context) failed_2: dbus_free (connections); failed_1: - connection_data_slot_unref (); + dbus_connection_free_data_slot (&connection_data_slot); failed_0: return NULL; } @@ -515,7 +481,7 @@ bus_connections_unref (BusConnections *connections) dbus_free (connections); - connection_data_slot_unref (); + dbus_connection_free_data_slot (&connection_data_slot); } } diff --git a/bus/dispatch.c b/bus/dispatch.c index 8b2ba08e..d43e8121 100644 --- a/bus/dispatch.c +++ b/bus/dispatch.c @@ -32,8 +32,7 @@ #include #include -static int message_handler_slot = -1; -static int message_handler_slot_refcount; +static dbus_int32_t message_handler_slot = -1; typedef struct { @@ -309,48 +308,15 @@ bus_dispatch_message_handler (DBusMessageHandler *handler, return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; } -static dbus_bool_t -message_handler_slot_ref (void) -{ - if (message_handler_slot < 0) - { - message_handler_slot = dbus_connection_allocate_data_slot (); - - if (message_handler_slot < 0) - return FALSE; - - _dbus_assert (message_handler_slot_refcount == 0); - } - - message_handler_slot_refcount += 1; - - return TRUE; -} - -static void -message_handler_slot_unref (void) -{ - _dbus_assert (message_handler_slot_refcount > 0); - - message_handler_slot_refcount -= 1; - - if (message_handler_slot_refcount == 0) - { - dbus_connection_free_data_slot (message_handler_slot); - message_handler_slot = -1; - } -} - static void free_message_handler (void *data) { DBusMessageHandler *handler = data; _dbus_assert (message_handler_slot >= 0); - _dbus_assert (message_handler_slot_refcount > 0); dbus_message_handler_unref (handler); - message_handler_slot_unref (); + dbus_connection_free_data_slot (&message_handler_slot); } dbus_bool_t @@ -358,26 +324,25 @@ bus_dispatch_add_connection (DBusConnection *connection) { DBusMessageHandler *handler; - if (!message_handler_slot_ref ()) + if (!dbus_connection_allocate_data_slot (&message_handler_slot)) return FALSE; handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL); if (handler == NULL) { - message_handler_slot_unref (); + dbus_connection_free_data_slot (&message_handler_slot); return FALSE; } if (!dbus_connection_add_filter (connection, handler)) { dbus_message_handler_unref (handler); - message_handler_slot_unref (); + dbus_connection_free_data_slot (&message_handler_slot); return FALSE; } _dbus_assert (message_handler_slot >= 0); - _dbus_assert (message_handler_slot_refcount > 0); if (!dbus_connection_set_data (connection, message_handler_slot, @@ -385,7 +350,7 @@ bus_dispatch_add_connection (DBusConnection *connection) free_message_handler)) { dbus_message_handler_unref (handler); - message_handler_slot_unref (); + dbus_connection_free_data_slot (&message_handler_slot); return FALSE; } diff --git a/bus/test.c b/bus/test.c index f8d4c5f3..30cbcd05 100644 --- a/bus/test.c +++ b/bus/test.c @@ -123,41 +123,7 @@ client_disconnect_handler (DBusMessageHandler *handler, return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; } -static int handler_slot = -1; -static int handler_slot_refcount = 0; - -static dbus_bool_t -handler_slot_ref (void) -{ - if (handler_slot < 0) - { - handler_slot = dbus_connection_allocate_data_slot (); - - if (handler_slot < 0) - return FALSE; - - _dbus_assert (handler_slot_refcount == 0); - } - - handler_slot_refcount += 1; - - return TRUE; - -} - -static void -handler_slot_unref (void) -{ - _dbus_assert (handler_slot_refcount > 0); - - handler_slot_refcount -= 1; - - if (handler_slot_refcount == 0) - { - dbus_connection_free_data_slot (handler_slot); - handler_slot = -1; - } -} +static dbus_int32_t handler_slot = -1; static void free_handler (void *data) @@ -165,7 +131,7 @@ free_handler (void *data) DBusMessageHandler *handler = data; dbus_message_handler_unref (handler); - handler_slot_unref (); + dbus_connection_free_data_slot (&handler_slot); } dbus_bool_t @@ -217,7 +183,7 @@ bus_setup_debug_client (DBusConnection *connection) if (!_dbus_list_append (&clients, connection)) goto out; - if (!handler_slot_ref ()) + if (!dbus_connection_allocate_data_slot (&handler_slot)) goto out; /* Set up handler to be destroyed */ @@ -225,7 +191,7 @@ bus_setup_debug_client (DBusConnection *connection) disconnect_handler, free_handler)) { - handler_slot_unref (); + dbus_connection_free_data_slot (&handler_slot); goto out; } -- cgit