对齐和未对齐的内存访问? [英] Aligned and unaligned memory accesses?

查看:188
本文介绍了对齐和未对齐的内存访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对齐和非对齐内存访问之间的区别是什么?



我在TMS320C64x DSP上工作,我想使用内部函数),它有

  ushort& _amem2(void * ptr); 
ushort& _mem2(void * ptr);

其中 _amem2



我应该什么时候使用?


<对齐存储器访问意味着指针(作为整数)是一个类型特定值的倍数,称为对齐。对齐是自然地址倍数,其中类型必须是或者应当被存储(例如,出于性能原因)在CPU上。例如,CPU可能要求所有两字节加载或存储通过为二的倍数的地址完成。对于小的基本类型(4字节以下),对齐几乎总是类型的大小。对于结构,对齐通常是任何成员的最大对齐。


C编译器总是把你声明的变量放在满足正确对齐的地址。所以如果ptr指向例如。一个uint16_t变量,它将被对齐,你可以使用_amem2。您需要使用_mem2仅当您正在访问例如。通过I / O接收的压缩字节数组,或者字符串中间的字节。


What is the difference between aligned and unaligned memory access?

I work on an TMS320C64x DSP, and I want to use the intrinsic functions (C functions for assembly instructions) and it has

ushort & _amem2(void *ptr);
ushort & _mem2(void *ptr);

where _amem2 does an aligned access of 2 bytes and _mem2 does unaligned access.

When should I use which?

解决方案

An aligned memory access means that the pointer (as an integer) is a multiple of a type-specific value called the alignment. The alignment is the natural address multiple where the type must be, or should be stored (e.g. for performance reasons) on a CPU. For example, a CPU might require that all two-byte loads or stores are done through addresses that are multiples of two. For small primitive types (under 4 bytes), the alignment is almost always the size of the type. For structs, the alignment is usually the maximum alignment of any member.

The C compiler always puts variables that you declare at addresses which satisfy the "correct" alignment. So if ptr points to e.g. a uint16_t variable, it will be aligned and you can use _amem2. You need to use _mem2 only if you are accessing e.g. a packed byte array received via I/O, or bytes in the middle of a string.

这篇关于对齐和未对齐的内存访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆