[vlc-commits] input: avoid heap-allocation in vlc_poll_i11e() in most cases

Rémi Denis-Courmont git at videolan.org
Wed Jul 1 18:22:10 CEST 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Jun 30 23:52:51 2015 +0300| [4fe23efc97563970ab4575fc259d9d2b6accba1a] | committer: Rémi Denis-Courmont

input: avoid heap-allocation in vlc_poll_i11e() in most cases

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=4fe23efc97563970ab4575fc259d9d2b6accba1a
---

 src/misc/interrupt.c |   23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/misc/interrupt.c b/src/misc/interrupt.c
index f24ab34..c2a87d7 100644
--- a/src/misc/interrupt.c
+++ b/src/misc/interrupt.c
@@ -328,14 +328,23 @@ int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
 
     int ret;
 
-    struct pollfd *ufd = malloc((nfds + 1) * sizeof (*ufd));
-    if (unlikely(ufd == NULL))
-        return -1; /* ENOMEM */
+    if (likely(nfds < 255))
+    {   /* Fast path with stack allocation */
+        struct pollfd ufd[nfds + 1];
 
-    vlc_cleanup_push(free, ufd);
-    ret = vlc_poll_i11e_inner(fds, nfds, timeout, ctx, ufd);
-    vlc_cleanup_pop();
-    free(ufd);
+        ret = vlc_poll_i11e_inner(fds, nfds, timeout, ctx, ufd);
+    }
+    else
+    {   /* Slow path but poll() is slow with large nfds anyway. */
+        struct pollfd *ufd = malloc((nfds + 1) * sizeof (*ufd));
+        if (unlikely(ufd == NULL))
+            return -1; /* ENOMEM */
+
+        vlc_cleanup_push(free, ufd);
+        ret = vlc_poll_i11e_inner(fds, nfds, timeout, ctx, ufd);
+        vlc_cleanup_pop();
+        free(ufd);
+    }
     return ret;
 }
 



More information about the vlc-commits mailing list