DM8147memory研究
========memory.c========
此示例演示如何使用xdc.runtime.Memory模块和不同的xdc.runtime.IHeap实现来管理内存。一个系统堆是静态使用ti.sysbios.heaps.HeapMem创建。这堆插入xdc.runtime.memory为defaultHeapInstance。此测试用例使用了两个任务。一个任务是创建静态和一个是动态创建的。这两个任务使用xdc.runtime.Memory分配内存,但使用不同的创建堆使用不同IHEAP实现。任务0使用ti.sysbios.heaps.HeapBuf静态创建其堆(task0Heap),因为在任务0的分配是一个固定的大小。任务1使用ti.sysbios.heaps.HeapMem静态地创建其堆(task1Heap),因为任务1分配可变块大小。这两个任务之前和之后分配其打印堆状态。
* This example shows the use ofxdc.runtime.Memory module and different
* xdc.runtime.IHeap implementations to manage memory.
* Asystem heap is created statically using ti.sysbios.heaps.HeapMem.
* Thisheap is plugged into xdc.runtime.memory as the defaultHeapInstance.
* Thistestcase uses two tasks. One task is statically created and one
* isdynamically created. Both tasks use xdc.runtime.Memory to allocate
* memory but use different heaps created using different IHeap
* implementations.
* Task0uses ti.sysbios.heaps.HeapBuf to statically
* create its heap (task0Heap) because the allocations in task0 are of
* afixed size.
* Task1uses ti.sysbios.heaps.HeapMem to statically create its heap
* (task1Heap) because task1 allocates variable block sizes.
* Bothtasks print their heap status before and after allocations.
#include<xdc/std.h>
#include <xdc/runtime/IHeap.h>
#include<xdc/runtime/System.h>
#include <xdc/runtime/Memory.h>
#include<ti/sysbios/BIOS.h>
#include<ti/sysbios/knl/Task.h>
#include <ti/sysbios/heaps/HeapBuf.h>
#include <ti/sysbios/heaps/HeapMem.h>
#include<xdc/cfg/global.h>
#defineTASK0BUFSIZE 32 /* size of allocations */
#defineTASK0NUMBUFS 2 /* number of buffers */
#defineTASK1BUFSIZE0 128 /* size of allocation */
#defineTASK1BUFSIZE1 64 /* size of allocation */
#defineTASK1BUFSIZE2 32 /* size of allocation */
#defineTASK1BUFSIZE3 16 /* size of allocation */
#defineTASK1NUMBUFS 4 /* number of buffers */
Voidtask0Fxn(UArg arg0, UArg arg1);
Voidtask1Fxn(UArg arg0, UArg arg1);
Voididl0Fxn();
/* Function to print heap statistics */
static Void printHeapStats(IHeap_Handle heap);
Int main()
{
Task_create(task1Fxn, NULL, NULL);
System_printf("Memoryexample started.\n");
BIOS_start(); /* does notreturn */
return(0);
}
/*task0Fxn*/
Voidtask0Fxn(UArgarg0, UArg arg1)
{
Int i;
Ptr bufs[TASK0NUMBUFS];
IHeap_Handle heap =HeapBuf_Handle_upCast(task0Heap);
System_printf("Initialtask0 heap status\n");
/* print initialtask0heap status */
printHeapStats(heap);
/* allocate blocks from task0Heap */
for (i = 0; i < TASK0NUMBUFS; i++) {
bufs[i] = Memory_alloc(heap, TASK0BUFSIZE, 0, NULL);
}
/* free memory blocks */
for (i = 0; i < TASK0NUMBUFS; i++) {
Memory_free(heap, bufs[i], TASK0BUFSIZE);
}
System_printf("Final task0 heap status\n");
/* print task0Heapstatus */
printHeapStats(heap);
System_printf("Task0Complete\n");
}
/*task1Fxn*/
Voidtask1Fxn(UArgarg0, UArg arg1)
{
Ptr bufs[TASK1NUMBUFS];
IHeap_Handle heap =HeapMem_Handle_upCast(task1Heap);
System_printf("Initialtask1 heap status\n");
/* print initialtask1Heap status */
printHeapStats(heap);
bufs[0] = Memory_alloc(heap, TASK1BUFSIZE0, 0, NULL);
bufs[1] = Memory_alloc(heap, TASK1BUFSIZE1, 0, NULL);
bufs[2] = Memory_alloc(heap, TASK1BUFSIZE2, 0, NULL);
Memory_free(heap, bufs[1], TASK1BUFSIZE1);
Memory_free(heap, bufs[2], TASK1BUFSIZE2);
bufs[3] = Memory_alloc(heap, TASK1BUFSIZE3, 0, NULL);
Memory_free(heap, bufs[0], TASK1BUFSIZE0);
Memory_free(heap, bufs[3], TASK1BUFSIZE3);
System_printf("Finaltask1 heap status\n");
/* print task1Heapstatus */
printHeapStats(heap);
System_printf("Task1Complete\n");
}
staticVoid printHeapStats(IHeap_Handle heap)
{
Memory_Stats stats;
Memory_getStats(heap, &stats);
#ifdefxdc_target__isaCompatible_28
System_printf("largestFreeSize =%ld\n", (ULong)stats.largestFreeSize);
System_printf("totalFreeSize =%ld\n", (ULong)stats.totalFreeSize);
System_printf("totalSize =%ld\n", (ULong)stats.totalSize);
#else
System_printf("largestFreeSize =%d\n", stats.largestFreeSize);
System_printf("totalFreeSize = %d\n",stats.totalFreeSize);
System_printf("totalSize = %d\n",stats.totalSize);
#endif
}
/*idl0Fxn*/
Void idl0Fxn()
{
BIOS_exit(0);
}