POSIX:在64KB边界上分配64KB

Elo*_*off 0 c linux posix

我真的只想分配64KB的内存,而不是128KB,然后手动进行对齐 - 太浪费了.Windows上的VirtualAlloc正好提供了这种行为.据说SquirrelFish中的代码几乎可以在每个平台上执行此操作,但我还没有设法找到它.是否有一种节省空间的方法在POSIX中的64KB边界上分配64KB?没错,在Linux?

Ste*_*ker 8

查看 posix_memalign(3)

概要

 #include <stdlib.h>

 int
 posix_memalign(void **memptr, size_t alignment, size_t size);
Run Code Online (Sandbox Code Playgroud)

描述

 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.
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请查看联机帮助页...

  • 它在libc中的实现方式是malloc(size + 2*alignment),然后在中间找到对齐的部分.但是如果它足够大的话,它会尝试将任何悬空的头部或尾部部分返回到堆中,所以它实际上并不像我担心的那样浪费.它会工作的. (2认同)