Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

VirtualAlloc #1

Open
retep998 opened this issue Oct 24, 2015 · 2 comments
Open

VirtualAlloc #1

retep998 opened this issue Oct 24, 2015 · 2 comments

Comments

@retep998
Copy link

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.

@jonas-schievink
Copy link
Owner

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!

@retep998
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants