From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id B8EB5383CCE3 for ; Thu, 1 Sep 2022 19:36:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B8EB5383CCE3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662061013; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OXuce74I3ye+mkKCCgdJaHYut33CSBkERgWPnLHW3g0=; b=iuUOX2j29+fqw+Yw3vSniWzuvl00TJMv3OpCUx/EcpghjzXok6HhUzkHhhEokKQo+P8iKi I0tZtOuJ9QdqxZc2ySJiolEXQ4TAeZFP94WHtU4v7ANveTk8ZJdLgHoqVL4yx3heC6P/3y QXetD2Vw06hjfFY962fdda1ZjBRRSvI= Received: from mail-yb1-f200.google.com (mail-yb1-f200.google.com [209.85.219.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-648-qvuU9RvEOLiSz3hFgieKKA-1; Thu, 01 Sep 2022 15:36:52 -0400 X-MC-Unique: qvuU9RvEOLiSz3hFgieKKA-1 Received: by mail-yb1-f200.google.com with SMTP id f12-20020a25b6cc000000b0069a9e36de26so93462ybm.16 for ; Thu, 01 Sep 2022 12:36:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=OXuce74I3ye+mkKCCgdJaHYut33CSBkERgWPnLHW3g0=; b=X+D+o2YJvkIqEVJdLWGVm65FzQLG2SMdPy18fmXLKi1xGd4/oxxzSm1NUiFRninOo8 Kh5n6N6znWKxy9dWHJPSdZQB2traNbfi+9IKw3j9N0xoGWlHmgdtLDiFFEFu/eK9DYnO byEwO29Bsbp/Jv0NVmGpYGe0WLggDZTjYJHm20za7+Rr7KcidDv+rXldbLSK4NPeQOZn /SerutB5rYqgYWwXHS+qYf8MtGy3V0pNKjeJozlYrGdafnVqg2sPoWJAmpPAN1V/Q8Nx yx2zVaNx370qMJHRfZ5/g6uTutFlQfq4/XWhXV7eZb4Xre3VpLqyacnQCPEmV57UFRvg igDw== X-Gm-Message-State: ACgBeo0kh2cZ1DVBt7fmhDLpHeyXGehexxF/eqeq5o+D0eQRUbhK4R7R JwE3Nj/dqMm1d4yAfszHY/kbigd8T8LS7IRQKkJdkw1qkA9DyeUPrZH1O71h5U0RnBAxO30XyjW fUM4RF1vbTH6+dgY39DllsoxJNtOP9to= X-Received: by 2002:a81:5a46:0:b0:341:4148:1e8 with SMTP id o67-20020a815a46000000b00341414801e8mr16322654ywb.145.1662061011723; Thu, 01 Sep 2022 12:36:51 -0700 (PDT) X-Google-Smtp-Source: AA6agR79HV8Z3xrvE14CjZ8Om7aVAGT4GCJK7ECvhAlR+vhcxKuHvzQgpUZ6U5/aEjxZnwaP9jlOmNhzDt5Fn/gCoY0= X-Received: by 2002:a81:5a46:0:b0:341:4148:1e8 with SMTP id o67-20020a815a46000000b00341414801e8mr16322648ywb.145.1662061011496; Thu, 01 Sep 2022 12:36:51 -0700 (PDT) MIME-Version: 1.0 References: <20220830135625.2247198-1-torbjorn.svensson@foss.st.com> <20220830135625.2247198-2-torbjorn.svensson@foss.st.com> In-Reply-To: From: Jeff Johnston Date: Thu, 1 Sep 2022 15:36:40 -0400 Message-ID: Subject: Re: [PATCH 2/2] Don't allocate another header when merging chunks To: Torbjorn SVENSSON Cc: Newlib X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Sep 2022 19:36:55 -0000 Hi Torbjorn, I was wrong. I somehow had it in my head that MINCHUNK would be checking for a minimal block size. Please ignore my comment. -- Jeff j. On Thu, Sep 1, 2022 at 3:04 PM Torbjorn SVENSSON < torbjorn.svensson@foss.st.com> wrote: > Hi Jeff, > > Thanks for the review. > > No, I don't think the MALLOC_MINCHUNK should apply as I think is to > ensure that there is enough room for the chunk header. > > /* size of smallest possible chunk. A memory piece smaller than this size > * won't be able to create a chunk */ > #define MALLOC_MINCHUNK (CHUNK_OFFSET + MALLOC_PADDING + MALLOC_MINSIZE) > > > In this particular case, there is already a chunk, but it's a bit too > small to fit the requested size to malloc(). As a result, it should be > the requested size minus the size for the last chunk minus the size of > the chunk header. The remaining number of bytes is the require memory to > create a chunk (user data + chunk header) that is just the right size. > > Think of it like this: > > If there is enough free heap, then malloc(100) would just call > _sbrk(108) and return that space. > > If that area is then free'ed, and a following malloc(200) is called, > then there are 3 scenarios: > > a) there is enough free heap, so it will be like returning the space > returned by _sbrk(208). > > b) these is not enough free heap, and the last chunk is still in use. In > this case, malloc would return 0 and errno should be ENOMEM. > > c) there is not enough free heap, but the last chunk is no used, thus it > could be extended. If the the chunk was 108 bytes including the header, > then _sbrk(200-108) would be needed to create a single chunk that is 200 > bytes + 8 bytes for the header. > If you look for the extreme, then the last chunk size could be 1 byte > less than what is requested of malloc, like the scenario above. The last > chunk size is 100 bytes (plus header) and the application calls > malloc(101). Then it would not make sense to apply MALLOC_MINCHUNK, as > that would call _sbrk(MALLOC_MINCHUNK) rather than _sbrk(1). In this > case, wasting MALLOC_MINCHUNK-1 bytes as they will never be access by > the application. > > > Does this make sense or am I overthinking this? > > Kind regards, > Torbj=C3=B6rn > > Ps. Header size for a chunk on arm-none-eabi is 8 bytes, for other > targets, the chunk header can be of different size, but I assumed > arm-none-eabi in my comment above to make it more clear. > > Ps2. _sbrk and malloc is obviously more complicated than my example > above, but I tried to reduce the complexity to the involved logic for > extending a chunk size. > > > > > On 2022-09-01 20:44, Jeff Johnston wrote: > > I think that the check for MALLOC_MINCHUNK should still apply. Do you > > agree? > > > > -- Jeff J. > > > > On Tue, Aug 30, 2022 at 9:57 AM Torbj=C3=B6rn SVENSSON > > > > > wrote: > > > > In the nano version of malloc, when the last chunk is to be extende= d, > > there is no need to acount for the header again as it's already tak= en > > into account in the overall "alloc_size" at the beginning of the > > function. > > > > Contributed by STMicroelectronics > > > > Signed-off-by: Torbj=C3=B6rn SVENSSON > > > > --- > > newlib/libc/stdlib/nano-mallocr.c | 4 ---- > > 1 file changed, 4 deletions(-) > > > > diff --git a/newlib/libc/stdlib/nano-mallocr.c > > b/newlib/libc/stdlib/nano-mallocr.c > > index 43eb20e07..b2273ba60 100644 > > --- a/newlib/libc/stdlib/nano-mallocr.c > > +++ b/newlib/libc/stdlib/nano-mallocr.c > > @@ -328,10 +328,6 @@ void * nano_malloc(RARG malloc_size_t s) > > /* The last free item has the heap end as neighbou= r. > > * Let's ask for a smaller amount and merge */ > > alloc_size -=3D p->size; > > - alloc_size =3D ALIGN_SIZE(alloc_size, CHUNK_ALIGN);= /* > > size of aligned data load */ > > - alloc_size +=3D MALLOC_PADDING; /* padding */ > > - alloc_size +=3D CHUNK_OFFSET; /* size of chunk head= */ > > - alloc_size =3D MAX(alloc_size, MALLOC_MINCHUNK); > > > > if (sbrk_aligned(RCALL alloc_size) !=3D (void *)-1= ) > > { > > -- > > 2.25.1 > > > >