VxWorks中内存操作API函数

系统中内存分配必须是字节对齐的,分配内存的起始地址和大小都应该是对齐值的整数倍

如果用户申请的内存块大小不满足对齐要求VxWorks会自动调整,VxWorks中分配内存块时,块本身有额外开销。不同的目标机体系结构,对齐值和内存块的开销是不同的(具体值在相应体系结构的头文件中定义)

                            各种体系的直接对齐值和开销

体系 对齐值 块开销 ARM 4 8 COLDFIRE 4 8 I86 4 8 M86K 4 8 MCORE 8 8 MIPS 16 16 PPC 8/16 8/16 SH 4 8 SIMNT 8 8 SIMSOLARIS 8 8 SPARC 8 8

在VxWorks内存分区只能创建,不能删除。VxWorks提供memLib、memPartLib库支持其内存分配。

memPartLib:紧凑的内存分区管理库

memLib:    完整功能的内存分区管理库

NAME

memPartLib - user level memory partition manager

ROUTINES

memPartCreate( ) - create a memory partition

memPartDelete( ) - delete a partition and free associated memory

memPartAddToPool( ) - add memory to a memory partition

memPartAlignedAlloc( ) - allocate aligned memory from a partition

memPartAlloc( ) - allocate a block of memory from a partition

memPartFree( ) - free a block of memory in a partition

memPartRealloc( ) - reallocate a block of memory in a specified partition

memPartOptionsGet( ) - get the options of a memory partition

memPartOptionsSet( ) - set the debug options for a memory partition

memPartFindMax( ) - find the size of the largest free block

memPartInfoGet( ) - get partition information

NAME

memLib - user heap manager

ROUTINES

memAddToPool( ) - add memory to the RTP memory partition

malloc( ) - allocate a block of memory from the RTP heap (ANSI)

free( ) - free a block of memory from the RTP heap (ANSI)

memalign( ) - allocate aligned memory from the RTP heap

valloc( ) - allocate memory on a page boundary from the RTP heap

memOptionsSet( ) - set the options for the RTP heap

memOptionsGet( ) - get the options for the RTP heap

calloc( ) - allocate space for an array from the RTP heap (ANSI)

realloc( ) - reallocate a block of memory from the RTP heap (ANSI)

cfree( ) - free a block of memory from the RTP heap

memFindMax( ) - find the largest free block in the RTP heap

memInfoGet( ) - get heap information

VxWorks提供memShow库支持内存的显示:

NAME

memShow - memory show routines

ROUTINES

memShow( ) - show blocks and statistics for the current heap partition

memPartShow( ) - show available and allocated memory in specified partition

应用1

创建一个内存分区,并从这个内存分区进行内存分配

内存分区从系统内存池的地址0x500000开始!大小为100000字节

 从该内存分区中分配大小为200字节的内存块

->partId = memPartCreate(0x500000,100000)

->ptr = memPartAlloc(partId,200)

应用2

把x86目标机上低端的1MB内存空间中的0x5000~0xA0000添加到内存池中:

添加到系统内存池:

memAddToPool(0x5000,0xA0000-0x5000)

添加到某个指定的内存分区中:

memPartAddToPool(partId, 0x5000,0xA0000-0x5000)

应用3

有的应用程序在运行时,需要一块较大的内存,但是当进行内存请求时,由于已经存在内存碎片,导致不能分配到所需要的一块连续内存区域,返回的错误值可能是“memPartAlloc:block too big”,如何解决这个问题?

·答案:

可以在映像启动获得过程中,尽早获得所需要的内存并把它作为一个私有的内存分区,这样可以避免这块内存由于碎片的存在而变得不连续进行该操作合理的位置是在usrAppInit0的开始处。因为系统运行到此时,VxWorks内核正常运行,系统的各种资源可以正常使用,但用户应用程序还没有开始使用系统资源,此时的内存碎片最少。

操作步骤  

在usrAppInit.c中申请相关全局变量,注意包含头文件memLib.h

#define MY_PART SIZE 0x200000

void *memPtr; /*pointer to my memory*/

PART_ID myPartId;/*My partion ID*/

在usrAppInit0函数开始处添加代码:

memPtr = malloc (MY PART SIZE);

myPartId = memPartCreate (memPtr,MY PART SIZE) ;

重新编译映像,启动目标机

VxWorks启动后,可以在shell中进行操作和验证显示这段内存的使用情况-

->memPartShow(myPartld)

从此内存分区申请200000字节大小的内存

->myMemPtr=memPartAlloc(myPartld,200000)

显示这段内存的使用情况

->memPartShow(myPartld)

- 释放申请的内存

->memPartFree(myPartId,myMemPtr)

显示这段内存的使用情况

->memPartShow(myPartId)

参考阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。