Simplify message reference management in Client_send_message_except().
authorTilman Sauerbeck <tilman@code-monkey.de>
Thu, 28 Dec 2017 11:20:56 +0000 (12:20 +0100)
committerTilman Sauerbeck <tilman@code-monkey.de>
Thu, 28 Dec 2017 11:20:56 +0000 (12:20 +0100)
In Client_send_message_except() and Client_send_message_except_ver(),
we only need to take an extra reference when we are about to send
the message.

src/client.c

index 1aea8e2fb5fadb32a66014f1182c761f2f84c875..3d884629dc66235e3311a6462e2c60e1814af79c 100644 (file)
@@ -679,22 +679,15 @@ void Client_textmessage(client_t *client, char *text)
 int Client_send_message_except(client_t *client, message_t *msg)
 {
        client_t *itr = NULL;
 int Client_send_message_except(client_t *client, message_t *msg)
 {
        client_t *itr = NULL;
-       int count = 0;
 
 
-       Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */
        while (Client_iterate(&itr) != NULL) {
                if (itr != client) {
        while (Client_iterate(&itr) != NULL) {
                if (itr != client) {
-                       if (count++ > 0)
-                               Msg_inc_ref(msg); /* One extra reference for each new copy */
+                       Msg_inc_ref(msg); /* One extra reference for each new copy */
                        Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
                        Client_send_message(itr, msg);
                }
        }
                        Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
                        Client_send_message(itr, msg);
                }
        }
-       Msg_free(msg); /* Free our reference to the message */
-
-       if (count == 0)
-               Msg_free(msg); /* If only 1 client is connected then no message is passed
-                                               * to Client_send_message(). Free it here. */
+       Msg_free(msg); /* Consume caller's reference. */
 
        return 0;
 }
 
        return 0;
 }
@@ -702,22 +695,15 @@ int Client_send_message_except(client_t *client, message_t *msg)
 int Client_send_message_except_ver(client_t *client, message_t *msg, uint32_t version)
 {
        client_t *itr = NULL;
 int Client_send_message_except_ver(client_t *client, message_t *msg, uint32_t version)
 {
        client_t *itr = NULL;
-       int count = 0;
 
 
-       Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */
        while (Client_iterate(&itr) != NULL) {
                if (itr != client) {
        while (Client_iterate(&itr) != NULL) {
                if (itr != client) {
-                       if (count++ > 0)
-                               Msg_inc_ref(msg); /* One extra reference for each new copy */
+                       Msg_inc_ref(msg); /* One extra reference for each new copy */
                        Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
                        Client_send_message_ver(itr, msg, version);
                }
        }
                        Log_debug("Msg %d to %s refcount %d",  msg->messageType, itr->username, msg->refcount);
                        Client_send_message_ver(itr, msg, version);
                }
        }
-       Msg_free(msg); /* Free our reference to the message */
-
-       if (count == 0)
-               Msg_free(msg); /* If only 1 client is connected then no message is passed
-                                               * to Client_send_message(). Free it here. */
+       Msg_free(msg); /* Consume caller's reference. */
 
        return 0;
 }
 
        return 0;
 }