[PHP-DEV] question about Zend MM

2012-03-01 Thread Adi Mutu


Hello,

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 1000 ( 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?

Thanks,

Re: [PHP-DEV] question about Zend MM

2012-03-01 Thread Gustavo Lopes

On Thu, 01 Mar 2012 17:22:23 +0100, Adi Mutu adi_mut...@yahoo.com 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 1000 (  
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] question about Zend MM

2012-03-01 Thread Adi Mutu



Ah, now i got it..so it can also return size, if size is directly a 
multiple of  ZEND_MM_ALIGNMENT.
I was convinced that it has to be stricly  than size, i guess i was wrong.



 From: Gustavo Lopes glo...@nebm.ist.utl.pt
To: Adi Mutu adi_mut...@yahoo.com; internals@lists.php.net 
Sent: Thursday, March 1, 2012 6:44 PM
Subject: Re: [PHP-DEV] question about Zend MM 
 
On Thu, 01 Mar 2012 17:22:23 +0100, Adi Mutu adi_mut...@yahoo.com 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 1000 ( 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

Re: [PHP-DEV] question about Zend MM

2012-03-01 Thread Richard Lynch
WILD GUESS ALERT!

I'm guessing that this is for byte-alignment on big-endian versus
little-endian...

So it's more like  and  as masks to flip-flop bytes by
some binary logic / magic.

The -1 is to wrap the byte to binary inversion.

You might want to grep the code and see how they are actually used.

That helps me a lot when I'm totally lost what the Magic Numbers are
used for.

Not knowing what the initial value of ZEND_MM_ALIGNMENT is in the
first place, this is just wild guess...

On Thu, March 1, 2012 10:22 am, Adi Mutu wrote:


 Hello,

 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 1000
 ( 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?

 Thanks,


-- 
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclickhosted_button_id=FS9NLTNEEKWBE



-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php