From d4e80132af03363a2f861cfd611847ee8758aed9 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 12 May 2003 02:44:45 +0000 Subject: 2003-05-11 Havoc Pennington * dbus/dbus-marshal.c (_dbus_marshal_validate_arg): fix to avoid calling _dbus_marshal_validate_arg() for every byte in a byte array, etc. * dbus/dbus-message-handler.c: use atomic reference counting to reduce number of locks slightly; the global lock in here sucks * dbus/dbus-connection.c (_dbus_connection_update_dispatch_status_and_unlock): variant of update_dispatch_status that can be called with lock held; then use in a couple places to reduce locking/unlocking (dbus_connection_send): hold the lock over the whole function instead of acquiring it twice. * dbus/dbus-timeout.c (_dbus_timeout_new): handle OOM * bus/connection.c (bus_connections_setup_connection): fix access to already-freed memory. * dbus/dbus-connection.c: keep a little cache of linked list nodes, to avoid using the global linked list alloc lock in the normal send-message case. Instead we just use the connection lock that we already have to take. * dbus/dbus-list.c (_dbus_list_find_last): new function * dbus/dbus-sysdeps.c (_dbus_atomic_inc, _dbus_atomic_dec): change to use a struct for the atomic type; fix docs, they return value before increment, not after increment. * dbus/dbus-string.c (_dbus_string_append_4_aligned) (_dbus_string_append_8_aligned): new functions to try to microoptimize this operation. (reallocate_for_length): break this out of set_length(), to improve profile info, and also so we can consider inlining the set_length() part. * dbus/dbus-message.c (dbus_message_new_empty_header): init data strings with some preallocation, cuts down on our calls to realloc a fair bit. Though if we can get the "move entire string to empty string" optimization below to kick in here, it would be better. * dbus/dbus-string.c (_dbus_string_move): just call _dbus_string_move_len (_dbus_string_move_len): add a special case for moving an entire string into an empty string; we can just swap the string data instead of doing any reallocs. (_dbus_string_init_preallocated): new function --- dbus/dbus-message-handler.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) (limited to 'dbus/dbus-message-handler.c') diff --git a/dbus/dbus-message-handler.c b/dbus/dbus-message-handler.c index a978ba05..f38e5100 100644 --- a/dbus/dbus-message-handler.c +++ b/dbus/dbus-message-handler.c @@ -1,7 +1,7 @@ /* -*- mode: C; c-file-style: "gnu" -*- */ /* dbus-message-handler.c Sender/receiver of messages. * - * Copyright (C) 2002 Red Hat Inc. + * Copyright (C) 2002, 2003 Red Hat Inc. * * Licensed under the Academic Free License version 1.2 * @@ -47,7 +47,7 @@ _DBUS_DEFINE_GLOBAL_LOCK (message_handler); */ struct DBusMessageHandler { - int refcount; /**< reference count */ + DBusAtomic refcount; /**< reference count */ DBusHandleMessageFunction function; /**< handler function */ void *user_data; /**< user data for function */ @@ -176,7 +176,7 @@ dbus_message_handler_new (DBusHandleMessageFunction function, if (handler == NULL) return NULL; - handler->refcount = 1; + handler->refcount.value = 1; handler->function = function; handler->user_data = user_data; handler->free_user_data = free_user_data; @@ -194,12 +194,8 @@ void dbus_message_handler_ref (DBusMessageHandler *handler) { _dbus_return_if_fail (handler != NULL); - - _DBUS_LOCK (message_handler); - _dbus_assert (handler != NULL); - - handler->refcount += 1; - _DBUS_UNLOCK (message_handler); + + _dbus_atomic_inc (&handler->refcount); } /** @@ -211,21 +207,13 @@ dbus_message_handler_ref (DBusMessageHandler *handler) void dbus_message_handler_unref (DBusMessageHandler *handler) { - int refcount; + dbus_bool_t last_unref; _dbus_return_if_fail (handler != NULL); - - _DBUS_LOCK (message_handler); - - _dbus_assert (handler != NULL); - _dbus_assert (handler->refcount > 0); - handler->refcount -= 1; - refcount = handler->refcount; - - _DBUS_UNLOCK (message_handler); + last_unref = (_dbus_atomic_dec (&handler->refcount) == 1); - if (refcount == 0) + if (last_unref) { DBusList *link; -- cgit