Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 31 Jul 2020 19:22:16 +0200
From: Markus Wichmann <nullplan@....net>
To: musl@...ts.openwall.com
Subject: Re: When to reclaim pages in __bin_chunk?

On Fri, Jul 31, 2020 at 08:17:02AM +0800, Zhao Zhengyu wrote:
> Hello,
>
> When chunks are merged, we use "(curr_size + pre_size) ^ pre_size >
> pre_size" to decide whether to reclaim. I think this may be something
> related to performance, but I can’t prove it. I want to know the
> reason.
>
> Thank you!
>
> Zhengyu

I asked that same question a while ago. For one, this was in the old
malloc code, which is now usually no longer used. For two, this tries to
figure out if adding current size to previous size rolls over into a new
power of two. Usually, curr_size will be small and pre_size will be
large. Therefore, adding the two will not change much about the high
bits of pre_size, so due to the XOR operator, those bits will cancel out
and the result will be smaller than pre_size. However, if the sum does
roll over, then the sum has one bit set that isn't in pre_size and is
larger than all the ones in pre_size. So the XOR can't cancel that bit,
and the result of the XOR is greater than pre_size.

Fundamentally, it is an optimized version of (a_clz(curr_size +
pre_size) < a_clz(pre_size)).

Ciao,
Markus

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.