[PATCH 4/6] shared/hfp: Extract hfp_cleanup function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is yet another cleanup patch preparing us to support hfp_hf.
This one adds helper function to cleanup hfp
---
 src/shared/hfp.c | 56 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 32 insertions(+), 24 deletions(-)

diff --git a/src/shared/hfp.c b/src/shared/hfp.c
index 20ddbd3..5d1f007 100644
--- a/src/shared/hfp.c
+++ b/src/shared/hfp.c
@@ -571,43 +571,51 @@ static bool set_debug(struct hfp *hfp, hfp_debug_func_t callback,
 	return true;
 }
 
-void hfp_gw_unref(struct hfp_gw *hfp)
+static void hfp_cleanup(struct hfp *hfp)
 {
-	if (!hfp)
-		return;
+	io_set_write_handler(hfp->io, NULL, NULL, NULL);
+	io_set_read_handler(hfp->io, NULL, NULL, NULL);
+	io_set_disconnect_handler(hfp->io, NULL, NULL, NULL);
 
-	if (__sync_sub_and_fetch(&hfp->hfp.ref_count, 1))
-		return;
+	io_destroy(hfp->io);
+	hfp->io = NULL;
 
-	hfp_gw_set_command_handler(hfp, NULL, NULL, NULL);
+	if (hfp->close_on_unref)
+		close(hfp->fd);
 
-	io_set_write_handler(hfp->hfp.io, NULL, NULL, NULL);
-	io_set_read_handler(hfp->hfp.io, NULL, NULL, NULL);
-	io_set_disconnect_handler(hfp->hfp.io, NULL, NULL, NULL);
+	set_debug(hfp, NULL, NULL, NULL);
 
-	io_destroy(hfp->hfp.io);
-	hfp->hfp.io = NULL;
+	ringbuf_free(hfp->read_buf);
+	hfp->read_buf = NULL;
 
-	if (hfp->hfp.close_on_unref)
-		close(hfp->hfp.fd);
+	ringbuf_free(hfp->write_buf);
+	hfp->write_buf = NULL;
 
-	set_debug(&hfp->hfp, NULL, NULL, NULL);
+	if (!hfp->in_disconnect) {
+		/*
+		 * Since struct hfp is a first field in hfp_gw, pointer to this
+		 * also points to hfp_gw, therefore we can do free here.
+		 */
+		free(hfp);
+		return;
+	}
 
-	ringbuf_free(hfp->hfp.read_buf);
-	hfp->hfp.read_buf = NULL;
+	hfp->destroyed = true;
+}
 
-	ringbuf_free(hfp->hfp.write_buf);
-	hfp->hfp.write_buf = NULL;
+void hfp_gw_unref(struct hfp_gw *hfp)
+{
+	if (!hfp)
+		return;
+
+	if (__sync_sub_and_fetch(&hfp->hfp.ref_count, 1))
+		return;
 
+	hfp_gw_set_command_handler(hfp, NULL, NULL, NULL);
 	queue_destroy(hfp->cmd_handlers, destroy_cmd_handler);
 	hfp->cmd_handlers = NULL;
 
-	if (!hfp->hfp.in_disconnect) {
-		free(hfp);
-		return;
-	}
-
-	hfp->hfp.destroyed = true;
+	hfp_cleanup(&hfp->hfp);
 }
 
 bool hfp_gw_set_debug(struct hfp_gw *hfp, hfp_debug_func_t callback,
-- 
1.8.4

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux