changeset 30847:74c4aa38adfc

Add support to ref/unref SlpMessageParts.
author masca@cpw.pidgin.im
date Tue, 06 Jul 2010 19:00:21 +0000
parents e0c374ad8fd3
children 544221aadf6e
files libpurple/protocols/msn/directconn.c libpurple/protocols/msn/msg.c libpurple/protocols/msn/slpmsg_part.c libpurple/protocols/msn/slpmsg_part.h
diffstat 4 files changed, 39 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/directconn.c	Tue Jul 06 18:56:58 2010 +0000
+++ b/libpurple/protocols/msn/directconn.c	Tue Jul 06 19:00:21 2010 +0000
@@ -116,7 +116,7 @@
 		msn_message_unref(p->msg);
 #endif
 	if (p->part)
-		msn_slpmsgpart_destroy(p->part);
+		msn_slpmsgpart_unref(p->part);
 
 	g_free(p);
 }
--- a/libpurple/protocols/msn/msg.c	Tue Jul 06 18:56:58 2010 +0000
+++ b/libpurple/protocols/msn/msg.c	Tue Jul 06 19:00:21 2010 +0000
@@ -72,7 +72,7 @@
 
 	g_hash_table_destroy(msg->header_table);
 	g_list_free(msg->header_list);
-	msn_slpmsgpart_destroy(msg->part);
+	msn_slpmsgpart_unref(msg->part);
 
 	g_free(msg);
 }
--- a/libpurple/protocols/msn/slpmsg_part.c	Tue Jul 06 18:56:58 2010 +0000
+++ b/libpurple/protocols/msn/slpmsg_part.c	Tue Jul 06 19:00:21 2010 +0000
@@ -17,7 +17,7 @@
 	part->ack_cb = msn_slpmsgpart_ack;
 	part->nak_cb = msn_slpmsgpart_nak;
 
-	return part;
+	return msn_slpmsgpart_ref(part);
 }
 
 MsnSlpMessagePart *msn_slpmsgpart_new_from_data(const char *data, size_t data_len)
@@ -61,6 +61,12 @@
 	if (!part)
 		return;
 
+	if (part->ref_count > 0) {
+		msn_slpmsgpart_unref(part);
+		
+		return;
+	}
+
 	g_free(part->header);
 	g_free(part->footer);
 
@@ -68,6 +74,30 @@
 
 }
 
+MsnSlpMessagePart *msn_slpmsgpart_ref(MsnSlpMessagePart *part)
+{
+	g_return_val_if_fail(part != NULL, NULL);
+	part->ref_count ++;
+
+	return part;
+}
+
+MsnSlpMessagePart *msn_slpmsgpart_unref(MsnSlpMessagePart *part)
+{
+	g_return_val_if_fail(part != NULL, NULL);
+	g_return_val_if_fail(part->ref_count > 0, NULL);
+
+	part->ref_count--;
+
+	if (part->ref_count == 0) {
+		msn_slpmsgpart_destroy(part);
+
+		 return NULL;
+	}
+
+	return part;
+}
+
 void msn_slpmsgpart_set_bin_data(MsnSlpMessagePart *part, const void *data, size_t len)
 {
 	g_return_if_fail(part != NULL);
--- a/libpurple/protocols/msn/slpmsg_part.h	Tue Jul 06 18:56:58 2010 +0000
+++ b/libpurple/protocols/msn/slpmsg_part.h	Tue Jul 06 19:00:21 2010 +0000
@@ -8,6 +8,8 @@
 
 struct _MsnSlpMessagePart
 {
+	int ref_count;
+
 	MsnP2PHeader *header;
 	MsnP2PFooter *footer;
 
@@ -25,6 +27,10 @@
 
 void msn_slpmsgpart_destroy(MsnSlpMessagePart *part);
 
+MsnSlpMessagePart *msn_slpmsgpart_ref(MsnSlpMessagePart *part);
+
+MsnSlpMessagePart *msn_slpmsgpart_unref(MsnSlpMessagePart *part);
+
 void msn_slpmsgpart_set_bin_data(MsnSlpMessagePart *part, const void *data, size_t len);
 
 char *msn_slpmsgpart_serialize(MsnSlpMessagePart *part, size_t *ret_size);