Skip to content

Commit

Permalink
src: add uv__reallocf()
Browse files Browse the repository at this point in the history
Modeled after FreeBSD's `reallocf(3)`: a version of `realloc(3)` that
frees the memory when reallocation fails, simplifying error handling in
many cases.

PR-URL: libuv#2735
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
bnoordhuis authored and cjihrig committed Mar 15, 2020
1 parent 506e4be commit 1bcfbfd
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/unix/core.c
Expand Up @@ -834,8 +834,8 @@ static void maybe_resize(uv_loop_t* loop, unsigned int len) {
}

nwatchers = next_power_of_two(len + 2) - 2;
watchers = uv__realloc(loop->watchers,
(nwatchers + 2) * sizeof(loop->watchers[0]));
watchers = uv__reallocf(loop->watchers,
(nwatchers + 2) * sizeof(loop->watchers[0]));

if (watchers == NULL)
abort();
Expand Down
9 changes: 2 additions & 7 deletions src/unix/fs.c
Expand Up @@ -666,7 +666,6 @@ static ssize_t uv__fs_readlink(uv_fs_t* req) {
ssize_t maxlen;
ssize_t len;
char* buf;
char* newbuf;

#if defined(_POSIX_PATH_MAX) || defined(PATH_MAX)
maxlen = uv__fs_pathmax_size(req->path);
Expand Down Expand Up @@ -710,14 +709,10 @@ static ssize_t uv__fs_readlink(uv_fs_t* req) {

/* Uncommon case: resize to make room for the trailing nul byte. */
if (len == maxlen) {
newbuf = uv__realloc(buf, len + 1);
buf = uv__reallocf(buf, len + 1);

if (newbuf == NULL) {
uv__free(buf);
if (buf == NULL)
return -1;
}

buf = newbuf;
}

buf[len] = '\0';
Expand Down
6 changes: 2 additions & 4 deletions src/unix/openbsd.c
Expand Up @@ -61,7 +61,6 @@ void uv_loadavg(double avg[3]) {
int uv_exepath(char* buffer, size_t* size) {
int mib[4];
char **argsbuf = NULL;
char **argsbuf_tmp;
size_t argsbuf_size = 100U;
size_t exepath_size;
pid_t mypid;
Expand All @@ -73,10 +72,9 @@ int uv_exepath(char* buffer, size_t* size) {
mypid = getpid();
for (;;) {
err = UV_ENOMEM;
argsbuf_tmp = uv__realloc(argsbuf, argsbuf_size);
if (argsbuf_tmp == NULL)
argsbuf = uv__reallocf(argsbuf, argsbuf_size);
if (argsbuf == NULL)
goto out;
argsbuf = argsbuf_tmp;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = mypid;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/os390-syscalls.c
Expand Up @@ -128,7 +128,7 @@ static void maybe_resize(uv__os390_epoll* lst, unsigned int len) {
}

newsize = next_power_of_two(len);
newlst = uv__realloc(lst->items, newsize * sizeof(lst->items[0]));
newlst = uv__reallocf(lst->items, newsize * sizeof(lst->items[0]));

if (newlst == NULL)
abort();
Expand Down
2 changes: 1 addition & 1 deletion src/unix/posix-poll.c
Expand Up @@ -61,7 +61,7 @@ static void uv__pollfds_maybe_resize(uv_loop_t* loop) {
return;

n = loop->poll_fds_size ? loop->poll_fds_size * 2 : 64;
p = uv__realloc(loop->poll_fds, n * sizeof(*loop->poll_fds));
p = uv__reallocf(loop->poll_fds, n * sizeof(*loop->poll_fds));
if (p == NULL)
abort();

Expand Down
11 changes: 11 additions & 0 deletions src/uv-common.c
Expand Up @@ -100,6 +100,17 @@ void* uv__realloc(void* ptr, size_t size) {
return NULL;
}

void* uv__reallocf(void* ptr, size_t size) {
void* newptr;

newptr = uv__realloc(ptr, size);
if (newptr == NULL)
if (size > 0)
uv__free(ptr);

return newptr;
}

int uv_replace_allocator(uv_malloc_func malloc_func,
uv_realloc_func realloc_func,
uv_calloc_func calloc_func,
Expand Down
1 change: 1 addition & 0 deletions src/uv-common.h
Expand Up @@ -322,5 +322,6 @@ char *uv__strndup(const char* s, size_t n);
void* uv__malloc(size_t size);
void uv__free(void* ptr);
void* uv__realloc(void* ptr, size_t size);
void* uv__reallocf(void* ptr, size_t size);

#endif /* UV_COMMON_H_ */

0 comments on commit 1bcfbfd

Please sign in to comment.