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 <info@bnoordhuis.nl>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
This commit is contained in:
cjihrig 2017-03-17 14:02:10 -04:00
parent fd7ce57f2b
commit e7a7ffb152

View File

@ -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;