天天看点

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日    §

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

继续阅读