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