[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAOG64qMoDjLer_OgRn2edGLMLMVfA5Lv3CZSU2VeOkgzPWNVUA@mail.gmail.com>
Date: Sun, 20 Mar 2022 22:50:30 +0700
From: Alviro Iskandar Setiawan <alviro.iskandar@...weeb.org>
To: Ammar Faizi <ammarfaizi2@...weeb.org>
Cc: Willy Tarreau <w@....eu>, "Paul E. McKenney" <paulmck@...nel.org>,
Nugraha <richiisei@...il.com>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
"GNU/Weeb Mailing List" <gwml@...r.gnuweeb.org>
Subject: Re: [RFC PATCH v1 5/6] tools/nolibc/stdlib: Implement `malloc()`,
`calloc()`, `realloc()` and `free()`
On Sun, Mar 20, 2022 at 4:37 PM Ammar Faizi wrote:
> +void *realloc(void *old_ptr, size_t new_size)
> +{
> + struct nolibc_heap *heap;
> + void *ret;
> +
> + if (!old_ptr)
> + return malloc(new_size);
> +
> + ret = malloc(new_size);
> + if (__builtin_expect(!ret, 0))
> + return NULL;
> +
> + heap = container_of(old_ptr, struct nolibc_heap, user_p);
> + memcpy(ret, heap->user_p, heap->len);
> + munmap(heap, heap->len);
> + return ret;
> +}
This better be simplified like this, so only have 1 malloc() call that
applies to both branches.
void *realloc(void *old_ptr, size_t new_size)
{
struct nolibc_heap *heap;
void *ret;
ret = malloc(new_size);
if (__builtin_expect(!ret, 0))
return NULL;
if (!old_ptr)
return ret;
heap = container_of(old_ptr, struct nolibc_heap, user_p);
memcpy(ret, heap->user_p, heap->len);
munmap(heap, heap->len);
return ret;
}
-- Viro
Powered by blists - more mailing lists