|
1 /* |
|
2 * stack.c |
|
3 * |
|
4 * Copyright(c) 1998 - 2010 Texas Instruments. All rights reserved. |
|
5 * All rights reserved. |
|
6 * |
|
7 * This program and the accompanying materials are made available under the |
|
8 * terms of the Eclipse Public License v1.0 or BSD License which accompanies |
|
9 * this distribution. The Eclipse Public License is available at |
|
10 * http://www.eclipse.org/legal/epl-v10.html and the BSD License is as below. |
|
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 * * Redistributions in binary form must reproduce the above copyright |
|
19 * notice, this list of conditions and the following disclaimer in |
|
20 * the documentation and/or other materials provided with the |
|
21 * distribution. |
|
22 * * Neither the name Texas Instruments nor the names of its |
|
23 * contributors may be used to endorse or promote products derived |
|
24 * from this software without specific prior written permission. |
|
25 * |
|
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
37 */ |
|
38 |
|
39 /** \file stack.c |
|
40 * \brief Seport module API |
|
41 * |
|
42 * \see stack.h |
|
43 */ |
|
44 |
|
45 /***************************************************************************/ |
|
46 /* */ |
|
47 /* MODULE: stack.c */ |
|
48 /* PURPOSE: Stack module implementation */ |
|
49 /* */ |
|
50 /***************************************************************************/ |
|
51 |
|
52 |
|
53 #define __FILE_ID__ FILE_ID_133 |
|
54 #include "tidef.h" |
|
55 #include "osApi.h" |
|
56 #include "stack.h" |
|
57 |
|
58 |
|
59 /** |
|
60 * \date 30-May-2006\n |
|
61 * \brief initialize stack object |
|
62 * |
|
63 * Function Scope \e Public.\n |
|
64 * \param pStack - pointer to the Stack_t structure\n |
|
65 * \param hOS - handle to the OS object\n |
|
66 * \param uElemSize - size of a one stack element\n |
|
67 * \param uDep - stack depth\n |
|
68 * \param pBuf - pointer to the stack buffer; if NULL a memory for the stack buffer will be dynamically allocated\n |
|
69 * \param fCpy - pointer to function copying the stack element; if NULL a default copy function will be used\n |
|
70 * \return 0 - on success, -1 - on failure\n |
|
71 */ |
|
72 unsigned stackInit |
|
73 ( |
|
74 Stack_t *pStack, |
|
75 TI_HANDLE hOs, |
|
76 unsigned uElemSize, |
|
77 unsigned uDep, |
|
78 void *pBuf, |
|
79 void (*fCpy) (TI_HANDLE, void*, void*, unsigned) |
|
80 ) |
|
81 { |
|
82 pStack->hOs = hOs; |
|
83 pStack->uPtr = 0; |
|
84 pStack->uElemSize = uElemSize; |
|
85 pStack->uDep = uDep * uElemSize; |
|
86 |
|
87 if (pBuf) |
|
88 { |
|
89 pStack->pBuf = pBuf; |
|
90 pStack->bBuf = 0; |
|
91 } |
|
92 |
|
93 else |
|
94 { |
|
95 pStack->pBuf = _os_memoryAlloc (hOs, pStack->uDep); |
|
96 pStack->bBuf = TI_TRUE; |
|
97 } |
|
98 |
|
99 if (fCpy) |
|
100 pStack->fCpy = fCpy; |
|
101 else |
|
102 pStack->fCpy = os_memoryCopy; |
|
103 |
|
104 return 0; |
|
105 } |
|
106 |
|
107 |
|
108 /** |
|
109 * \date 30-May-2006\n |
|
110 * \brief destroy stack object |
|
111 * |
|
112 * Function Scope \e Public.\n |
|
113 * \param pStack - pointer to the Stack_t structure\n |
|
114 * \return 0 - on success, -1 - on failure\n |
|
115 */ |
|
116 unsigned stackDestroy (Stack_t *pStack) |
|
117 { |
|
118 if (pStack->bBuf) |
|
119 _os_memoryFree (pStack->hOs, pStack->pBuf, pStack->uDep); |
|
120 |
|
121 return 0; |
|
122 } |
|
123 |
|
124 |
|
125 /** |
|
126 * \date 30-May-2006\n |
|
127 * \brief destroy stack object |
|
128 * |
|
129 * Function Scope \e Public.\n |
|
130 * \param pStack - pointer to the Stack_t structure\n |
|
131 * \param pVal - the pointer to the pushed value\n |
|
132 * \return 0 - on success, -1 - on failure\n |
|
133 */ |
|
134 unsigned stackPush (Stack_t *pStack, void *pVal) |
|
135 { |
|
136 if (pStack->uPtr < pStack->uDep) |
|
137 { |
|
138 pStack->fCpy (pStack->hOs, (unsigned char*)pStack->pBuf + pStack->uPtr, pVal, pStack->uElemSize); |
|
139 pStack->uPtr += pStack->uElemSize; |
|
140 |
|
141 return 0; |
|
142 } |
|
143 |
|
144 return -1; |
|
145 } |
|
146 |
|
147 |
|
148 /** |
|
149 * \date 30-May-2006\n |
|
150 * \brief destroy stack object |
|
151 * |
|
152 * Function Scope \e Public.\n |
|
153 * \param pStack - pointer to the Stack_t structure\n |
|
154 * \param pVal - the pointer to the popped value\n |
|
155 * \return 0 - on success, -1 - on failure\n |
|
156 */ |
|
157 unsigned stackPop (Stack_t *pStack, void *pVal) |
|
158 { |
|
159 if (pStack->uPtr > 0) |
|
160 { |
|
161 pStack->uPtr -= pStack->uElemSize; |
|
162 pStack->fCpy (pStack->hOs, pVal, (unsigned char*)pStack->pBuf + pStack->uPtr, pStack->uElemSize); |
|
163 |
|
164 return 0; |
|
165 } |
|
166 |
|
167 return -1; |
|
168 } |