I'm sorry I couldn't reply earlier to this but I've been really busy.
This binary math is part of a subject I had in college not long ago. So I may be of help here indeed (I hope at least).
About that if statement, it's as Koshling said, but I can't see it being broken.
First it avoids negative numbers with iNumSlots > 0, which is good for its '&' bitwise operation later (2's complement notation for negative integers could potentially make the bitwise '&' operator run erroneously).
Then it guarantees (iNumSlots & (iNumSlots - 1)) is different from 0. This means only a number which isn't a power of 2 will trigger this 'if', because the only case where a [number & its predecessor] will yield 0, is when that number is a power of 2.
Eg.: The number 8
01000
00111 &
00000 = 0
Eg.: The number 14
01110
01101 &
01100 = 12
With some examples it becomes clear that only a power of 2 would yield 0 in this situation.
Then, when it enters the 'if', the only thing it does is finding the highest bit from the binary representation of iNumSlots and then it rounds up the iNumSlots to the next power of 2.
Eg.: The number 14 again
If we had chosen 14 as iNumSlots then the if statement would be triggered and iNumSlots would become 16, the next closest power of 2
What I find most curious is this just after the if statement:
Code:
assert(((iNumSlots - 1) & iNumSlots) == 0);
assert((m_iNumSlots <= FLTA_MAX_BUCKETS) && "FFreeListTrashArray<T>::init() size too large");
uninit();
m_iNumSlots = iNumSlots;
The first assert is ok, it's exactly the one which guarantees iNumSlots is a power of 2, but the second one seems wrong, as it now compares another variable m_iNumSlots to the Max possible size (FLTA_MAX_BUCKETS) instead of iNumSlots, and then it makes m_iNumSlots get the value of iNumSlots. To me it's possible that here a number which is higher then FLTA_MAX_BUCKETS can enter as m_iNumSlots for the rest of the function.
I've been reading the rest of the file but now I need to sleep, so I hope this was helpful and I'm eager to continue discovering what is happening in these functions.