Example of System Memory Allocation APIs

 1 /*
 2  *        main.c 
 3  *        To demonstrate APIs related to system memory mgmt functions
 4  */
 5 
 6 #include <basic.h>
 7 #include <tk/tkernel.h>
 8 #include <stdio.h>
 9 #ifdef DEBUG
10 #include <util/tmonitor.h>
11 #endif
12 
13 void show_mem_details (VP * addr);
14 
15 /******************************************************************************
16 main
17 ******************************************************************************/
18 EXPORT ER
19 main (INT ac, UB * av[])
20 {
21   ER ercd = 0;
22 
23 #ifdef DEBUG
24   tm_monitor ();
25 #endif
26   printf ("main: (ac = %d)\n", ac);
27 
28 
29 
30   VP *addr;
31   INT nblk = 4;
32   UINT attr = TA_RNG0 | TA_NORESIDENT;
33 
34   ercd = tk_get_smb (&addr, nblk, attr);
35 
36   if (ercd == E_OK)
37     {
38       printf ("Memory allocated: Start Address = %x\n", addr);
39     }
40   else
41     {
42       printf ("Memory allocation error\n");
43     }
44 
45   show_mem_details (addr);
46 
47   tk_dly_tsk (5000);
48   ercd = tk_rel_smb (addr);
49 
50   if (ercd == E_OK)
51     {
52       printf ("Memory Released with the Start Address = %x\n", addr);
53     }
54   else
55     {
56       printf ("Memory release error\n");
57     }
58 
59 
60 //exit:
61   printf ("main ended\n\n");
62   return 0;
63 }
64 
65 void
66 show_mem_details (VP * addr)
67 {
68   T_RSMB pk_rsmb;
69   ER ercd;
70 
71   ercd = tk_ref_smb (&pk_rsmb);
72   if (ercd < E_OK)
73     {
74       printf ("Error in tk_ref_smb - %08X\n", ercd);
75       return;
76     }
77 
78   //Output the details
79   printf (" Block Size - %d \n", pk_rsmb.blksz);
80   printf (" Total Bock Count - %d\n", pk_rsmb.total);
81   printf (" Remaining free block count- %d\n", pk_rsmb.free);
82   return;
83 }

Comments

Click here to Post a Comment