delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2026/09/03/13:17:39

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 683HHaHU2378152
Authentication-Results: delorie.com; dmarc=none (p=none dis=none) header.from=delorie.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=delorie.com
X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Authentication-Results: delorie.com; arc=none smtp.remote-ip=172.31.3.1
ARC-Seal: i=1; a=rsa-sha256; d=delorie.com; s=key; t=1788455856; cv=none; b=cLshLHdKH417osLEkceHsrLMoioIYTdu8vSb/rSIx8oPB8Zzx4cK2i9IeQIw0P1JrdVdlGef8QoLCWu4JjId+CV2avjc5vH6+UXLAqyn7Mf8ExLeNWbE7DLdpNfV6HM9BfzfEAiLkJHl7HbnIfR2VKCsFE7XOL5BApsfur8RwHQ=
ARC-Message-Signature: i=1; a=rsa-sha256; d=delorie.com; s=key; t=1788455856;
c=relaxed/simple; bh=xnD9Jn/26TgaHnKTxFbFPeR0lZ3Py5xS9B3vmuzIF54=;
h=From:To:Subject:Date:Message-ID:MIME-Version; b=AePFzY8u4F4ZBbAh+YJq9d+40+0viMf5RtAivJRIipGzxNxtzQAF9fOPFiQ5bCvsXjFrZIT9NFYYdpLK6U8aBb/PW2j9H0egVvv0l2WLJzFZpYzfH8wpaYX7vMo5PnirlgS2R81y74LxNEkYn7dttjbYWOTzcmowugUfTHcKpUA=
ARC-Authentication-Results: i=1; delorie.com; dmarc=none (p=none dis=none) header.from=delorie.com; spf=fail smtp.mailfrom=delorie.com
DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 683HHaTd2378134
Authentication-Results: delorie.com; dmarc=none (p=none dis=none) header.from=delorie.com
Authentication-Results: delorie.com; spf=fail smtp.mailfrom=delorie.com
X-Authentication-Warning: envy.delorie.com: dj set sender to dj AT delorie DOT com using -f
From: "DJ Delorie (dj AT delorie DOT com) [via djgpp-workers AT delorie DOT com]" <djgpp-workers AT delorie DOT com>
To: djgpp-workers AT delorie DOT com
Subject: Re: memalign broken (again)
In-Reply-To: <ea4e57b0-05ca-42cf-9112-47783ee8d73f@gmail.com>
(djgpp-workers AT delorie DOT com)
Date: Thu, 03 Sep 2026 13:17:05 -0400
Message-ID: <xn1pbaxnji.fsf@envy.delorie.com>
MIME-Version: 1.0
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id 683HHaTc2378138
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

"Frank Sapone (emoaddict15 AT gmail DOT com) [via djgpp-workers AT delorie DOT com]"
<djgpp-workers AT delorie DOT com> writes:
>> @@ -141,7 +142,9 @@ void *nmemalign(size_t alignment, size_t size)
>>               /* two or more chunks to release */
>>               DBGPRTM("  Complex case, release multiple chunks");
>>               DBGEOLN;
>> -            nfree(PTR(__nmalloc_split(&m, alignment - misalign)));
>> +            malign = (char *)(((ulong)minit + XTRA - 1) & ~(alignment - 1));
>> +            m1 = MEMBLKp(malign);
>> +            nfree(PTR(__nmalloc_split(&m, (ulong)m1 - (ulong)m)));
>>               return nrealloc(PTR(m), size);
>>            }
>>         } /* alignment > ALIGN */
> Bringing this up again.  I have had a small handful of complaints about 
> combinelo: memory fouled on some machines for my Quake DOS port.  This 
> was never tested and vetted, but the patch looks and sounds reasonable.  

I assume you *did* test it with your port?

My thoughts after years of fixing glibc malloc bugs... ;-)

If the minimum chunk size is 3 (2 overhead, 1 data), and you want to
align to something more than 3 chunks, the worst case is the front-extra
is slightly less than a chunk (2 units, in this case).  To fix that, you
have to move the aligned pointer by an extra "alignment", making it
2+alignment past the original allocation.

Example: alignment is 64, ALIGN is 8, size is 1024, and the returned
pointer is 0xff8 (chunk at 0xff8 - 0x10 = 0xfe8)

The next valid chunk after the above (if smallest split) would be
0x1000/0x1010 but 0x1010 is not sufficiently aligned.

The next aligned address after that is 0x1040 (chunk 0x1030, so 0x1030/0x1040)

So the needed adjustment would be 0x1030/0x1040 - 0xfe8/0xff8 = 0x48 (72).

malign in the above would be (0xff8 + (16+64) - 1) & ~ 63 or 0x1040
m1 would thus be 0x1040 - 0x10 = 0x1030.

(note: I did a full analysis before I remembered that XTRA included
"alignment" and was going to claim the patch was wrong, so my confidence
in this new analysis is not 100% ;)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2026   by DJ Delorie     Updated Sep 20 2026