posix_memalign 与 malloc 对比

1. 原因原理


编程中的类型对齐问题主要是处于性能考虑,如果不做对齐,那么单个数据元素的访问很容易跨在多个时钟周期上,从而导致性能下降。
内建数据类型的对齐,是由编译器和C语言库的API实现中自动完成的,这对于用户是透明的,比如常用的 malloc。
有时候需要使用大块数据,同时需要提高性能,可能需要 64 bytes对齐,按照机器的cache line对齐等,这时候可以使用 posix_memalign:

Function: int posix_memalign (void **memptr, size_t alignment, size_t size)

Preliminary: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock fd mem | See POSIX Safety Concepts.

The posix_memalign function is similar to the memalign function in that it returns a buffer of size bytes aligned to a multiple of alignment. But it adds one requirement to the parameter alignment: the value must be a power of two multiple of sizeof (void *).

If the function succeeds in allocation memory a pointer to the allocated memory is returned in *memptr and the return value is zero. Otherwise the function returns an error value indicating the problem. The possible error values returned are:

ENOMEM

There was insufficient memory available to satisfy the request.

EINVAL

alignment is not a power of two multiple of sizeof (void *).

This function was introduced in POSIX 1003.1d. Although this function is superseded by aligned_alloc, it is more portable to older POSIX systems that do not support ISO C11.

更多内存对齐的API的内容可参考:

Aligned Memory Blocks (The GNU C Library)

2. 测试代码

#include <stdlib.h>
#include <stdio.h>

int main()
{
//      posix_memalign, (, void **, p,. size_t, alignment,. size_t, size. )
        float* p = nullptr;
        int res;

        for(int i=0; i<10; i++)
        {
                res = 99;
                res = posix_memalign((void**)&p, 64, 100*sizeof(float));
                p[99] = 0.333f + i;
                printf("res=%d, p[99] = %7.4f , p = %p\n", res, p[99], p);

//              free(p);
        }

        for(int i=0; i<10; i++)
        {
                p = (float*)malloc(100*sizeof(float));
                p[99] = 10.333f + i;
                printf("res=%d, p[99] = %7.4f , p = %p\n", p[99], p);

//                free(p);
        }

        return 0;
}

Makefile:

EXE := hello_posix_memalign

$(EXE):

.PHONY: clean
clean:
        -rm -rf $(EXE)

3. 运行效果

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值