QNX: Shmemallocator: Fix Compilation Warnings
[ipc/ipcdev.git] / qnx / src / ipc3x_dev / sharedmemallocator / samples / sharedMemAllocator / usr / SharedMemAllocator.c
1 /*
2  *  @file       SharedMemAllocator.c
3  *
4  *  @brief      Shared Memory Allocator.
5  *
6  *  ============================================================================
7  *
8  *  Copyright (c) 2012, Texas Instruments Incorporated
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *  *  Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *
17  *  *  Redistributions in binary form must reproduce the above copyright
18  *     notice, this list of conditions and the following disclaimer in the
19  *     documentation and/or other materials provided with the distribution.
20  *
21  *  *  Neither the name of Texas Instruments Incorporated nor the names of
22  *     its contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35  *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *  Contact information for paper mail:
37  *  Texas Instruments
38  *  Post Office Box 655303
39  *  Dallas, Texas 75265
40  *  Contact information:
41  *  http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm?
42  *  DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact
43  *  ============================================================================
44  *
45  */
47 #include <stdio.h>
48 #include <stdlib.h>
50 #include "ti/shmemallocator/SharedMemoryAllocatorUsr.h"
52 shm_buf bufs[100];
53 int shmblocks[2] = {0x1000000, 0x1000000};
55 void printMenu()
56 {
57     printf("\n================================\n");
58     printf("\n1 : req buffer(size, block, alignment)\n");
59     printf("\n2 : req buffer(size, block)\n");
60     printf("\n3 : req buffer(size, alignment)\n");
61     printf("\n4 : req buffer(size)\n");
62     printf("\n0 : release buffer\n");
63     printf("\nC : release all buffers\n");
64     printf("\n# : print allocated bufs\n");
65     printf("\nR : RANDOM test\n");
66     printf("\nT : MONKEY test\n");
67     printf("\nM/m : print Menu\n");
68     printf("\nE/e : EXIT\n");
69     printf("\n================================\n");
70 }
72 void printAllocBufs()
73 {
74     int i;
75     for(i=0; i<100; i++)
76         if(bufs[i].size)
77             printf("%d)VA=0x%0x bl=%d size=0x%0x\n", i, (unsigned)bufs[i].vir_addr, bufs[i].blockID, bufs[i].size);
78 }
80 void clearSHM()
81 {
82     int i;
83     int status;
84     for(i=0; i<100; i++)
85         if(bufs[i].size) {
86             status = SHM_release(&bufs[i]);
87             if(!status) {
88                 shmblocks[bufs[i].blockID] += bufs[i].size;
89                 bufs[i].size = 0;
90                 bufs[i].vir_addr = 0;
91                 bufs[i].phy_addr = 0;
92                 bufs[i].blockID = -1;
93             }
94             else
95                 printf("\nclearSHM: cloudn't release buf(%d) 0x%x)\n", i, (unsigned)bufs[i].vir_addr);
96         }
97 }
99 void monkeyTest()
101     int i;
102     int size;
103     int blockID;
104     int alignment;
105     int status;
107     for(i=0; i<100; i++) {
108         switch(rand()%8) {
109             case 0:
110                 size = rand();
111                 alignment = rand();
112                 status = SHM_alloc_aligned(size, alignment, &bufs[i]);
113                 printf("\n[APP] : SHM_alloc_aligned(%d, 0x%x): %s\n", size, alignment, status?"FAILED":"PASSED");
114                 if(!status)
115                     shmblocks[bufs[i].blockID] -= bufs[i].size;
116                 if(rand()%2) {
117                     status = SHM_release(&bufs[i]);
118                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
119                     if(!status) {
120                         shmblocks[bufs[i].blockID] += bufs[i].size;
121                         bufs[i].size = 0;
122                         bufs[i].vir_addr = 0;
123                         bufs[i].phy_addr = 0;
124                         bufs[i].blockID = -1;
125                     }
127                 }
128                 break;
129             case 1:
130                 size = rand()%0x1000;
131                 alignment = rand()%10000;
132                 status = SHM_alloc_aligned(size, alignment, &bufs[i]);
133                 printf("\n[APP] : SHM_alloc_aligned(%d, 0x%x): %s\n", size, alignment, status?"FAILED":"PASSED");
134                 if(!status)
135                     shmblocks[bufs[i].blockID] -= bufs[i].size;
136                 if(rand()%2) {
137                     status = SHM_release(&bufs[i]);
138                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
139                     if(!status) {
140                         shmblocks[bufs[i].blockID] += bufs[i].size;
141                         bufs[i].size = 0;
142                         bufs[i].vir_addr = 0;
143                         bufs[i].phy_addr = 0;
144                         bufs[i].blockID = -1;
145                     }
146                 }
147                 break;
148             case 2:
149                 size = rand();
150                 blockID = rand();
151                 alignment = rand();
152                 status = SHM_alloc_aligned_fromBlock(size, alignment, blockID, &bufs[i]);
153                 if(!status)
154                     shmblocks[bufs[i].blockID] -= bufs[i].size;
155                 printf("\n[APP] : SHM_alloc_aligned_fromBlock(%d, 0x%x, %d): %s\n", size, alignment, blockID, status?"FAILED":"PASSED");
156                 if(rand()%2) {
157                     status = SHM_release(&bufs[i]);
158                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
159                     if(!status) {
160                         shmblocks[bufs[i].blockID] += bufs[i].size;
161                         bufs[i].size = 0;
162                         bufs[i].vir_addr = 0;
163                         bufs[i].phy_addr = 0;
164                         bufs[i].blockID = -1;
165                     }
166                 }
167                 break;
168             case 3:
169                 size = rand()%0x1000;
170                 blockID = rand()%2;
171                 alignment = rand()%0xFFF;
172                 status = SHM_alloc_aligned_fromBlock(size, alignment, blockID, &bufs[i]);
173                 if(!status)
174                     shmblocks[bufs[i].blockID] -= bufs[i].size;
175                 printf("\n[APP] : SHM_alloc_aligned_fromBlock(%d, 0x%x, %d): %s\n", size, alignment, blockID, status?"FAILED":"PASSED");
176                 if(rand()%2) {
177                     status = SHM_release(&bufs[i]);
178                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
179                     if(!status) {
180                         shmblocks[bufs[i].blockID] += bufs[i].size;
181                         bufs[i].size = 0;
182                         bufs[i].vir_addr = 0;
183                         bufs[i].phy_addr = 0;
184                         bufs[i].blockID = -1;
185                     }
186                 }
187                 break;
188             case 4:
189                 size = rand();
190                 blockID = rand();
191                 status = SHM_alloc_fromBlock(size, blockID, &bufs[i]);
192                 if(!status)
193                     shmblocks[bufs[i].blockID] -= bufs[i].size;
194                 printf("\n[APP] : SHM_alloc_fromBlock(%d, %d): %s\n", size, blockID, status?"FAILED":"PASSED");
195                 if(rand()%2) {
196                     status = SHM_release(&bufs[i]);
197                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
198                     if(!status) {
199                         shmblocks[bufs[i].blockID] += bufs[i].size;
200                         bufs[i].size = 0;
201                         bufs[i].vir_addr = 0;
202                         bufs[i].phy_addr = 0;
203                         bufs[i].blockID = -1;
204                     }
205                 }
206                 break;
207             case 5:
208                 size = rand()%0x1000;
209                 blockID = rand()%2;
210                 status = SHM_alloc_fromBlock(size, blockID, &bufs[i]);
211                 printf("\n[APP] : SHM_alloc_fromBlock(%d, %d): %s\n", size, blockID, status?"FAILED":"PASSED");
212                 if(!status)
213                     shmblocks[bufs[i].blockID] -= bufs[i].size;
214                 if(rand()%2) {
215                     status = SHM_release(&bufs[i]);
216                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
217                     if(!status) {
218                         shmblocks[bufs[i].blockID] += bufs[i].size;
219                         bufs[i].size = 0;
220                         bufs[i].vir_addr = 0;
221                         bufs[i].phy_addr = 0;
222                         bufs[i].blockID = -1;
223                     }
224                 }
225                 break;
226             case 6:
227                 size = rand();
228                 blockID = rand();
229                 status = SHM_alloc(size, &bufs[i]);
230                 printf("\n[APP] : SHM_alloc(%d): %s\n", size, status?"FAILED":"PASSED");
231                 if(!status)
232                     shmblocks[bufs[i].blockID] -= bufs[i].size;
233                 if(rand()%2) {
234                     status = SHM_release(&bufs[i]);
235                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
236                     if(!status) {
237                         shmblocks[bufs[i].blockID] += bufs[i].size;
238                         bufs[i].size = 0;
239                         bufs[i].vir_addr = 0;
240                         bufs[i].phy_addr = 0;
241                         bufs[i].blockID = -1;
242                     }
243                 }
244                 break;
245             case 7:
246                 size = rand()%0x1000;
247                 blockID = rand()%2;
248                 status = SHM_alloc(size, &bufs[i]);
249                 printf("\n[APP] : SHM_alloc(%d): %s\n", size, status?"FAILED":"PASSED");
250                 if(!status)
251                     shmblocks[bufs[i].blockID] -= bufs[i].size;
252                 if(rand()%2) {
253                     status = SHM_release(&bufs[i]);
254                     printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)bufs[i].vir_addr, bufs[i].blockID, status?"FAILED":"PASSED");
255                     if(!status) {
256                         shmblocks[bufs[i].blockID] += bufs[i].size;
257                         bufs[i].size = 0;
258                         bufs[i].vir_addr = 0;
259                         bufs[i].phy_addr = 0;
260                         bufs[i].blockID = -1;
261                     }
262                 }
263                 break;
264         }
265     }
268 void randomTest()
270     int i;
271     int size;
272     int blockID;
273     int alignment;
274     int status;
275     shm_buf buf;
277     for(i=1; i<3; i++) {
278         size = rand();
279         blockID = rand();
280         alignment = rand();
281         status = SHM_alloc_aligned(size, alignment, &buf);
282         printf("\n[APP] : SHM_alloc_aligned(%d, 0x%x): %s\n", size, alignment, status?"FAILED":"PASSED");
283         status = SHM_release(&buf);
284         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
285         status = SHM_alloc_fromBlock(size, blockID, &buf);
286         printf("\n[APP] : SHM_alloc_fromBlock(%d, %d): %s\n", size, blockID, status?"FAILED":"PASSED");
287         status = SHM_release(&buf);
288         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
289         status = SHM_alloc_aligned_fromBlock(size, alignment, blockID, &buf);
290         printf("\n[APP] : SHM_alloc_aligned_fromBlock(%d, 0x%x, %d): %s\n", size, alignment, blockID, status?"FAILED":"PASSED");
291         status = SHM_release(&buf);
292         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
293         status = SHM_alloc(size, &buf);
294         printf("\n[APP] : SHM_alloc(%d): %s\n", size, status?"FAILED":"PASSED");
295         status = SHM_release(&buf);
296         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
297         size = rand()%4096;
298         blockID = rand()%2;
299         alignment = rand()%(i*1000);
300         status = SHM_alloc_aligned(size, alignment, &buf);
301         printf("\n[APP] : SHM_alloc_aligned(%d, 0x%x): %s\n", size, alignment, status?"FAILED":"PASSED");
302         status = SHM_release(&buf);
303         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
304         status = SHM_alloc_fromBlock(size, blockID, &buf);
305         printf("\n[APP] : SHM_alloc_fromBlock(%d, %d): %s\n", size, blockID, status?"FAILED":"PASSED");
306         status = SHM_release(&buf);
307         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
308         status = SHM_alloc_aligned_fromBlock(size, alignment, blockID, &buf);
309         printf("\n[APP] : SHM_alloc_aligned_fromBlock(%d, 0x%x, %d): %s\n", size, alignment, blockID, status?"FAILED":"PASSED");
310         status = SHM_release(&buf);
311         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
312         status = SHM_alloc(size, &buf);
313         printf("\n[APP] : SHM_alloc(%d): %s\n", size, status?"FAILED":"PASSED");
314         status = SHM_release(&buf);
315         printf("\n[APP] : SHM_release(0x%x, %d): %s\n", (unsigned)buf.vir_addr, buf.blockID, status?"FAILED":"PASSED");
316     }
319 int main()
321     int status, i;
322     int size, alignment, blockID;
323     char ch;
324     int ip = 1;
325     printMenu();
326     while(ip) {
327         ch = getchar();
328         switch(ch) {
329             case '#':
330                 printAllocBufs();
331                 break;
332             case 'R':
333                 randomTest();
334                 break;
335             case 'T':
336                 monkeyTest();
337                 break;
338             case 'E':
339             case 'e':
340                 ip = 0;
341                  break;
342             case 'M':
343             case 'm':
344                 printMenu();
345                 break;
346             case 'C':
347                 clearSHM();
348                 break;
349             case '1':
350             case '2':
351             case '3':
352             case '4':
353                 for(i=0; i<100; i++) {
354                     if(!bufs[i].vir_addr) {
355                         printf("BUF[%d] selected\n", i);
356                         break;
357                     }
358                 }
359                 if(i==100) {
360                     printf("\n###buf list full\n");
361                     break;
362                 }
363                 if(ch == '1') {
364                     printf("\nenter size(0x), alignment(0x), block(0x)\n");
365                     scanf("%x, %x, %x", &size, &alignment, &blockID);
366                     printf("\n0x%x, 0x%x, 0x%x\n", size, blockID, alignment);
367                     fflush(stdin);
368                     status = SHM_alloc_aligned_fromBlock(size, alignment, blockID, &(bufs[i]));
369                 } else if(ch == '2') {
370                     printf("\nenter size(0x), block(0x)\n");
371                     scanf("%x, %x", &size, &blockID);
372                     printf("\n0x%x, 0x%x\n", size, blockID);
373                     fflush(stdin);
374                     status = SHM_alloc_fromBlock(size, blockID, &(bufs[i]));
375                 } else if(ch == '3') {
376                     printf("\nenter size(0x), alignment(0x)\n");
377                     scanf("%x, %x", &size, &alignment);
378                     printf("\n0x%x, 0x%x\n", size, alignment);
379                     fflush(stdin);
380                     status = SHM_alloc_aligned(size, alignment, &(bufs[i]));
381                 } else {
382                     printf("\nenter size(0x)\n");
383                     scanf("%x", &size);
384                     printf("\n%0x\n", size);
385                     fflush(stdin);
386                     status = SHM_alloc(size, &(bufs[i]));
387                 }
388                 if(!status) {
389                     printf("\n[APP] : ALLOC REQ PASSED\n");
390                     printf("\nsize=0x%x, addr=0x%x\n", bufs[i].size, (unsigned)bufs[i].vir_addr);
391                     shmblocks[bufs[i].blockID] -= bufs[i].size;
392                     printf("\nremainng block[0] = 0x%0x", shmblocks[0]);
393                     printf("\nremainng block[1] = 0x%0x", shmblocks[1]);
394                 }
395                 else
396                     printf("\n[APP] : ALLOC REQ FAILED\n");
397                 break;
398             case '0':
399                 printAllocBufs();
400                 printf("\nEnter which buffer to be released:\n");
401                 scanf("%d", &i);
402                 if(i < 0 || i > 100)
403                     printf("\nWRONG RELEASE ENTRY\n");
404                 else {
405                     status = SHM_release(&bufs[i]);
406                     if(!status) {
407                         printf("\n[APP] : RELEASED REQ PASSED\n");
408                         shmblocks[bufs[i].blockID] += bufs[i].size;
409                         bufs[i].size = 0;
410                         bufs[i].vir_addr = 0;
411                         bufs[i].phy_addr = 0;
412                         bufs[i].blockID = -1;
413                         printf("\nremainng block[0] = 0x%0x", shmblocks[0]);
414                         printf("\nremainng block[1] = 0x%0x", shmblocks[1]);
415                     }
416                     else
417                         printf("\n[APP] : RELEASE REQ FAILED\n");
418                 }
419                 break;
420         }
421         if(ch != '\n' && ch != 'e' && ch != 'E')
422             printMenu();
423     }
424     return 0;