Quote:
Originally Posted by estabroo
get a 128K size chunk and then use a pointer and some math to give you the address of the first 64K aligned spot in the chunk assign the pointer to that spot and your good to go
|
I don't think skaushal_lk said that the desired size is 64KB, just that the desired alignment is 64K. The size might be far larger than 64KB.
But your suggestion contains the basic idea of one (somewhat wasteful) way to solve the problem:
If you request a very big chunk from malloc, the result would be 4K aligned. If you request a chunk that is 60KB larger than the amount you actually want, then the first 64K aligned address within that chunk (which can be computed easily with pointer masking) is the start of a sub chunk of at least the desired size.
In the unlikely case malloc is configured so that some of your chunks (plus 60KB) are still not large enough to to trigger 4K alignment, you could add 0xFFF8 to the desired size, so (with the 8 byte alignment used for small chunks) you have enough extra.