diff options
author | Richard Hughes <richard@hughsie.com> | 2015-01-06 11:54:43 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2015-01-06 11:54:43 +0000 |
commit | cdbd7e6cc165c39580e72ac1415d233f5eaa7101 (patch) | |
tree | 6ea38596717c0b7b0b80c33baffcfaeb7faabbf9 | |
parent | 229c9b0322a88245cad966aa527458e291bae8d2 (diff) | |
download | gusb-cdbd7e6cc165c39580e72ac1415d233f5eaa7101.tar.gz |
Use __attribute__(cleanup) in more places
-rw-r--r-- | gusb/gusb-context.c | 19 | ||||
-rw-r--r-- | gusb/gusb-self-test.c | 40 | ||||
-rw-r--r-- | tools/gusb-main.c | 33 |
3 files changed, 28 insertions, 64 deletions
diff --git a/gusb/gusb-context.c b/gusb/gusb-context.c index 1334b16..7579d2e 100644 --- a/gusb/gusb-context.c +++ b/gusb/gusb-context.c @@ -274,11 +274,11 @@ static void g_usb_context_add_device (GUsbContext *context, struct libusb_device *dev) { - GUsbDevice *device = NULL; GUsbContextPrivate *priv = context->priv; guint8 bus; guint8 address; - GError *error = NULL; + _cleanup_error_free_ GError *error = NULL; + _cleanup_object_unref_ GUsbDevice *device = NULL; /* does any existing device exist */ bus = libusb_get_bus_number (dev); @@ -288,7 +288,7 @@ g_usb_context_add_device (GUsbContext *context, device = g_usb_context_find_by_bus_address (context, bus, address, NULL); if (device != NULL) { g_debug ("%i:%i already exists", bus, address); - goto out; + return; } /* add the device */ @@ -296,24 +296,20 @@ g_usb_context_add_device (GUsbContext *context, if (device == NULL) { g_debug ("There was a problem creating the device: %s", error->message); - g_error_free (error); - goto out; + return; } g_ptr_array_add (priv->devices, g_object_ref (device)); g_usb_context_emit_device_add (context, device); -out: - if (device != NULL) - g_object_unref (device); } static void g_usb_context_remove_device (GUsbContext *context, struct libusb_device *dev) { - GUsbDevice *device = NULL; GUsbContextPrivate *priv = context->priv; guint8 bus; guint8 address; + _cleanup_object_unref_ GUsbDevice *device = NULL; /* does any existing device exist */ bus = libusb_get_bus_number (dev); @@ -325,7 +321,6 @@ g_usb_context_remove_device (GUsbContext *context, } g_usb_context_emit_device_remove (context, device); g_ptr_array_remove (priv->devices, device); - g_object_unref (device); } static int @@ -352,13 +347,13 @@ g_usb_context_hotplug_cb (struct libusb_context *ctx, static void g_usb_context_rescan (GUsbContext *context) { - GList *existing_devices = NULL; GList *l; GUsbDevice *device; GUsbContextPrivate *priv = context->priv; gboolean found; guint i; libusb_device **dev_list = NULL; + _cleanup_list_free_ GList *existing_devices = NULL; /* copy to a context so we can remove from the array */ for (i = 0; i < priv->devices->len; i++) { @@ -387,8 +382,6 @@ g_usb_context_rescan (GUsbContext *context) libusb_get_device_list (priv->ctx, &dev_list); for (i = 0; dev_list && dev_list[i]; i++) g_usb_context_add_device (context, dev_list[i]); - - g_list_free (existing_devices); libusb_free_device_list (dev_list, 1); } diff --git a/gusb/gusb-self-test.c b/gusb/gusb-self-test.c index 5eaee0a..3ad0199 100644 --- a/gusb/gusb-self-test.c +++ b/gusb/gusb-self-test.c @@ -75,15 +75,13 @@ gusb_context_lookup_func (void) static void gusb_context_func (void) { - GUsbContext *ctx; GError *error = NULL; GPtrArray *array; guint old_number_of_devices; guint8 bus, address; GUsbDevice *device; - gchar *manufacturer; - gchar *product; guint i; + _cleanup_object_unref_ GUsbContext *ctx = NULL; ctx = g_usb_context_new (&error); g_assert_no_error (error); @@ -100,6 +98,8 @@ gusb_context_func (void) /* Print a list (also excercising various bits of g_usb_device) */ g_print ("\n"); for (i = 0; i < array->len; i++) { + _cleanup_free_ gchar *manufacturer = NULL; + _cleanup_free_ gchar *product = NULL; device = G_USB_DEVICE (g_ptr_array_index (array, i)); g_assert_cmpint (g_usb_device_get_vid (device), >, 0x0000); @@ -128,9 +128,6 @@ gusb_context_func (void) g_usb_device_get_pid (device), manufacturer ? manufacturer : "", product ? product : ""); - - g_free (manufacturer); - g_free (product); } g_ptr_array_unref (array); @@ -164,19 +161,17 @@ gusb_context_func (void) G_USB_DEVICE_ERROR_NO_DEVICE); g_assert (device == NULL); g_clear_error (&error); - - g_object_unref (ctx); } static void gusb_device_huey_func (void) { - GUsbContext *ctx; - GError *error = NULL; - GUsbDevice *device; gboolean ret; GCancellable *cancellable = NULL; const gchar request[8] = { 0x0e, 'G', 'r', 'M', 'b', 'k', 'e', 'd' }; + _cleanup_error_free_ GError *error = NULL; + _cleanup_object_unref_ GUsbContext *ctx = NULL; + _cleanup_object_unref_ GUsbDevice *device = NULL; ctx = g_usb_context_new (&error); g_assert_no_error (error); @@ -193,8 +188,7 @@ gusb_device_huey_func (void) error->domain == G_USB_DEVICE_ERROR && error->code == G_USB_DEVICE_ERROR_NO_DEVICE) { g_print ("No device detected!\n"); - g_error_free (error); - goto out; + return; } g_assert_no_error (error); g_assert (device != NULL); @@ -275,10 +269,6 @@ gusb_device_huey_func (void) ret = g_usb_device_close (device, &error); g_assert_no_error (error); g_assert (ret); - - g_object_unref (device); -out: - g_object_unref (ctx); } typedef struct { @@ -315,15 +305,14 @@ g_usb_test_button_pressed_cb (GObject *source_object, gpointer user_data) { gboolean ret; - GError *error = NULL; GUsbDeviceAsyncHelper *helper = (GUsbDeviceAsyncHelper *) user_data; + _cleanup_error_free_ GError *error = NULL; ret = g_usb_device_interrupt_transfer_finish (G_USB_DEVICE (source_object), res, &error); if (!ret) { g_error ("%s", error->message); - g_error_free (error); return; } @@ -337,13 +326,13 @@ g_usb_test_button_pressed_cb (GObject *source_object, static void gusb_device_munki_func (void) { - GError *error = NULL; - GUsbContext *ctx; - GUsbDevice *device; gboolean ret; GCancellable *cancellable = NULL; guint8 request[24]; GUsbDeviceAsyncHelper *helper; + _cleanup_error_free_ GError *error = NULL; + _cleanup_object_unref_ GUsbContext *ctx = NULL; + _cleanup_object_unref_ GUsbDevice *device = NULL; ctx = g_usb_context_new (&error); g_assert_no_error (error); @@ -360,8 +349,7 @@ gusb_device_munki_func (void) error->domain == G_USB_DEVICE_ERROR && error->code == G_USB_DEVICE_ERROR_NO_DEVICE) { g_print ("No device detected!\n"); - g_error_free (error); - goto out; + return; } g_assert_no_error (error); g_assert (device != NULL); @@ -458,10 +446,6 @@ gusb_device_munki_func (void) ret = g_usb_device_close (device, &error); g_assert_no_error (error); g_assert (ret); - - g_object_unref (device); -out: - g_object_unref (ctx); } int diff --git a/tools/gusb-main.c b/tools/gusb-main.c index bb8c3de..a870760 100644 --- a/tools/gusb-main.c +++ b/tools/gusb-main.c @@ -103,9 +103,9 @@ gusb_sort_command_name_cb (GUsbCmdItem **item1, GUsbCmdItem **item2) static void gusb_cmd_add (GPtrArray *array, const gchar *name, const gchar *description, GUsbCmdPrivateCb callback) { - gchar **names; - guint i; GUsbCmdItem *item; + guint i; + _cleanup_strv_free_ gchar **names = NULL; /* add each one */ names = g_strsplit (name, ",", -1); @@ -122,7 +122,6 @@ gusb_cmd_add (GPtrArray *array, const gchar *name, const gchar *description, GUs item->callback = callback; g_ptr_array_add (array, item); } - g_strfreev (names); } /** @@ -319,10 +318,10 @@ static gboolean gusb_cmd_watch (GUsbCmdPrivate *priv, gchar **values, GError **error) { gboolean ret = TRUE; - GPtrArray *devices; guint i; GUsbDevice *device; GMainLoop *loop; + _cleanup_ptrarray_unref_ GPtrArray *devices = NULL; devices = g_usb_context_get_devices (priv->usb_ctx); for (i = 0; i < devices->len; i++) { @@ -343,7 +342,6 @@ gusb_cmd_watch (GUsbCmdPrivate *priv, gchar **values, GError **error) g_main_loop_run (loop); g_main_loop_unref (loop); - g_ptr_array_unref (devices); return ret; } @@ -353,18 +351,15 @@ gusb_cmd_watch (GUsbCmdPrivate *priv, gchar **values, GError **error) static gboolean gusb_cmd_run (GUsbCmdPrivate *priv, const gchar *command, gchar **values, GError **error) { - gboolean ret = FALSE; guint i; GUsbCmdItem *item; - GString *string; + _cleanup_string_free_ GString *string = NULL; /* find command */ for (i = 0; i < priv->cmd_array->len; i++) { item = g_ptr_array_index (priv->cmd_array, i); - if (g_strcmp0 (item->name, command) == 0) { - ret = item->callback (priv, values, error); - goto out; - } + if (g_strcmp0 (item->name, command) == 0) + return item->callback (priv, values, error); } /* not found */ @@ -376,9 +371,7 @@ gusb_cmd_run (GUsbCmdPrivate *priv, const gchar *command, gchar **values, GError g_string_append_printf (string, " * %s\n", item->name); } g_set_error_literal (error, 1, 0, string->str); - g_string_free (string, TRUE); -out: - return ret; + return FALSE; } /** @@ -389,12 +382,11 @@ main (int argc, char *argv[]) { gboolean ret; gboolean verbose = FALSE; - gchar *cmd_descriptions = NULL; - gchar *options_help = NULL; - GError *error = NULL; gint retval = 0; GUsbCmdPrivate *priv; - + _cleanup_free_ gchar *cmd_descriptions = NULL; + _cleanup_free_ gchar *options_help = NULL; + _cleanup_error_free_ GError *error = NULL; const GOptionEntry options[] = { { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Show extra debugging information", NULL }, @@ -456,7 +448,6 @@ main (int argc, char *argv[]) ret = gusb_cmd_run (priv, argv[1], (gchar**) &argv[2], &error); if (!ret) { g_print ("%s\n", error->message); - g_error_free (error); retval = 1; goto out; } @@ -469,9 +460,5 @@ out: g_option_context_free (priv->context); g_slice_free (GUsbCmdPrivate, priv); } - - /* free state */ - g_free (options_help); - g_free (cmd_descriptions); return retval; } |