unix: use system allocator for scandir()

On unix, scandir() uses the system allocator to allocate memory.
This commit releases the memory with free() instead of uv__free().
uv__free() is still used on Windows, which uses uv__malloc() to
request the memory.

Fixes: https://github.com/libuv/libuv/issues/873
PR-URL: https://github.com/libuv/libuv/pull/874
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
cjihrig 2016-05-16 11:34:36 -04:00
parent a1bd1ee803
commit 4fa89fbc61
2 changed files with 16 additions and 5 deletions

View File

@ -370,9 +370,10 @@ out:
if (dents != NULL) {
int i;
/* Memory was allocated using the system allocator, so use free() here. */
for (i = 0; i < n; i++)
uv__free(dents[i]);
uv__free(dents);
free(dents[i]);
free(dents);
}
errno = saved_errno;

View File

@ -483,6 +483,16 @@ static unsigned int* uv__get_nbufs(uv_fs_t* req) {
#endif
}
/* uv_fs_scandir() uses the system allocator to allocate memory on non-Windows
* systems. So, the memory should be released using free(). On Windows,
* uv__malloc() is used, so use uv__free() to free memory.
*/
#ifdef _WIN32
# define uv__fs_scandir_free uv__free
#else
# define uv__fs_scandir_free free
#endif
void uv__fs_scandir_cleanup(uv_fs_t* req) {
uv__dirent_t** dents;
@ -492,7 +502,7 @@ void uv__fs_scandir_cleanup(uv_fs_t* req) {
if (*nbufs > 0 && *nbufs != (unsigned int) req->result)
(*nbufs)--;
for (; *nbufs < (unsigned int) req->result; (*nbufs)++)
uv__free(dents[*nbufs]);
uv__fs_scandir_free(dents[*nbufs]);
}
@ -506,11 +516,11 @@ int uv_fs_scandir_next(uv_fs_t* req, uv_dirent_t* ent) {
/* Free previous entity */
if (*nbufs > 0)
uv__free(dents[*nbufs - 1]);
uv__fs_scandir_free(dents[*nbufs - 1]);
/* End was already reached */
if (*nbufs == (unsigned int) req->result) {
uv__free(dents);
uv__fs_scandir_free(dents);
req->ptr = NULL;
return UV_EOF;
}