开发者

Why memory alignment of 4 is needed for efficient access?

开发者 https://www.devze.com 2022-12-28 04:14 出处:网络
I understand why data n开发者_StackOverfloweed to be aligned (and all the efforts made to accomplish it like padding) so we can reduce the number of memory accesses but this assumes that processor jus

I understand why data n开发者_StackOverfloweed to be aligned (and all the efforts made to accomplish it like padding) so we can reduce the number of memory accesses but this assumes that processor just can fetch addresses multiples of 4(supposing we are using a 32-bit architecture). And because of that assumption we need to align memory.

My question is:

Why we can just access addresses multiple of 4(efficiency, hardware restriction, another one)?

Which is the advantages of doing this? Why cannot we access all the addresses available?


Memory is constructed from hardware (RAM) that is attached to memory busses. The wider the bus, the fewer cycles are required to fetch data. If memory was one byte wide, you'd need four cycles to read one 32-bit value. Over time memory architectures have evolved, and depending on the class of processor (embedded, low power, high performance, etc.), and the cache design, memory may be quite wide (say, 256 bits).

Given a very wide internal bus (between RAM or cache) and registers, say twice the width of the register, you could fetch a value in one cycle regardless of alignment if you have a barrel shifter in the data path. Barrel shifters are expensive, so not all processors have them; without one in the path, multiple cycles would be needed to align the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号