As Craze Fog already pointed out above, you have to differentiate between "allocating" physical (i.e. RAM frames), allocating virtual memory SPACES, and MAPPING virtual pages to physical frames.
- Layer 1 above allocates "physical" frames from RAM.
- Layer 2 above MAPS a virtual address region into physical frames. Input to this layer is ideally the LOGICAL address region that you want to map into physical frames. This layer doesn't "guess" where the logical region should be. You should provide this layer with the start/end addresses for the region you want to map.
For example: if you want to map 0xC8000000-0xC8FFFFFF into anonymous frames; you should pass these parameters to layer 2, and it will simply update the page tables for that region, and allocates anonymous frames for you.
- Now the job of layer 3 is pretty clear: it has a bitmap (or some sort of structure) that describe the mapping info from 0xC0000000 to 0xFFFFFFFF. If you want to "kmalloc()" an amount of contiguous pages, the layer would look into the bitmap, find a contiguous region that is not already mapped, call layer 2 to anonymously map the region, and finally update the bitmap and return a logical pointer.
You may want to check these:
->
https://en.wikipedia.org/wiki/Buddy_memory_allocation->
http://man7.org/linux/man-pages/man2/mmap.2.html