Add move-function for sc_usb_device

Add a function to "move" a sc_usb_device into another instance.

This will avoid unnecessary copies.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
This commit is contained in:
Romain Vimont 2022-02-05 19:07:53 +01:00
parent 8c50342fb2
commit b88c4aa75e
2 changed files with 24 additions and 1 deletions

View file

@ -72,12 +72,23 @@ accept_device(libusb_device *device, const char *serial,
void void
sc_usb_device_destroy(struct sc_usb_device *usb_device) { sc_usb_device_destroy(struct sc_usb_device *usb_device) {
if (usb_device->device) {
libusb_unref_device(usb_device->device); libusb_unref_device(usb_device->device);
}
free(usb_device->serial); free(usb_device->serial);
free(usb_device->manufacturer); free(usb_device->manufacturer);
free(usb_device->product); free(usb_device->product);
} }
void
sc_usb_device_move(struct sc_usb_device *dst, struct sc_usb_device *src) {
*dst = *src;
src->device = NULL;
src->serial = NULL;
src->manufacturer = NULL;
src->product = NULL;
}
void void
sc_usb_devices_destroy_all(struct sc_usb_device *usb_devices, size_t count) { sc_usb_devices_destroy_all(struct sc_usb_device *usb_devices, size_t count) {
for (size_t i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {

View file

@ -40,6 +40,18 @@ struct sc_usb_device {
void void
sc_usb_device_destroy(struct sc_usb_device *usb_device); sc_usb_device_destroy(struct sc_usb_device *usb_device);
/**
* Move src to dst
*
* After this call, the content of src is undefined, except that
* sc_usb_device_destroy() can be called.
*
* This is useful to take a device from a list that will be destroyed, without
* making unnecessary copies.
*/
void
sc_usb_device_move(struct sc_usb_device *dst, struct sc_usb_device *src);
void void
sc_usb_devices_destroy_all(struct sc_usb_device *usb_devices, size_t count); sc_usb_devices_destroy_all(struct sc_usb_device *usb_devices, size_t count);