天天看點

WDM驅動程式開發之記憶體映射:KMemory類

KMemory類:

一、Overview

    KMemory類實作對記憶體描述清單(MDL)的封裝。一個記憶體描述清單描述了一系列虛拟記憶體。它的成員變量包括開始位址和大小,以及記憶體頁的實體位址集合(這個如果有的話,應該在虛位址後面)。

    KMemory類對于用direct I/O方式的裝置是很重要的。包括在建立一個KDevice派生類執行個體時傳遞一個DO_DIRECT_IO标志的那些裝置,也包括使用一個由METHOD_IN_DIRECT或METHOD_OUT_DIRECT構成的I/O控制碼來支援DeviceIoControl調用的那些裝置。這兩種情況下,IRP包含一個指向MDL的指針,這個指針可以由KIrp::Mdl函數取得。由這些IRP傳遞過來的MDL描述了I/O操作将要傳輸的源位址或者目的位址(取決于讀操作還是寫操作,讀的目的位址,寫的源位址)。KMemory類提供了成員函數來使編譯器自動實作KMemory和PMDL之間的類型轉換。

    一些驅動程式通過包含一個用MapToSystemSpace函數傳回的指針(指向緩沖區)來直接存取緩沖區。裝置驅動程式作DMA傳輸的時候經常會把這個指針作為參數直接傳遞給KDmaTransfer::Initiate。

二、Member Functions

1、KMemory - Constructor (five forms).構造函數五種原型

2、Initialize - Initializes the object when in an invalid state (three forms).對處于非法狀态的對象進行初始化

3、~KMemory - Destructor.析構函數

4、Invalidate - Removes the object from an initialized state.釋放資源

5、IsValid - Test if the object is initialized.測試對象是否經過初始化,是否合法

6、Size - Returns the size of the memory region described by the object.傳回對象描述的記憶體區大小

7、OffsetFromPageBase - Returns the byte offset from the highest page boundary below or equal to the address of the

beginning of the memory region described by the object to the start of the region.傳回從最大頁邊界位址到區域開始位址的偏移

8、VirtualAddress - Returns the virtual address of the memory region described by the object.傳回對象描述的虛拟位址區域

9、MapToSystemSpace* - Provides a mapping of the memory region that is usable by the system.提供到系統使用的記憶體區的映射

10、MapToSystemSpaceSafe - Provides a safer mapping of the memory region that is usable by the system.映射的更安全的方法

11、MapToUserSpace - Maps locked pages to an address usable by the current process.把鎖定頁映射到目前程序使用的一個位址

12、LockPages - Make the pages of the memory region remain resident.鎖定記憶體頁,使此頁常駐記憶體

13、UnlockPages - Allow the pages of the memory region to be paged.解除記憶體頁鎖定

14、Unmap - Unmaps previously mapped locked pages.解除先前映射的鎖定記憶體頁

15、IsNull - Tests for a NULL memory object.測試一個記憶體對象是否為空

16、operator PMDL ( ) - Overloaded cast to a pointer to the underlying system MDL object.重載操作符,MDL指針

17、RequiredMdlStorage - Static member returns count of byte required to store system MDL object given base virtual address and size of memory region.靜态函數,傳回要存儲系統MDL對象所需要的位元組數大小

18、GetPageArray - Obtains a pointer to the array of physical page addresses that underlie a locked memory object.傳回一個指向實體頁位址數組(處于鎖定記憶體頁對象下面)的指針

19、SetPageArray - Causes the system to fill the objects array of physical page addresses if not already filled.使系統填充實體頁位址數組(如果還沒有填充的話)

三、注意事項

    對于Windows2000及其以後的系統,MapToSystemSpace函數已經廢棄不用,實作相同的功能應該使用MapToSystemSpaceSafe函數。值得一提的是,在DriverStudio Wizard生成的XP下的架構裡也不小心使用了MapToSystemSpace函數,不知是不是他們的倏忽,開發者應該使用MapToSystemSpaceSafe代替之。

                                            ----------------

                                            § 譯自"DriverWorks幫助檔案"  §

                                            §   李文凱 2008年03月30日    §

                                            ----------------

繼續閱讀