问题描述
我想了解什么功能 memalign可()
和 posix_memalign()
做的。读取现有的文件没有帮助。
I'm trying to understand what functions memalign()
and posix_memalign()
do. Reading the available documentation didn't help.
有人可以帮助我理解它是如何工作的,并用于是什么?或者,也许提供了一个使用例子?
Can someone help me understand how it works and what is it used for? Or, perhaps provide a usage example?
我想了解Linux的内存作品,我需要写我自己的简单的内存池(低碎片堆)。
I'm trying to understand how linux memory works, I need to write my own simple memory pool (low-fragmentation heap).
推荐答案
而的malloc
为您提供了可能有任何对齐内存块(唯一的要求是,它必须为最大的基本数据类型实现所支持)对齐, posix_memalign
给你这是保证有要求的对齐内存块。
Whereas malloc
gives you a chunk of memory that could have any alignment (the only requirement is that it must be aligned for the largest primitive type that the implementation supports), posix_memalign
gives you a chunk of memory that is guaranteed to have the requested alignment.
所以如结果 posix_memalign(安培; P,32,128)。
将内存的128字节块的起始地址保证是32的倍数。
So the result of e.g. posix_memalign(&p, 32, 128)
will be a 128-byte chunk of memory whose start address is guaranteed to be a multiple of 32.
这是各种低级操作(如使用SSE指令,或DMA),需要内存服从特定的排列很有用的。
This is useful for various low-level operations (such as using SSE instructions, or DMA), that require memory that obeys a particular alignment.
这篇关于什么posix_memalign / memalign可办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!