Annotation of OpenXM/src/ox_ntl/oxstack.c, Revision 1.2
1.2 ! iwane 1: /* $OpenXM: OpenXM/src/ox_ntl/oxstack.c,v 1.1 2003/11/08 12:34:01 iwane Exp $ */
1.1 iwane 2:
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include <string.h>
6: #include <errno.h>
7:
8: #include "oxstack.h"
9:
10:
11: #define DPRINTF(x) printf x; fflush(stdout)
12:
13: #define OXSERV_INIT_STACK_SIZE 2048
14: #define OXSERV_EXT_STACK_SIZE 2048
15:
16: /*===========================================================================*
17: * Global Variables.
18: *===========================================================================*/
19: /* cmo stack */
20: static int G_ox_stack_size = 0;
21: static int G_ox_stack_pointer = 0;
1.2 ! iwane 22: static oxstack_node **G_ox_stack = NULL;
1.1 iwane 23:
24:
25: /*===========================================================================*
26: * CMO STACK FUNCTIONs
27: *===========================================================================*/
28: /*****************************************************************************
29: * return the number of cmo in the stack.
30: *
31: * PARAM : NONE
32: * RETURN : the number of cmo in the stack.
33: *****************************************************************************/
34: int
35: oxstack_get_stack_pointer()
36: {
37: return (G_ox_stack_pointer);
38: }
39:
40:
41: /*****************************************************************************
42: * initialize stack.
43: *
44: * PARAM : NONE
45: * RETURN : if success return OXSERV_SUCCESS, else OXSERV_FAILURE.
46: *****************************************************************************/
47: int
48: oxstack_init_stack(void)
49: {
50: free(G_ox_stack);
51:
52: G_ox_stack_pointer = 0;
53: G_ox_stack_size = OXSERV_INIT_STACK_SIZE;
1.2 ! iwane 54: G_ox_stack = (oxstack_node **)malloc(G_ox_stack_size * sizeof(oxstack_node *));
1.1 iwane 55: if (G_ox_stack == NULL) {
56: DPRINTF(("server: %d: %s\n", errno, strerror(errno)));
57: return (OXSERV_FAILURE);
58: }
59:
60: return (OXSERV_SUCCESS);
61: }
62:
63: /*****************************************************************************
64: *
65: * PARAM : NONE
66: * RETURN : if success return OXSERV_SUCCESS, else OXSERV_FAILURE.
67: *****************************************************************************/
68: int
69: oxstack_extend_stack(void)
70: {
71: int size2 = G_ox_stack_size + OXSERV_EXT_STACK_SIZE;
1.2 ! iwane 72: oxstack_node **stack2 = (oxstack_node **)malloc(size2 * sizeof(oxstack_node *));
1.1 iwane 73: if (stack2 == NULL) {
74: DPRINTF(("server: %d: %s\n", errno, strerror(errno)));
75: return (OXSERV_FAILURE);
76: }
77:
1.2 ! iwane 78: memcpy(stack2, G_ox_stack, G_ox_stack_size * sizeof(oxstack_node *));
1.1 iwane 79: free(G_ox_stack);
80:
81: G_ox_stack = stack2;
82: G_ox_stack_size = size2;
83:
84: return (OXSERV_SUCCESS);
85: }
86:
87: /*****************************************************************************
88: * push a cmo onto the topof the stack.
89: *
90: * PARAM : m : the cmo to be pushed on the stack.
91: * RETURN : if success return OXSERV_SUCCESS, else OXSERV_FAILURE.
92: *****************************************************************************/
93: int
1.2 ! iwane 94: oxstack_push(oxstack_node *m)
1.1 iwane 95: {
96: int ret;
97:
98: if (G_ox_stack_pointer >= G_ox_stack_size) {
99: ret = oxstack_extend_stack();
100: if (ret != OXSERV_SUCCESS)
101: return (ret);
102: }
103:
104: G_ox_stack[G_ox_stack_pointer] = m;
105: G_ox_stack_pointer++;
106:
107: return (OXSERV_SUCCESS);
108: }
109:
1.2 ! iwane 110: int
! 111: oxstack_push_cmo(cmo *c)
! 112: {
! 113: return (oxstack_push(oxstack_node_init(c)));
! 114: }
! 115:
! 116:
1.1 iwane 117: /*****************************************************************************
118: * remove thd CMO at the top of this stack and
119: * returns that cmo as the value of this function.
120: *
121: * PARAM : NONE
122: * RETURN : CMO at the top of the stack.
123: *****************************************************************************/
1.2 ! iwane 124: oxstack_node *
1.1 iwane 125: oxstack_pop(void)
126: {
1.2 ! iwane 127: oxstack_node *c;
1.1 iwane 128: if (G_ox_stack_pointer > 0) {
129: G_ox_stack_pointer--;
130: c = G_ox_stack[G_ox_stack_pointer];
131: G_ox_stack[G_ox_stack_pointer] = NULL;
132: return (c);
133: }
134: return (NULL);
135: }
136:
137: /*****************************************************************************
138: * return the cmo at the specified position in the stack without removing it from the stack.
139: *
140: * PARAM : i : position in the stack.
141: * RETURN: thd cmo at the specified position in the stack.
142: *****************************************************************************/
1.2 ! iwane 143: oxstack_node *
1.1 iwane 144: oxstack_get(int i)
145: {
146: if (i < G_ox_stack_pointer && i >= 0) {
147: return (G_ox_stack[i]);
148: }
149: return (NULL);
150: }
151:
152:
153: /*****************************************************************************
154: * return the cmo at the top of the stack without removing it from the stack.
155: * PARAM : NONE
156: * RETURN: the cmo at the top of the stack.
157: *****************************************************************************/
1.2 ! iwane 158: oxstack_node *
1.1 iwane 159: oxstack_peek(void)
160: {
161: return (oxstack_get(G_ox_stack_pointer - 1));
162: }
163:
164:
165:
166: /*****************************************************************************
167: * destroy
168: *
169: * PARAM : NONE
170: * RETURN: NONE
171: *****************************************************************************/
172: void
173: oxstack_dest(void)
174: {
175: free(G_ox_stack);
176: }
177:
178:
1.2 ! iwane 179: /*****************************************************************************
! 180: * destroy
! 181: *
! 182: * PARAM : NONE
! 183: * RETURN: NONE
! 184: *****************************************************************************/
! 185: oxstack_node *
! 186: oxstack_node_init(cmo *c)
! 187: {
! 188: oxstack_node *p;
! 189:
! 190: p = malloc(sizeof(oxstack_node));
! 191: memset(p, 0, sizeof(*p));
! 192:
! 193: p->c = c;
! 194: return (p);
! 195: }
! 196:
1.1 iwane 197:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>