YJX_Driver_034_内存管理相关内核API

1、

内存管理相关内核API
  A、RtlCopyMemory,RtlCopyBytes和RtlMoveMemory
  C、RtlZeroMemory和RtlFillMemory
  D、RtlEqualMemory
  E、ExAllocatePool和 ExFreePool
  F、重载new和delete操作符

【225】下面是从MSDN里面,复制出来的函数的说明

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

VOID RtlCopyMemory(
  __in VOID UNALIGNED *Destination, ////IA64 表示逐字节对齐 #pragma packed(1) byte
  __in const VOID UNALIGNED *Source,
  __in SIZE_T Length
);

  【310】"UNALIGNED" 表示 不对其的一个方式,它也是一个宏。它一般表示 在32位的系统下面 相当于是一个空宏;在64位平台 及一些其他的平台下面,它表示 比如说 IA64的平台下面 表示逐字节对齐(有点像 前面学过的 "#pragma packed(1)")。【505】也就是说,相当于 其实这个指针("VOID UNALIGNED *Destination")的话有点相当于byte*,我是这样理解的

    ZC: 自己再看看 这个宏的定义到底是啥...  个人理解,偏向于 不管什么平台都是相当于 byte*。

【950】

RtlCopyBytes // memcpy
VOID RtlCopyBytes(
  __in PVOID Destination,
  __in const VOID *Source,
  __in SIZE_T Length
);

  ZC: RtlCopyBytes 和 RtlCopyMemory,区别是啥?

【1015】

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
);

2、

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
);

重载new和delete操作符
//重载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)

时间: 2024-08-16 15:36:12

YJX_Driver_034_内存管理相关内核API的相关文章

与内存管理相关的几个宏

[转]与内存管理相关的几个宏 这几个宏把无符号整数转换成对应的类型      #define __pte(x) ((pte_t) { (x) } )   #define __pmd(x) ((pmd_t) { (x) } )   #define __pgd(x) ((pgd_t) { (x) } )   #define __pgprot(x)     ((pgprot_t) { (x) } )   根据x把它转换成对应的无符号整数      #define pte_val(x)      ((x

OC内存管理相关整理

OC内存管理 一.基本原理 (一)为什么要进行内存管理.内存管理的目的是什么? 由于移动设备的内存极其有限,所以每个APP所占的内存也是有限制的,当app所占用的内存较多时,系统就会发出内存警告,这时需要回收一些不需要再继续使用的内存空间,比如回收一些不再使用的对象和变量等. 管理范围:任何继承NSObject的对象,对其他的基本数据类型无效 管理目的: 1.不要释放或者覆盖还在使用的内存,这会引起程序崩溃: 2.释放不再使用的内存,防止内存泄露.(ios程序的内存资源很是宝贵.) 本质原因是因

linux内存管理-内核用户空间 【转】

转自:http://blog.chinaunix.net/uid-25909619-id-4491362.html 1,linux内存管理中几个重要的结构体和数组 page unsigned long flags 一组标志,也对页框所在的管理区进行编号 atomic_t _count 该页被引用的次数 atomic_t _mapcount 页框中页表项数目,如果没有则为-1 struct list_head lru 管理page忙碌/空闲链表(inactive_list/active_list)

Kernel那些事儿之内存管理(11) --- 内核映射(上)

前面简单地介绍了三种不同的地址空间,接下来重点讲述线性地址空间到物理地址空间的映射. 我们先从32位系统开始. 在32位系统中,线性地址空间的大小为 2^32,即4GB.Kernel一般会按照 3:1 的比例,把线性地址空间分为两部分: 0~3GB 用户地址空间 3GB~4GB 内核地址空间. 用户地址空间的管理和映射是个大的topic.我们后面再详细讲述. 内核地址空间只有1GB大小,最多只能映射1GB的物理内存.那问题来了:1GB之外物理内存怎么办? Kernel 给出的解决办法是,把1GB

【研究任务】linux内存管理机制——内核空间

Linux内存中线性地址为4G,0~3G为用户空间,3~4G为内核空间 一.      内核空间 内核空间是3~4G的内存地址,主要用来存储高优先级的代码 在X86结构中的内核地址存在三种类型的区域: ZONE_DMA     内存开始的16m ZONE_NORMAL       16m~896m ZONE_HIGHMEM    896M~ ZONE_DMA是DMA使用的页(DMA是直接路径访问,不经过cpu缓存而直接访问内存)ZONE_NORMAL是正常可寻址的页.ZONE_HIGHMEM是动

Kernel那些事儿之内存管理(13) --- 内核映射(下)

前面讲过,针对于内核地址空间中后面的128MB空间,Kernel提供了三种机制来映射物理内存.之前讲过了两种,即持久内核映射和临时内核映射.这两种机制的目的都是一样的:使Kernel能够访问到高端内存. 今天讲一下第三种机制:非连续内存分配,也就是vmalloc.这个机制同样可以使Kernel能够访问到高端内存,不过这不是该机制的主要目的.该机制的主要目的是:把物理上不连续的页面映射到连续的内核线性地址空间中. 非连续内存区域管理 既然是映射,肯定会涉及到三个元素:集合L,集合P,映射M. 集合

C/C++中内存管理相关知识

内存分配方式 内存分配方式有三种: (1) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量, static 变量. (2) 在栈上创建.在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放.栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限. (3) 从堆上分配,亦称动态内存分配.程序在运行的时候用 malloc 或 new 申请任意多少的内存,程序员自己负责在何时用 free 或

Kernel那些事儿之内存管理(12) --- 内核映射(中)

内核地址空间中后面这128MB的最后一部分,是固定映射 (fixed mappings). 固定映射是什么意思?为什么要有固定映射?Kernel源代码的注释里有一句话,可谓一语中的:The point is to have a constant address at compile time, but to set the physical address only in the boot process. 一个固定映射的线性地址是个常量,例如0xffffc000,且该常量在编译阶段就可以确定.

内存管理 相关宏

CC_SAFE_DELETE(p)       使用delete操作符删除一个C++对象p,如果p为NULL,则不进行操作 CC_SAFE_DELETE_ARRAY(p) 使用delete[]操作符删除一个C++数组p,如果p为NULL,则不进行操作 CC_SAFE_FREE(p)         使用free()函数删除p,如果p为NULL,则不进行操作 CC_SAFE_RELEASE(p)      使用release()方法释放Cocos2d-x对象p的一次引用,如果p为NULL,则不进行