From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) by gnuweeb.org (Postfix) with ESMTPSA id 7F4B87E2C4 for ; Sun, 20 Mar 2022 15:50:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647791443; bh=DrWgZ38PquuHSGxjZUS3cpQF4U4/ehYfTH8o3ux34qI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=BS61PPnLUTTd6D5sqKoO0MxLinu4DP6IXwzoox1fSTuSaZgCp9r4HCJoxAZ71prDz sLojXHFpE3xWba+b4ELIC1v3KDeflTumEEgoitQ8mHV13Z16oXLD4UfDvYPl3gDFwk VSXP/PoctDiVsV1j6nBCq7a05+vFFIGUzUE0QP9ZQY1Ly9XLFN0djOWMLq0aWlSGzJ kuRbzRhiE41YqqHLx6p9a4CZIh3lTbMlFR0f6eldYGIlCVeXlZcMPAzybRv7xjoIQv G4j5TQMlVLb/QxNP8AuS+9P4ugU3tJWRx9BQqI6OFHWNrojz8/egAU9IBOU3b2Vg4G 4bdii8ggDCQcw== Received: by mail-lf1-f53.google.com with SMTP id k21so4683748lfe.4 for ; Sun, 20 Mar 2022 08:50:43 -0700 (PDT) X-Gm-Message-State: AOAM532b6tLsFiFWN8v28Qzg2fn40McucNn7hAUIR6cXfh8pbmNtM/RF AXQ7n6g07uz1Jgw+nWSkkYbYenH46Ef3N65Qoyw= X-Google-Smtp-Source: ABdhPJx4soqBEdPe9BsBWRodarFmFvffwnL8gJNGNWxLdPiwvnRyf9uPClxnIIPxrfeO5I1WrJjrhE+gwbDK5jDzzi0= X-Received: by 2002:a05:6512:32c8:b0:44a:27fd:cd06 with SMTP id f8-20020a05651232c800b0044a27fdcd06mr1979767lfg.239.1647791441629; Sun, 20 Mar 2022 08:50:41 -0700 (PDT) MIME-Version: 1.0 References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> <20220320093750.159991-6-ammarfaizi2@gnuweeb.org> In-Reply-To: <20220320093750.159991-6-ammarfaizi2@gnuweeb.org> From: Alviro Iskandar Setiawan Date: Sun, 20 Mar 2022 22:50:30 +0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH v1 5/6] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()` To: Ammar Faizi Cc: Willy Tarreau , "Paul E. McKenney" , Nugraha , Linux Kernel Mailing List , "GNU/Weeb Mailing List" Content-Type: text/plain; charset="UTF-8" List-Id: 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