Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58417 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71749 invoked from network); 1 Mar 2012 16:44:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Mar 2012 16:44:59 -0000 Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.21 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.21 smtp1.ist.utl.pt Linux 2.6 Received: from [193.136.128.21] ([193.136.128.21:53095] helo=smtp1.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 05/9D-46815-987AF4F4 for ; Thu, 01 Mar 2012 11:44:58 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id E6D36700049D; Thu, 1 Mar 2012 16:44:53 +0000 (WET) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp1.ist.utl.pt ([127.0.0.1]) by localhost (smtp1.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id ORH07Yzakcbc; Thu, 1 Mar 2012 16:44:53 +0000 (WET) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp1.ist.utl.pt (Postfix) with ESMTP id A60257000490; Thu, 1 Mar 2012 16:44:53 +0000 (WET) Received: from slws007.slhq.int (a79-168-248-114.cpe.netcabo.pt [79.168.248.114]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id 31EC52008821; Thu, 1 Mar 2012 16:44:53 +0000 (WET) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: "Adi Mutu" , internals@lists.php.net References: <1330618943.51670.YahooMailNeo@web43505.mail.sp1.yahoo.com> Date: Thu, 01 Mar 2012 17:44:52 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Organization: =?utf-8?Q?N=C3=BAcleo_de_Eng=2E_Biom=C3=A9di?= =?utf-8?Q?ca_do_I=2ES=2ET=2E?= Message-ID: In-Reply-To: <1330618943.51670.YahooMailNeo@web43505.mail.sp1.yahoo.com> User-Agent: Opera Mail/11.61 (Win32) Subject: Re: [PHP-DEV] question about Zend MM From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Thu, 01 Mar 2012 17:22:23 +0100, Adi Mutu wrote: > I want to understand how Zend MM works, so i'm looking trought the > sources and i see this: > > #define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1) #define > ZEND_MM_ALIGNED_SIZE(size) (((size) + ZEND_MM_ALIGNMENT - 1) & > ZEND_MM_ALIGNMENT_MASK) > > > I understand that the first define will create something like 11111000 ( > it will clear last 3 bits) > but what does the 2nd define? Before clearing the last 3 bytes why does > it add ZEND_MM_ALIGNMENT- 1 > to size? > It basically just rounds to the next multiple of ZEND_MM_ALIGNMENT, assuming ZEND_MM_ALIGNMENT is a power of 2. ZEND_MM_ALIGNMENT is a power of 2, so it has 1 bit set. Subtracting 1 will zero that bit and and flip on all the other less significant bits. Then negating flips the bits so that now the bits less significant than the log2(ZEND_MM_ALIGNMENT)-th will be zero and the others will be one. ZEND_MM_ALIGNED_SIZE adds ZEND_MM_ALIGNMENT - 1 and applies the mask. The effect is that the result will be >= size and it will be a multiple of ZEND_MM_ALIGNMENT (in particular the smallest multiple of ZEND_MM_ALIGNMENT that's >= the argument) because the bits less significant than the log2(ZEND_MM_ALIGNMENT)-th will be zero. "a & n-1" is the same as "a mod n" with n being a power of 2. So if a & ZEND_MM_ALIGNMENT - 1 == 0 then a mod ZEND_MM_ALIGNMENT == 0 and a is a multiple of ZEND_MM_ALIGNMENT. -- Gustavo Lopes