aligned_alloc.rs icon indicating copy to clipboard operation
aligned_alloc.rs copied to clipboard

VirtualAlloc

Open retep998 opened this issue 10 years ago • 2 comments

VirtualAlloc is not a heap. It is a page allocator. Pages are rather large, often 4KB. VirtualAlloc cannot be used to allocate part of a page. As such you're wasting space for small allocations by allocating entire pages, and you're also not optimizing for the case where the alignment is less than the size of a page so you can just allocate some pages and assume they have the correct alignment.

retep998 avatar Oct 24 '15 19:10 retep998

As such you're wasting space for small allocations by allocating entire pages

Yep, but that's kind of the use case I had in mind: Large allocations with large alignment requirements (as in, many pages each).

I could just use HeapAlloc, but I don't know of a way to free all excess memory: HeapReAlloc isn't able to free, say, the first few bytes of an allocation (moving the allocation upwards to an aligned address), it can only free the "tail" by making the allocation smaller, so it still wastes a fair bit of memory in bad cases. An allocation aligned to X bytes will waste up to X-1 bytes - since I'm using this to allocate memory aligned to up to 1 MB, this is unacceptable.

That said, perhaps a good solution would be to use VirtualAlloc for requests that are at least a page in size (or have size plus alignment larger than the page size), and HeapAlloc for everything smaller (the memory wasted by using it should be less than the memory wasted by allocating a whole page, if the threshold is good).

you're also not optimizing for the case where the alignment is less than the size of a page

Right. This fix is simpler, so I might do that later.

Thanks for the report!

jonas-schievink avatar Oct 24 '15 19:10 jonas-schievink

Feel free to look at the way I wrote aligned allocations in Rust's system allocator which uses HeapAlloc. https://github.com/rust-lang/rust/blob/master/src/liballoc_system/lib.rs#L143-L232

retep998 avatar Oct 24 '15 19:10 retep998