原创 wince 内存管理函数

2011-4-22 21:12 2014 7 7 分类: MCU/ 嵌入式

因为IST作为一个在用户模式下运行的线程,它需要访问一个物理内存块以和设备进行通信。所以必须将这个内存块映射到IST正在运行的地址空间。完成映射的步骤如下:

1.保留一个虚拟内存块,这时需要调用VirtualAlloc函数,并传输MEM_RESERVE值作为这个函数的一个参数。

 VIRtualAlloc函数的作用:

This function reserves or commits a region of pages in the virtual addressspace of the calling process.

例子:

Memory allocated by VirtualAlloc is initialized to zero.

LPVOID VirtualAlloc(
LPVOID
lpAddress,
DWORD
dwSize,
DWORD
flAllocationType,
DWORD
flProtect
);

Parameters

lpAddress
[in] Long pointer to the specified starting address of the region to be allocated.

If the memory is being reserved, the specified address is rounded down to the next 64-KB boundary.

If the memory is reserved and is being committed, the address is rounded down to the next page boundary.

To determine the size of a page on the host computer, use the GetSystemInfo function.

If this parameter is NULL, the system determines where to allocate the region.

dwSize
[in] Specifies the size, in bytes, of the region. It is an error to set this parameter to 0.

If the lpAddress parameter is NULL, this value is rounded up to the next page boundary.

Otherwise, the allocated pages include all pages containing one or more bytes in the range from lpAddress to lpAddress+dwSize. This means that a 2-byte range straddling a page boundary causes both pages to be included in the allocated region.

flAllocationType
[in] Specifies the type of allocation.

The following table shows flags you can specify for this parameter in any combination.

Value Description
MEM_COMMIT Allocates physical storage in memory or in the paging file on disk for the specified region of pages.

An attempt to commit a committed page does not cause the function to fail. This means that a range of committed or decommitted pages can be committed without being concerned about a failure.

MEM_RESERVE Reserves a range of the virtual address space of the process without allocating physical storage.

The reserved range cannot be used by any other allocation operations, such as the malloc and LocalAlloc functions, until it is released.

Reserved pages can be committed in subsequent calls to the VirtualAlloc function.

MEM_RESET Not supported.
MEM_TOP_DOWN Allocates memory at the highest possible address.

This flag is ignored in Windows CE.

flProtect
[in] Specifies the type of access protection.

If the pages are being committed, you can specify any of the following flags, along with the PAGE_GUARD and PAGE_NOCACHE protection modifier flags.

Value Description
PAGE_READONLY Enables read access to the committed region of pages.

An attempt to write to the committed region results in an access violation.

If the system differentiates between read-only access and execute access, an attempt to execute code in the committed region results in an access violation.

PAGE_READWRITE Enables both read and write access to the committed region of pages.
PAGE_EXECUTE Enables execute access to the committed region of pages.

An attempt to read or write to the committed region results in an access violation.

PAGE_EXECUTE_READ Enables execute and read access to the committed region of pages.

An attempt to write to the committed region results in an access violation.

PAGE_EXECUTE_READWRITE Enables execute, read, and write access to the committed region of pages.
PAGE_GUARD Pages in the region become guard pages.

An attempt to read from or write to a guard page causes the system to raise a STATUS_GUARD_PAGE exception and turn off the guard page status.

Guard pages thus act as a one-shot access alarm.

The PAGE_GUARD flag is a page protection modifier. An application uses it with one of the other page protection flags, with one exception: It cannot be used with PAGE_NOACCESS.

When an access attempt leads the system to turn off guard page status, the underlying page protection takes over.

If a guard page exception occurs during a system service, the service typically returns a failure status indicator.

PAGE_NOACCESS Disables all access to the committed region of pages.

An attempt to read from, write to, or execute in the committed region results in an access violation exception, called a general protection (GP) fault.

PAGE_NOCACHE Allows no caching of the committed regions of pages.

The hardware attributes for the physical memory should be specified as no cache.

This is not recommended for general use. It is useful for device drivers; for example, mapping a video frame buffer with no caching.

This flag is a page protection modifier and is only valid when used with a page protection other than PAGE_NOACCESS.

Return Values

The base address of the allocated region of pages indicates success.

NULL indicates failure.

举例:pVitualAddr=(unsigned char *)VirtualAlloc(

                                                         0,

                                                       0x1000,//保留4KB的虚拟地址空间

                                                       MEM_RESERVE,

                                                      PAGE_NOACCESS);

2.wince 分配内存并返回一个指向这个内存块得指针,然后需要将这个块与一个世纪的物理内存块对应。为此,使用VirtualCopy函数并定义PAGE_NOCACHE参数,以便这个块里存储的数据不能被缓冲。

           

VirtualCopy

Windows CE Features > Core OS Services > Core OS Design Development > Kernel Reference > Kernel Functions

This function dynamically maps a virtual address to a physical address by creating a new page-table entry. Terminate the mapping by calling VirtualFree.

BOOL VirtualCopy( 
LPVOID
lpvDest,
LPVOID
lpvSrc,
DWORD
cbSize,
DWORD
fdwProtect
);

Parameters

lpvDest
[in] Pointer to the destination memory, which must be reserved.
lpvSrc
[in] Pointer to committed memory.
cbSize
[in] Size, in bytes, of the region. The allocated pages include all pages containing one or more bytes in the range from lpAddress to lpAddress+cbSize. This means that a 2-byte range straddling a page boundary causes both pages to be included in the allocated region.
fdwProtect
[in] Type of access protection. If the pages are being committed, any one of a number of flags can be specified, along with the PAGE_GUARD and PAGE_NOCACHE, protection modifier flags. The following table shows the flags that can be specified.
Value Description
PAGE_READONLY Enables read access to the committed region of pages. An attempt to write to the committed region results in an access violation. If the system differentiates between read-only access and execute access, an attempt to execute code in the committed region results in an access violation.
PAGE_READWRITE Enables both read and write access to the committed region of pages.
PAGE_EXECUTE Enables execution access to the committed region of pages. An attempt to read or write to the committed region results in an access violation.
PAGE_EXECUTE_READ Enables execute and read access to the committed region of pages. An attempt to write to the committed region results in an access violation.
PAGE_EXECUTE_READWRITE Enables execute, read, and write access to the committed region of pages.
PAGE_GUARD Pages in the region become guard pages. Any attempt to read from or write to a guard page causes the operating system to raise the STATUS_GUARD_PAGE exception and turn off the guard page status. Guard pages thus act as a one-shot access alarm.

The PAGE_GUARD flag is a page protection modifier. An application uses it with one of the other page protection flags, with one exception: it cannot be used with PAGE_NOACCESS.

When an access attempt leads the operating system to turn off guard page status, the underlying page protection takes over.

If a guard page exception occurs during a system service, the service typically returns a failure status indicator.

PAGE_NOACCESS Disables all access to the committed region of pages. An attempt to read from, write to, or execute in the committed region results in an access violation exception, called a general protection (GP) fault.
PAGE_NOCACHE Allows no caching of the committed regions of pages. The hardware attributes for the physical memory should be specified as no cache. It is useful for device drivers; when, for example, mapping a video frame buffer with no caching. This flag is a page protection modifier and is valid only when used with one of the page protections other than PAGE_NOACCESS.
PAGE_PHYSICAL Used to map a physical memory region. When using this flag, divide the physical address — that is, lpvSrc — by 256. Memory mapped with PAGE_PHYSICAL is not freed until the device is rebooted. Calling VirtualFree will not free this mapped physical memory. PAGE_PHYSICAL is intended for use with dedicated hardware buffers, so it cannot be freed after being mapped.

Return Values

TRUE indicates success; FALSE indicates failure. To obtain extended errorinformation, call GetLastError.

举例:nTmpVal=VirtualCopy(

                             pVirtualAddr,

                             (LPVOID)((PhyAddress<<4)/256),

                             0XD000,

                              PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);



PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条