Add type parameter to the list_for_each_entry() and _safe() macros
authorPete Batard <pbatard@gmail.com>
Mon, 15 Feb 2010 01:46:05 +0000 (19:46 -0600)
committerDaniel Drake <dan@reactivated.net>
Thu, 20 May 2010 22:35:30 +0000 (19:35 -0300)
typeof() is a GCC extension, not supported by target compilers such
as MSVC.

libusb/core.c
libusb/io.c
libusb/libusbi.h
libusb/os/darwin_usb.c
libusb/os/linux_usbfs.c

index 02ef427..716ca60 100644 (file)
@@ -560,7 +560,7 @@ struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
        struct libusb_device *ret = NULL;
 
        usbi_mutex_lock(&ctx->usb_devs_lock);
-       list_for_each_entry(dev, &ctx->usb_devs, list)
+       list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device)
                if (dev->session_data == session_id) {
                        ret = dev;
                        break;
index c7bfeb5..b38a7c7 100644 (file)
@@ -1132,7 +1132,7 @@ static int add_to_flying_list(struct usbi_transfer *transfer)
        }
 
        /* otherwise, find appropriate place in list */
-       list_for_each_entry(cur, &ctx->flying_transfers, list) {
+       list_for_each_entry(cur, &ctx->flying_transfers, list, struct usbi_transfer) {
                /* find first timeout that occurs after the transfer in question */
                struct timeval *cur_tv = &cur->timeout;
 
@@ -1331,7 +1331,7 @@ static int arm_timerfd_for_next_timeout(struct libusb_context *ctx)
 {
        struct usbi_transfer *transfer;
 
-       list_for_each_entry(transfer, &ctx->flying_transfers, list) {
+       list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
                struct timeval *cur_tv = &transfer->timeout;
 
                /* if we've reached transfers of infinite timeout, then we have no
@@ -1732,7 +1732,7 @@ static int handle_timeouts_locked(struct libusb_context *ctx)
 
        /* iterate through flying transfers list, finding all transfers that
         * have expired timeouts */
-       list_for_each_entry(transfer, &ctx->flying_transfers, list) {
+       list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
                struct timeval *cur_tv = &transfer->timeout;
 
                /* if we've reached transfers of infinite timeout, we're all done */
@@ -1803,7 +1803,7 @@ static int handle_events(struct libusb_context *ctx, struct timeval *tv)
        int timeout_ms;
 
        usbi_mutex_lock(&ctx->pollfds_lock);
-       list_for_each_entry(ipollfd, &ctx->pollfds, list)
+       list_for_each_entry(ipollfd, &ctx->pollfds, list, struct usbi_pollfd)
                nfds++;
 
        /* TODO: malloc when number of fd's changes, not on every poll */
@@ -1813,7 +1813,7 @@ static int handle_events(struct libusb_context *ctx, struct timeval *tv)
                return LIBUSB_ERROR_NO_MEM;
        }
 
-       list_for_each_entry(ipollfd, &ctx->pollfds, list) {
+       list_for_each_entry(ipollfd, &ctx->pollfds, list, struct usbi_pollfd) {
                struct libusb_pollfd *pollfd = &ipollfd->pollfd;
                int fd = pollfd->fd;
                i++;
@@ -2127,7 +2127,7 @@ API_EXPORTED int libusb_get_next_timeout(libusb_context *ctx,
        }
 
        /* find next transfer which hasn't already been processed as timed out */
-       list_for_each_entry(transfer, &ctx->flying_transfers, list) {
+       list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
                if (!(transfer->flags & USBI_TRANSFER_TIMED_OUT)) {
                        found = 1;
                        break;
@@ -2229,7 +2229,7 @@ void usbi_remove_pollfd(struct libusb_context *ctx, int fd)
 
        usbi_dbg("remove fd %d", fd);
        usbi_mutex_lock(&ctx->pollfds_lock);
-       list_for_each_entry(ipollfd, &ctx->pollfds, list)
+       list_for_each_entry(ipollfd, &ctx->pollfds, list, struct usbi_pollfd)
                if (ipollfd->pollfd.fd == fd) {
                        found = 1;
                        break;
@@ -2269,14 +2269,14 @@ API_EXPORTED const struct libusb_pollfd **libusb_get_pollfds(
        USBI_GET_CONTEXT(ctx);
 
        usbi_mutex_lock(&ctx->pollfds_lock);
-       list_for_each_entry(ipollfd, &ctx->pollfds, list)
+       list_for_each_entry(ipollfd, &ctx->pollfds, list, struct usbi_pollfd)
                cnt++;
 
        ret = calloc(cnt + 1, sizeof(struct libusb_pollfd *));
        if (!ret)
                goto out;
 
-       list_for_each_entry(ipollfd, &ctx->pollfds, list)
+       list_for_each_entry(ipollfd, &ctx->pollfds, list, struct usbi_pollfd)
                ret[i++] = (struct libusb_pollfd *) ipollfd;
        ret[cnt] = NULL;
 
@@ -2312,7 +2312,7 @@ void usbi_handle_disconnect(struct libusb_device_handle *handle)
        while (1) {
                usbi_mutex_lock(&HANDLE_CTX(handle)->flying_transfers_lock);
                to_cancel = NULL;
-               list_for_each_entry(cur, &HANDLE_CTX(handle)->flying_transfers, list)
+               list_for_each_entry(cur, &HANDLE_CTX(handle)->flying_transfers, list, struct usbi_transfer)
                        if (__USBI_TRANSFER_TO_LIBUSB_TRANSFER(cur)->dev_handle == handle) {
                                to_cancel = cur;
                                break;
index 3cf9882..9a573bb 100644 (file)
@@ -51,17 +51,18 @@ struct list_head {
  *     pos - A structure pointer has a "member" element
  *     head - list head
  *     member - the list_head element in "pos"
+ *     type - the type of the first parameter
  */
-#define list_for_each_entry(pos, head, member)                         \
-       for (pos = list_entry((head)->next, typeof(*pos), member);      \
-            &pos->member != (head);                                    \
-            pos = list_entry(pos->member.next, typeof(*pos), member))
-
-#define list_for_each_entry_safe(pos, n, head, member)                 \
-        for (pos = list_entry((head)->next, typeof(*pos), member),     \
-               n = list_entry(pos->member.next, typeof(*pos), member); \
-            &pos->member != (head);                                    \
-            pos = n, n = list_entry(n->member.next, typeof(*n), member))
+#define list_for_each_entry(pos, head, member, type)                   \
+       for (pos = list_entry((head)->next, type, member);                      \
+                &pos->member != (head);                                                                \
+                pos = list_entry(pos->member.next, type, member))
+
+#define list_for_each_entry_safe(pos, n, head, member, type)   \
+       for (pos = list_entry((head)->next, type, member),                      \
+                n = list_entry(pos->member.next, type, member);                \
+                &pos->member != (head);                                                                \
+                pos = n, n = list_entry(n->member.next, type, member))
 
 #define list_empty(entry) ((entry)->next == (entry))
 
index 0e65385..00febbe 100644 (file)
@@ -233,7 +233,7 @@ static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) {
     IOObjectRelease (device);
 
     usbi_mutex_lock(&ctx->open_devs_lock);
-    list_for_each_entry(handle, &ctx->open_devs, list) {
+    list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) {
       dpriv = (struct darwin_device_priv *)handle->dev->os_priv;
 
       /* the device may have been opened several times. write to each handle's event descriptor */
@@ -1400,7 +1400,7 @@ static int op_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds
       continue;
 
     num_ready--;
-    list_for_each_entry(handle, &ctx->open_devs, list) {
+    list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) {
       hpriv =  (struct darwin_device_handle_priv *)handle->os_priv;
       if (hpriv->fds[0] == pollfd->fd)
        break;
index f89a87d..cf69286 100644 (file)
@@ -2114,7 +2114,7 @@ static int op_handle_events(struct libusb_context *ctx,
                        continue;
 
                num_ready--;
-               list_for_each_entry(handle, &ctx->open_devs, list) {
+               list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) {
                        hpriv =  __device_handle_priv(handle);
                        if (hpriv->fd == pollfd->fd)
                                break;