开发者

POSIX: allocate 64KB on 64KB boundary

开发者 https://www.devze.com 2022-12-31 02:36 出处:网络
I would really like to actually only allocate 64KB of memory, not 128KB and then do the alignment manually - far too wasteful. VirtualAlloc on windows gives precisely this behavior. Supposedly there\'

I would really like to actually only allocate 64KB of memory, not 128KB and then do the alignment manually - far too wasteful. VirtualAlloc on windows gives precisely this behavior. Supposedly there's code in SquirrelFish for doing this on just about every platform, but I haven't managed to l开发者_运维百科ocate it. Is there a space efficient way to allocate 64KB on a 64KB boundary in POSIX? Failing that, in Linux?


Check out posix_memalign(3)

SYNOPSIS

 #include <stdlib.h>

 int
 posix_memalign(void **memptr, size_t alignment, size_t size);

DESCRIPTION

 The posix_memalign() function allocates size bytes of memory such that
 the allocation's base address is an exact multiple of alignment, and
 returns the allocation in the value pointed to by memptr.

Check out the manpage for further details...


You could use mmap() with MAP_PRIVATE. Quite a few libc allocators these days internally use mmap() to get chunks of memory to allocate from.


Of course posix_memalign is the elegant solution, but even if you allocate 128k, any untouched pages will remain as copy-on-write zero pages and use minimal physical resources. You could also use mmap with MAP_PRIVATE to make a 128k map, and then munmap whatever parts of it are unaligned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号