# question about Zend MM

- Thread: https://externals.io/message/58416
- Original archive: https://news-web.php.net/php.internals/58416
- Started by Adi Mutu on 2012-03-01T16:22:23+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #58416 — Adi Mutu — 2012-03-01T16:22:23+00:00

URL: https://externals.io/message/58416#58416

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

Thanks,

---

## #58417 — Gustavo Lopes — 2012-03-01T16:44:52+00:00

URL: https://externals.io/message/58416#58417 · In reply to: #58416

> 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

---

## #58418 — Adi Mutu — 2012-03-01T17:26:05+00:00

URL: https://externals.io/message/58416#58418 · In reply to: #58417

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 <glopes@nebm.ist.utl.pt>
To: Adi Mutu <adi_mutu06@yahoo.com>; internals@lists.php.net 
Sent: Thursday, March 1, 2012 6:44 PM
Subject: Re: [PHP-DEV] question about Zend MM 
 


> 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

---

## #58425 — Richard Lynch — 2012-03-01T21:35:34+00:00

URL: https://externals.io/message/58416#58425 · In reply to: #58416

WILD GUESS ALERT!

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

So it's more like 11110000 and 00001111 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...


>
>
> 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 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?
>
> Thanks,


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