內存對齊

一 內存形式
編程者看內存的方式
在這裏插入圖片描述
cpu看內存的方式
在這裏插入圖片描述

二 內存不對齊的弊端

If you don’t understand and address alignment issues in your software,
the following scenarios, in increasing order of severity, are all possible:

1 Your software will run slower.
2 Your application will lock up.
3 Your operating system will crash.
4 Your software will silently fail, yielding incorrect results.

三 內存對齊好處
1 平臺原因(移植原因):不是全部的硬件平臺都能訪問隨意地址上的隨意數據的;某些硬件平臺僅僅能在某些地址處取某些特定類型的數據,否則拋出硬件異常。

2 性能原因:經過內存對齊後,CPU的內存訪問速度大大提升。

四 內存訪問粒度
本質是cpu讀取是按塊讀取,塊的大小分爲1Byte, 2Byte, 4Byte, 8Byte, 16Byte, 32Byte等,這即是內存訪問粒度

your computer’s processor does not read from and write to memory in byte-sized chunks. Instead, it accesses memory in two-, four-, eight- 16- or even 32-byte chunks. We’ll call the size in which a processor accesses memory its memory access granularity.

假設需要取四字節長度數據,看一下不同訪問粒度下的訪問次數

1 單字節訪問粒度
在這裏插入圖片描述
無論是數據是對齊,還是非對齊方式,cpu都要執行4次訪問

2 雙字節訪問粒度
在這裏插入圖片描述
對齊方式,訪問需要兩次,非對齊方式,訪問需要3次

3 四字節內存訪問粒度
在這裏插入圖片描述
對齊方式,訪問需要一次,非對齊方式訪問需要2次

五 結構體
結構體的內存對齊,相比更復雜一下,成員變量的順序不同,對齊後佔用的內存大小也有區別,對齊是使佔用空間變大。

void Munge64( void ∗data, uint32_t size ) {
typedef struct {
    char    a;
    long    b;
    char    c;
}   Struct;

What is the size of this structure in bytes? Many programmers will answer “6 bytes.” It makes sense: one byte for a, four bytes for b and another byte for c. 1 + 4 + 1 equals 6. Here’s how it would lay out in memory:
在這裏插入圖片描述

However, if you were to ask your compiler to sizeof( Struct ), chances are the answer you’d get back would be greater than six, perhaps eight or even twenty-four. There’s two reasons for this: backwards compatibility and efficiency.

First, backwards compatibility. Remember the 68000 was a processor with two-byte memory access granularity, and would throw an exception upon encountering an odd address. If you were to read from or write to field b, you’d attempt to access an odd address. If a debugger weren’t installed, the old Mac OS would throw up a System Error dialog box with one button: Restart. Yikes!

So, instead of laying out your fields just the way you wrote them, the compiler padded the structure so that b and c would reside at even addresses:

在這裏插入圖片描述
六 總結
內存對齊本身對程序員來說是透明的,即程序員該取變量就取變量,該存就存,編譯程序時編譯器會把變量按本身的平臺進行對齊。
並且,現在的CPU都很高級,很多CPU,ARM 7以上應該也支持內存不對齊訪問

參考文檔
1 https://developer.ibm.com/articles/pa-dalign/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章