Help with complicated and strange magic
I was going through and commenting c++ source code to gain a better understanding of it, when I came across this range checking code. I would like to know what the maximum call to LargeAllocate would be and how or why range checking might be done in this way. Also if you see errors in my comments please let me know.
Thanks in advance
dword declaration:
Code:
typedef unsigned int dword;
code (it stretches 160 lengthwise so you may want to copy it into you favorite text editor first)
Code:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// MEMBER FUNCTION: idHeap::Allocate
//
// Allocate memory based off of the bytes needed. There is some funky magic here with checking ranges and I am not sure of the speed increase over
// the loss in readability
//
// ~~ GLOBAL VARIABLE: c_heapAllocRunningCount
// Gets incremented to keep the count of heap allocations current.
//
// ~~ MACRO: USE_LIBC_MALLOC
// Forces use of standard C allocation functions for debugging and preformance testing.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void* idHeap::Allocate(const dword bytes){
//Checks to see if bytes is a zero, if it is then there isn't anything to allocate
if(!bytes){ //FIX. COULD WE NOT JUST CHECK TO SEE IF IT WAS LESS THAN 1 HERE AND AVOID THE MAGIC LATER? if(bytes < 1){
return NULL;
}
//Increment the count of heap allocations
c_heapAllocRunningCount++;
//Allocate memory via our custom functions or standard C functions
#if USE_LIBC_MALLOC
return malloc(bytes);
#else
//Check if bytes is within the range 0 to 255 (it was checked earlier in the function to be non-zero so the desired range is actually 1 to 255). This is
//done by "not-ing" the value 255 (denoted by the ~) which is represented by the compiler as two's compliment -- meaning 255 in binary looks like 01111111.
//Not-ing it would give the value 10000000 (-256). So if you preform a bitwise "and" on the number 'bytes' then only values within the range 0 to 255
//would return 0. Also, 255 is the signed one byte integer maximum.
if(!(bytes & ~255)){
return SmallAllocate(bytes);
}
//Same as the previous check except that the desired range is 1 to the maximum value of a signed 2 byte integer.
if(!(bytes & ~32767)){
return MediumAllocate(bytes);
}
//This basically means that the unsigned 4 byte integer 'bytes' is greater than the range 1 to 32,767 (or over 15 bit in length). In turn that means bytes'
//is in the range 32,768 (short int maximum) to 4,294,967,295 (unsigned int maximum) or 4gb. However, apparently c++ has it so you cant have constant decimal
//numbers over 2,147,483,647 or 2gb in a function call or variable assignment -- so as of this revision I don't know what the maximum call to this
//would be (2gb or 4gb).
return LargeAllocate(bytes);
#endif
}
Edit: I think I made a mistake in my comments. Is it true that the constants (255 and 32767) are up-converted for comparison to the length of dword, and if so would it be unsigned or signed?