本文介绍了posix_memalign/memalign 有什么作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 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 有什么作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:06