Memory Management
5-10
Example 5-8. Memory Allocation (C6000 Platform)
/* ======== memtest.c ========
* This program allocates and frees memory from
* different memory segments.
*/
#include <std.h>
#include <log.h>
#include <mem.h>
#define NALLOCS 2 /* # of allocations from each segment */
#define BUFSIZE 128 /* size of allocations */
/* "trace" Log created by Configuration Tool */
extern LOG_Obj trace;
extern Int IDRAM;
static Void printmem(Int segid);
/*
* ======== main ========
*/
Void main()
{
Int i;
Ptr ram[NALLOCS];
LOG_printf(&trace, "before allocating ...");
/* print initial memory status */
printmem(IDRAM);
LOG_printf(&trace, "allocating ...");
/* allocate some memory from each segment */
for (i = 0; i < NALLOCS; i++) {
ram[i] = MEM_alloc(IDRAM, BUFSIZE, 0);
LOG_printf(&trace, "seg %d: ptr = 0x%x", IDRAM, ram[i]);
}
LOG_printf(&trace, "after allocating ...");
/* print memory status */
printmem(IDRAM);
/* free memory */
for (i = 0; i < NALLOCS; i++) {
MEM_free(IDRAM, ram[i], BUFSIZE);
}
LOG_printf(&trace, "after freeing ...");
/* print memory status */
printmem(IDRAM);
}
/*
* ======== printmem ========
*/
static Void printmem(Int segid)
{
MEM_Stat statbuf;
MEM_stat(segid, &statbuf);
LOG_printf(&trace, "seg %d: O 0x%x", segid, statbuf.size);
LOG_printf(&trace, "\tU 0x%x\tA 0x%x", statbuf.used, stat-
buf.length);
}