[ipc/ipcdev.git] / qnx / src / ipc3x_dev / ti / syslink / procMgr / hlos / knl / loaders / Elf / Qnx / dlw_client / Stack.h
1 /*
2 * Stack.h
3 *
4 * Interface to Stack
5 * ------------------
6 *
7 * This is an implementation of a type-independent stack implemented as
8 * a signly linked list class for C. It's basically a template class, but
9 * uses macros instead, so that it can be compiled with a C-only compiler.
10 *
11 * To define a Stack class:
12 * #include "Stack.h"
13 * TYPE_STACK_DEFINITION(object_type,Class_Identifier)
14 *
15 * In a separate C file:
16 * #include "Stack.h"
17 * TYPE_STACK_DEFINITION(object_type,Class_Identifier)
18 * TYPE_STACK_IMPLEMENTATION(object_type,Class_Identifier)
19 *
20 * Now, to create a stack:
21 * struct Class_Identifier_Stack name;
22 * Get it initialized to zero everywhere somehow, maybe like this:
23 * initialize_stack_Class_Identifier(&name);
24 *
25 * To add to the stack:
26 * push_Class_Identifier(&name, object);
27 *
28 * To access the top of the stack:
29 * Class_Identifier_Stack_Node *tos = name.top_ptr;
30 * do_something_to_(tos->value);
31 *
32 * To delete from the stack:
33 * if (name.size > 0) pop_Class_Identifier(&name);
34 *
35 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
36 *
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 *
45 * Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the
48 * distribution.
49 *
50 * Neither the name of Texas Instruments Incorporated nor the names of
51 * its contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 */
68 #ifndef STACK_H
69 #define STACK_H
71 #if defined (__KERNEL__)
72 #include <linux/types.h>
73 #else
74 #include <inttypes.h>
75 #endif
76 #include "dload_api.h"
78 /*****************************************************************************/
79 /* TYPE_STACK_DEFINITION() - Define structure specifications for a last-in, */
80 /* first-out linked list of t_name objects. */
81 /*****************************************************************************/
82 #define TYPE_STACK_DEFINITION(t, t_name) \
83 struct t_name##_Stack_Node_ \
84 { \
85 t value; \
86 struct t_name##_Stack_Node_* next_ptr; \
87 }; \
88 typedef struct t_name##_Stack_Node_ t_name##_Stack_Node; \
89 \
90 typedef struct \
91 { \
92 t_name##_Stack_Node* top_ptr; \
93 t_name##_Stack_Node* bottom_ptr; \
94 int size; \
95 } t_name##_Stack; \
96 \
97 extern void t_name##_initialize_stack(t_name##_Stack* stack); \
98 extern void t_name##_push(t_name##_Stack* stack, t to_push); \
99 extern t t_name##_pop(t_name##_Stack* stack);
101 /*****************************************************************************/
102 /* TYPE_STACK_IMPLEMENTATION() - Define member functions of new LIFO linked */
103 /* list "class" of t_name objects. */
104 /* */
105 /* <type>_initialize_stack() - clears the stack */
106 /* <type>_push() - pushes a <t> type object to the top of the stack */
107 /* <type>_pop() - pop a <t> type object from the top of the stack */
108 /* and provide access to it to the caller */
109 /*****************************************************************************/
110 #define TYPE_STACK_IMPLEMENTATION(t, t_name) \
111 void t_name##_initialize_stack (t_name##_Stack* stack) \
112 { \
113 stack->top_ptr = stack->bottom_ptr = NULL; \
114 stack->size = 0; \
115 } \
116 void t_name##_push(t_name##_Stack* stack, t to_push) \
117 { \
118 stack->size++; \
119 \
120 if(!stack->top_ptr) \
121 { \
122 stack->bottom_ptr = stack->top_ptr = \
123 (t_name##_Stack_Node*)(DLIF_malloc(sizeof(t_name##_Stack_Node))); \
124 if (NULL == stack->top_ptr) \
125 return; \
126 stack->top_ptr->next_ptr = NULL; \
127 } \
128 else \
129 { \
130 t_name##_Stack_Node* next_ptr = stack->top_ptr; \
131 stack->top_ptr = \
132 (t_name##_Stack_Node*)(DLIF_malloc(sizeof(t_name##_Stack_Node))); \
133 if (NULL == stack->top_ptr) \
134 return; \
135 stack->top_ptr->next_ptr = next_ptr; \
136 } \
137 \
138 stack->top_ptr->value = to_push; \
139 } \
140 \
141 t t_name##_pop(t_name##_Stack* stack) \
142 { \
143 t to_ret; \
144 t_name##_Stack_Node* next_ptr = stack->top_ptr->next_ptr; \
145 \
146 stack->size--; \
147 to_ret = stack->top_ptr->value; \
148 DLIF_free((void*)(stack->top_ptr)); \
149 \
150 if(!stack->size) \
151 stack->top_ptr = stack->bottom_ptr = NULL; \
152 else \
153 stack->top_ptr = next_ptr; \
154 \
155 return to_ret; \
156 }
158 #endif