【Windows DDK】IoAllocateMd函数参数length限制

本文深入探讨了Windows系统中IoAllocateMdl函数的作用,如何分配内存描述符列表(MDL),并将其与IRP(内部请求程序)关联。重点介绍了函数参数解释、返回值、以及最大可分配缓冲区大小的计算方法。

The IoAllocateMdl routine allocates a memory descriptor list (MDL) large enough to map a buffer, given the buffer's starting address and length. Optionally, this routine associates the MDL with an IRP.

Syntax

C++

PMDL IoAllocateMdl(
  _In_opt_     PVOID VirtualAddress,
  _In_         ULONG Length,
  _In_         BOOLEAN SecondaryBuffer,
  _In_         BOOLEAN ChargeQuota,
  _Inout_opt_  PIRP Irp
);

Parameters

VirtualAddress [in, optional]

Pointer to the base virtual address of the buffer the MDL is to describe.

Length [in]

Specifies the length, in bytes, of the buffer that the MDL is to describe. For more information, see the following Remarks section.

SecondaryBuffer [in]

Indicates whether the buffer is a primary or secondary buffer. This parameter determines how the MDL is to be linked to the IRP. All buffers except the first buffer described by an MDL in an IRP are considered secondary buffers. This field must be FALSE if no IRP is associated with the MDL. For more information, see the following Remarks section.

ChargeQuota [in]

Reserved for system use. Drivers must set this parameter to FALSE.

Irp [in, out, optional]

Pointer to an IRP to be associated with the MDL. If the Irp pointer is non-NULL, the allocated MDL is associated with the specified IRP's MDL list, according to the value of SecondaryBuffer.

Return value

IoAllocateMdl returns a pointer to an MDL, or, if the MDL cannot be allocated, it returns NULL.

Remarks

IoAllocateMdl can be used by a driver that needs to break a buffer into pieces, each mapped by a separate MDL, or to map a driver-allocated buffer. The driver should call MmBuildMdlForNonPagedPool with the MDL allocated by this call to set up an MDL describing a driver-allocated buffer in nonpaged pool.

The Length parameter specifies the size of the buffer that is to be described by the MDL. In Windows Server 2003, Windows XP, and Windows 2000, the maximum buffer size, in bytes, that this routine can allocate is PAGE_SIZE * (65535 - sizeof(MDL)) / sizeof(ULONG_PTR). In Windows Vista and Windows Server 2008, the maximum buffer size is (2 gigabytes - PAGE_SIZE). In Windows 7 and Windows Server 2008 R2, the maximum buffer size is (4 gigabytes - PAGE_SIZE).

If the SecondaryBuffer parameter is FALSE, the routine updates Irp->MdlAddress to point to the new MDL. IfSecondaryBuffer is TRUE, the routine adds the MDL to the end of the MDL chain that Irp->MdlAddress points to.

For more information about MDLs, see Using MDLs.

Requirements

 

Version

Available in Windows 2000 and later versions of Windows.

Header

Wdm.h (include Wdm.h, Ntddk.h, or Ntifs.h)

Library

Contained in Ntoskrnl.lib.

IRQL

<= DISPATCH_LEVEL

 

        这里,需要注意的是第二个参数Length。计算公式Length ≤PAGE_SIZE * (65535 - sizeof(MDL)) / sizeof(ULONG_PTR),PAGE_SIZE在大多数Windows系统(包括win2003-x64、xp)中为4KB,sizeof(ULONG_PTR)在32bit系统中为4B,在64bit系统中为8B。因此,限制为32bit系统中length<64MB,64bit系统中length<32MB,否则函数返回值将会是NULL。

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值