From e7a7ffb1524326d6a431f219b07278ceaebac915 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 17 Mar 2017 14:02:10 -0400 Subject: [PATCH] win: free uv__loops once empty This commit moves the allocation of uv__loops into uv__loops_add() and frees all of the memory associated with uv__loops in uv__loops_remove() once the loop count reaches zero. Fixes: https://github.com/libuv/libuv/issues/1125 Fixes: https://github.com/libuv/libuv/issues/1252 PR-URL: https://github.com/libuv/libuv/pull/1262 Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno --- src/win/core.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/win/core.c b/src/win/core.c index cd480034..cc841872 100644 --- a/src/win/core.c +++ b/src/win/core.c @@ -85,11 +85,6 @@ static uv_mutex_t uv__loops_lock; static void uv__loops_init() { uv_mutex_init(&uv__loops_lock); - uv__loops = uv__calloc(UV__LOOPS_CHUNK_SIZE, sizeof(uv_loop_t*)); - if (!uv__loops) - uv_fatal_error(ERROR_OUTOFMEMORY, "uv__malloc"); - uv__loops_size = 0; - uv__loops_capacity = UV__LOOPS_CHUNK_SIZE; } static int uv__loops_add(uv_loop_t* loop) { @@ -138,6 +133,13 @@ static void uv__loops_remove(uv_loop_t* loop) { uv__loops[uv__loops_size - 1] = NULL; --uv__loops_size; + if (uv__loops_size == 0) { + uv__loops_capacity = 0; + uv__free(uv__loops); + uv__loops = NULL; + goto loop_removed; + } + /* If we didn't grow to big skip downsizing */ if (uv__loops_capacity < 4 * UV__LOOPS_CHUNK_SIZE) goto loop_removed;