1 /*******************************************************************
2 * FILE: qmsim.c
3 *
4 * DESCRIPTION: netapi user space transport
5 * library desktop simulator for queues.
6 * (test code, not operational)
7 *
8 * REVISION HISTORY: rev 0.0.1
9 *
10 * Copyright (c) Texas Instruments Incorporated 2010-2011
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the
22 * distribution.
23 *
24 * Neither the name of Texas Instruments Incorporated nor the names of
25 * its contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ***********************************************************************/
42 #include <stdlib.h>
43 #include "qmsim.h"
45 /*-------------------Simulator part ---------------*/
47 void dumpq(Q* q)
48 {
49 printf("QDUMP: %d %d %d %d\n",q->size, q->ne, q->head,q->tail);
50 }
51 void qsim_close(Q * q)
52 {
53 if (!q) return;
54 if (q->q) free(q->q);
55 free(q);
56 printf("QSIM: freeing queue\n");
57 }
59 Q * qsim_create(nelem)
60 {
61 void *p;
62 Q * q;
63 if (!nelem) return NULL;
64 q = (Q*) calloc(1,sizeof(Q));
65 if (!q) return NULL;
67 p = (void **) calloc(nelem,sizeof(void *));
68 if (!p) { free(q); return NULL; }
70 q->size=nelem;
71 q->q = p ;
72 printf("QSIM CREATE .. "); dumpq(q);
73 return q;
74 }
76 int qsim_push(Q *q, void *p)
77 {
78 if (!q) return -1;
79 if (q->ne >= q->size) return-1;
80 q->q[q->tail] = p;
81 q->tail +=1;
82 q->ne+=1;
83 if (q->tail >= q->size) q->tail = 0;
84 //dumpq(q);
85 return 1;
86 }
87 void * qsim_pop(Q *q )
88 {
89 void * val;
90 if (!q) return NULL;
91 if (q->ne ==0) return NULL;
92 val = q->q[q->head];
93 q->head+=1;
94 if(q->head>= q->size) q->head=0;
95 q->ne -=1;
96 //dumpq(q);
97 return val;
98 }
100 //#define TEST_QSIM
101 #ifdef TEST_QSIM
102 main()
103 {
104 Q * q;
105 int i;
106 q= qsim_create(10);
107 for(i=1;i<11;i++) qsim_push(q, (void *) i);
109 for(;i<16;i++) { void * val; val = qsim_pop(q); printf("%d\n", (int) val);}
110 for(;i<21;i++) qsim_push(q, (void *) i);
111 for(;i<31;i++) { void * val; val = qsim_pop(q); printf("%d\n", (int) val);}
112 }
113 #endif