Memory Management
VxWorks 5.x Allocation Algorithm
In vxWorks 5.x, the heap manipulation is performed on the system memory partition using the routines defined in memLib. The malloc() and free() functions in vxWorks are wrappers that map directly to memPartAlignAlloc() and memPartFree(). The routines in memLib can also be used to add memory to the system memory partition. In some cases this memory is mapped in and physically resides on the pci or VME busses. The function memAddToPool() will allow the embedded system designer to treat offboard memory as system memory. This function is sometimes used to add memory that was reserved by moving down sysMemTop() back into the system memory partition. The partition id used to designate the system memory pool is defined in global variable sysMemPartId (?)Heap management in vxWorks can present challenges to the embedded system designer. It must be remembered that the vxWorks memory model is flat, does not make use of virtual to physical mapping and is limited to the amount of physical memory available. The heap manager is extremely simple and uses a “first fit” algorithm when walking the heap during a memory allocation cycle. That is, the first available block of memory of adequate size is split, if necessary, and returned to the user. While the deallocation routines will attempt to coalesce adjacent blocks, the heap can quickly become fragmented to the point that the heap manager is unable to complete a memory allocation request. This can also affect timing in that an allocation from a fragmented heap will take considerably longer complete than a memory allocation request from an unfragmented heap. As a general guideline, while developing code for vxWorks 5.x, it is desirable to limit the amount of memory allocation and deallocation that occurs during the running of the program. If possible, all of the allocations should be done in the startup of the individual tasks. If not possible and a task is causing fragmentation, one possible cure is to use a separate memory partition for the fragmenting task. The following code show how to create a separate memory partition and to overload malloc() and free() Insert Code Excerpt Here Once the separate memory partition is created, malloc() and free() can be overloaded with call to the memLib routines. The fragmentation, though not eliminated, is limited to the memory partition. VxWorks AE Allocation Algorithm
VxVMI
VxWorks AE