The Buddy System
This is more of a general programming question than a C specific question, but can anyone explain to me how exactly the binary buddy system works?
I.e., Two memory addresses: how can I tell if they are buddies or not given a size I am looking for? (128K, 256K, etc).
I know I need to use some sort of bit masking but I am unsure how to go about it.
I understand how it actually works, that's not the problem. It's how exactly to identify which blocks are buddies based on the memory address is what is confusing me.
I figured this out on my own, but just in case anyone ever has the same problem and searches the forums:
Say you have memory *offsets* of X and Y and you want to test to see if they are buddies of size S, where S must be a power of two. Buddies of the same size will only differ in 1 bit, so:
X xor S = Y
Y xor S = X
I say offset since you have to start at location 0 for this to actually work right.