From c1ff7cc6aab22a395c93bb23d75a95e73c134929 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 11 Nov 2017 12:02:09 +0100 Subject: [PATCH] unix: avoid malloc() call in uv_spawn() The stdio count for the new process is almost always a low number that we can allocate on the stack instead of the heap. PR-URL: https://github.com/libuv/libuv/pull/1626 Reviewed-By: Colin Ihrig Reviewed-By: Santiago Gimeno --- src/unix/process.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/unix/process.c b/src/unix/process.c index 80b9686e..9842710d 100644 --- a/src/unix/process.c +++ b/src/unix/process.c @@ -419,6 +419,7 @@ int uv_spawn(uv_loop_t* loop, return -ENOSYS; #else int signal_pipe[2] = { -1, -1 }; + int pipes_storage[8][2]; int (*pipes)[2]; int stdio_count; ssize_t r; @@ -443,7 +444,10 @@ int uv_spawn(uv_loop_t* loop, stdio_count = 3; err = -ENOMEM; - pipes = uv__malloc(stdio_count * sizeof(*pipes)); + pipes = pipes_storage; + if (stdio_count > (int) ARRAY_SIZE(pipes_storage)) + pipes = uv__malloc(stdio_count * sizeof(*pipes)); + if (pipes == NULL) goto error; @@ -548,7 +552,9 @@ int uv_spawn(uv_loop_t* loop, process->pid = pid; process->exit_cb = options->exit_cb; - uv__free(pipes); + if (pipes != pipes_storage) + uv__free(pipes); + return exec_errorno; error: @@ -562,7 +568,9 @@ error: if (pipes[i][1] != -1) uv__close_nocheckstdio(pipes[i][1]); } - uv__free(pipes); + + if (pipes != pipes_storage) + uv__free(pipes); } return err;