内存管理相关内核API

 A、RtlCopyMemory,RtlCopyBytes和RtlMoveMemory

 C、RtlZeroMemory和RtlFillMemory

 D、RtlEqualMemory

 E、ExAllocatePool和 ExFreePool

 F、重newdelete操作符

RtlCopyMemory

//把地址Source开始的长度为Length这块内存数据 复制到Destination

VOID RtlCopyMemory(

__in  VOID UNALIGNED *Destination,
  IA64 表示逐字节对齐 #pragma packed(1) byte

__in  const VOID UNALIGNED *Source,
 

__in  SIZE_T Length
);

RtlCopyBytes //memcpy

VOID RtlCopyBytes(
 

 __in  PVOID Destination,
 

 __in  const VOID *Source,

 __in  SIZE_T Length
);

 

RtlMoveMemory //重叠内存 A B C D p1=AD p2=BC

// memmove

VOID RtlMoveMemory(
 

__in  VOID UNALIGNED *Destination,

__in  const VOID UNALIGNED *Source,
 

__in  SIZE_T Length
);

RtlZeroMemory //清零内存块,清零长度为Length

VOID RtlZeroMemory(

__in  VOID UNALIGNED *Destination,
 

__in  SIZE_T Length
);
//字节

RtlFillMemory//为字符fill填充Destination 填充长度为Length

VOID RtlFillMemory(

 __in  VOID UNALIGNED *Destination,
 

 __in  SIZE_T Length,
 

 __in  UCHAR Fill
);

RtlEqualMemory //比较内存块source1和Source2 长度为Length这个范围内是否相等

LOGICAL RtlEqualMemory(

__in  const VOID *Source1,
 

__in  const VOID *Source2,
 

__in  SIZE_T Length
);

SIZE_T RtlCompareMemory(
 

__in  const VOID *Source1,
 

__in  const VOID *Source2,
 

__in  SIZE_T Length
);

 

ExAllocatePool

PVOID ExAllocatePool(
 

 __in  POOL_TYPE PoolType,
 

__in  SIZE_T NumberOfBytes
);

ExFreePool

VOID ExFreePool(
 __in  PVOID P
);
 

newdelete操作符

//new

void * __cdecl operator new(size_t size,POOL_TYPE PoolType=PagedPool)

{

       KdPrint(("global operator new\n"));

       KdPrint(("Allocate size :%d\n",size));

       return ExAllocatePool(PagedPool,size);

}

//delete

void __cdecl operator delete(void* pointer)

{

       KdPrint(("Global delete operator\n"));

       ExFreePool(pointer);

}

The POOL_TYPE enumeration type specifies the type of system memory to allocate.

Syntax

Copy

typedef enum _POOL_TYPE {

  NonPagedPool                    = 0,  //不分页

  PagedPool                       = 1,   //分页

  NonPagedPoolMustSucceed         = 2, //指定分配 非分页内存,要求必须成功。

  DontUseThisType                 = 3, //未指定,留给系统使用

  NonPagedPoolCacheAligned        = 4,//非分页内存,要求内存边界对齐。

  PagedPoolCacheAligned           = 5,//分页内存,要求内存边界对齐。

  NonPagedPoolCacheAlignedMustS   = 6 //指定分配 非分页内存,要求内存边界对齐,要求必须成功。

} POOL_TYPE;

#define RtlCopyBytes  RtlCopyMemory

#define RtlZeroBytes   RtlZeroMemory

#define RtlFillBytes   RtlFillMemory

Constants

NonPagedPool

Nonpaged pool, which is nonpageable system memory. Nonpaged pool can be accessed from any IRQL, but it is a scarce resource and drivers should allocate it only when necessary.

The system can allocate buffers larger than PAGE_SIZE from nonpaged pool only in multiples of PAGE_SIZE. Requests for buffers larger than PAGE_SIZE, but not a PAGE_SIZE multiple, waste nonpageable memory.

PagedPool

Paged pool, which is pageable system memory. Paged pool can only be allocated and accessed at IRQL < DISPATCH_LEVEL.

NonPagedPoolMustSucceed

This value is for internal use only, and is allowed only during system startup. Drivers must not specify this value at times other than system startup, because a "must succeed" request crashes the system if the requested memory size is unavailable.

DontUseThisType

Reserved for system use.

NonPagedPoolCacheAligned

Nonpaged pool, aligned on processor cache boundaries. This value is for internal use only.

PagedPoolCacheAligned

Paged pool, aligned on processor cache boundaries. This value is for internal use only.

NonPagedPoolCacheAlignedMustS

This value is for internal use only, and is allowed only during system startup. It is the cache-aligned equivalent of NonPagedPoolMustSucceed.

Remarks

When the system allocates a buffer from pool memory of PAGE_SIZE or greater, it aligns the buffer on a page boundary. Memory requests smaller than PAGE_SIZE are not necessarily aligned on page boundaries, but these requests always fit within a single page and are aligned on an 8-byte boundary.

Requirements

Header

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

__unaligned 关键字会让指针逐字节读取

#pragma packed(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蚂蚁_CrkRes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值