Clean up handle free function 19/130619/4
authorHyeongsik Min <hyeongsik.min@samsung.com>
Tue, 23 May 2017 06:22:05 +0000 (15:22 +0900)
committerHyeongsik Min <hyeongsik.min@samsung.com>
Wed, 24 May 2017 05:19:48 +0000 (05:19 +0000)
Without traversing the list, this patch uses g_list_find() to get a link of
current handle.

Change-Id: I7ec38d61c09f6c6fd1c68c6f93f50759109c0fdb
Signed-off-by: Hyeongsik Min <hyeongsik.min@samsung.com>
src/daemon/peripheral_bus_i2c.c
src/daemon/peripheral_bus_pwm.c
src/daemon/peripheral_bus_uart.c

index 9c0146c..81a01de 100644 (file)
@@ -32,16 +32,16 @@ static pb_i2c_data_h peripheral_bus_i2c_data_get(int bus, int address, GList **l
 {
        GList *i2c_list = *list;
        GList *link;
-       pb_i2c_data_h i2c_data;
+       pb_i2c_data_h i2c_handle;
 
        if (i2c_list == NULL)
                return NULL;
 
        link = i2c_list;
        while (link) {
-               i2c_data = (pb_i2c_data_h)link->data;
-               if (i2c_data->bus == bus && i2c_data->address == address)
-                       return i2c_data;
+               i2c_handle = (pb_i2c_data_h)link->data;
+               if (i2c_handle->bus == bus && i2c_handle->address == address)
+                       return i2c_handle;
                link = g_list_next(link);
        }
 
@@ -51,44 +51,38 @@ static pb_i2c_data_h peripheral_bus_i2c_data_get(int bus, int address, GList **l
 static pb_i2c_data_h peripheral_bus_i2c_data_new(GList **list)
 {
        GList *i2c_list = *list;
-       pb_i2c_data_h i2c_data;
+       pb_i2c_data_h i2c_handle;
 
-       i2c_data = (pb_i2c_data_h)calloc(1, sizeof(peripheral_bus_i2c_data_s));
-       if (i2c_data == NULL) {
+       i2c_handle = (pb_i2c_data_h)calloc(1, sizeof(peripheral_bus_i2c_data_s));
+       if (i2c_handle == NULL) {
                _E("failed to allocate peripheral_bus_i2c_data_s");
                return NULL;
        }
 
-       *list = g_list_append(i2c_list, i2c_data);
+       *list = g_list_append(i2c_list, i2c_handle);
 
-       return i2c_data;
+       return i2c_handle;
 }
 
-static int peripheral_bus_i2c_data_free(pb_i2c_data_h i2c_handle, GList **list)
+static int peripheral_bus_i2c_data_free(pb_i2c_data_h i2c_handle, GList **i2c_list)
 {
-       GList *i2c_list = *list;
        GList *link;
-       pb_i2c_data_h i2c;
 
-       if (i2c_handle == NULL)
-               return -1;
+       RETVM_IF(i2c_handle == NULL, -1, "handle is null");
 
-       link = i2c_list;
-       while (link) {
-               i2c = (pb_i2c_data_h)link->data;
-
-               if (i2c == i2c_handle) {
-                       *list = g_list_remove_link(i2c_list, link);
-                       if (i2c->buffer)
-                               free(i2c->buffer);
-                       free(i2c);
-                       g_list_free(link);
-                       return 0;
-               }
-               link = g_list_next(link);
+       link = g_list_find(*i2c_list, i2c_handle);
+       if (!link) {
+               _E("handle does not exist in list");
+               return -1;
        }
 
-       return -1;
+       *i2c_list = g_list_remove_link(*i2c_list, link);
+       if (i2c_handle->buffer)
+               free(i2c_handle->buffer);
+       free(i2c_handle);
+       g_list_free(link);
+
+       return 0;
 }
 
 int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer user_data)
index 35d5e9f..aed54dd 100644 (file)
@@ -29,13 +29,13 @@ static pb_pwm_data_h peripheral_bus_pwm_data_get(int device, int channel, GList
 {
        GList *pwm_list = *list;
        GList *link;
-       pb_pwm_data_h pwm_data;
+       pb_pwm_data_h pwm_handle;
 
        link = pwm_list;
        while (link) {
-               pwm_data = (pb_pwm_data_h)link->data;
-               if (pwm_data->device == device && pwm_data->channel == channel)
-                       return pwm_data;
+               pwm_handle = (pb_pwm_data_h)link->data;
+               if (pwm_handle->device == device && pwm_handle->channel == channel)
+                       return pwm_handle;
                link = g_list_next(link);
        }
 
@@ -45,39 +45,36 @@ static pb_pwm_data_h peripheral_bus_pwm_data_get(int device, int channel, GList
 static pb_pwm_data_h peripheral_bus_pwm_data_new(GList **list)
 {
        GList *pwm_list = *list;
-       pb_pwm_data_h pwm_data;
+       pb_pwm_data_h pwm_handle;
 
-       pwm_data = (pb_pwm_data_h)calloc(1, sizeof(peripheral_bus_pwm_data_s));
-       if (pwm_data == NULL) {
+       pwm_handle = (pb_pwm_data_h)calloc(1, sizeof(peripheral_bus_pwm_data_s));
+       if (pwm_handle == NULL) {
                _E("failed to allocate peripheral_bus_pwm_data_s");
                return NULL;
        }
 
-       *list = g_list_append(pwm_list, pwm_data);
+       *list = g_list_append(pwm_list, pwm_handle);
 
-       return pwm_data;
+       return pwm_handle;
 }
 
-static int peripheral_bus_pwm_data_free(pb_pwm_data_h pwm_handle, GList **list)
+static int peripheral_bus_pwm_data_free(pb_pwm_data_h pwm_handle, GList **pwm_list)
 {
-       GList *pwm_list = *list;
        GList *link;
-       pb_pwm_data_h pwm_data;
 
-       link = pwm_list;
-       while (link) {
-               pwm_data = (pb_pwm_data_h)link->data;
-
-               if (pwm_data == pwm_handle) {
-                       *list = g_list_remove_link(pwm_list, link);
-                       free(pwm_data);
-                       g_list_free(link);
-                       return 0;
-               }
-               link = g_list_next(link);
+       RETVM_IF(pwm_handle == NULL, -1, "handle is null");
+
+       link = g_list_find(*pwm_list, pwm_handle);
+       if (!link) {
+               _E("handle does not exist in list");
+               return -1;
        }
 
-       return -1;
+       *pwm_list = g_list_remove_link(*pwm_list, link);
+       free(pwm_handle);
+       g_list_free(link);
+
+       return 0;
 }
 
 int peripheral_bus_pwm_open(int device, int channel, pb_pwm_data_h *pwm, gpointer user_data)
index c555e45..549c1b4 100644 (file)
@@ -33,13 +33,13 @@ static pb_uart_data_h peripheral_bus_uart_data_get(int port, GList **list)
 {
        GList *uart_list = *list;
        GList *link;
-       pb_uart_data_h uart_data;
+       pb_uart_data_h uart_handle;
 
        link = uart_list;
        while (link) {
-               uart_data = (pb_uart_data_h)link->data;
-               if (uart_data->port == port)
-                       return uart_data;
+               uart_handle = (pb_uart_data_h)link->data;
+               if (uart_handle->port == port)
+                       return uart_handle;
                link = g_list_next(link);
        }
 
@@ -49,41 +49,38 @@ static pb_uart_data_h peripheral_bus_uart_data_get(int port, GList **list)
 static pb_uart_data_h peripheral_bus_uart_data_new(GList **list)
 {
        GList *uart_list = *list;
-       pb_uart_data_h uart_data;
+       pb_uart_data_h uart_handle;
 
-       uart_data = (pb_uart_data_h)calloc(1, sizeof(peripheral_bus_uart_data_s));
-       if (uart_data == NULL) {
+       uart_handle = (pb_uart_data_h)calloc(1, sizeof(peripheral_bus_uart_data_s));
+       if (uart_handle == NULL) {
                _E("failed to allocate peripheral_bus_uart_data_s");
                return NULL;
        }
 
-       *list = g_list_append(uart_list, uart_data);
+       *list = g_list_append(uart_list, uart_handle);
 
-       return uart_data;
+       return uart_handle;
 }
 
-static int peripheral_bus_uart_data_free(pb_uart_data_h uart_handle, GList **list)
+static int peripheral_bus_uart_data_free(pb_uart_data_h uart_handle, GList **uart_list)
 {
-       GList *uart_list = *list;
        GList *link;
-       pb_uart_data_h uart_data;
 
-       link = uart_list;
-       while (link) {
-               uart_data = (pb_uart_data_h)link->data;
-
-               if (uart_data == uart_handle) {
-                       *list = g_list_remove_link(uart_list, link);
-                       if (uart_data->buffer)
-                               free(uart_data->buffer);
-                       free(uart_data);
-                       g_list_free(link);
-                       return 0;
-               }
-               link = g_list_next(link);
+       RETVM_IF(uart_handle == NULL, -1, "handle is null");
+
+       link = g_list_find(*uart_list, uart_handle);
+       if (!link) {
+               _E("handle does not exist in list");
+               return -1;
        }
 
-       return -1;
+       *uart_list = g_list_remove_link(*uart_list, link);
+       if (uart_handle->buffer)
+               free(uart_handle->buffer);
+       free(uart_handle);
+       g_list_free(link);
+
+       return 0;
 }
 
 int peripheral_bus_uart_open(int port, pb_uart_data_h *uart, gpointer user_data)