1 /* -*- mode: c ; c-file-style: "canonware-c-style" -*-
2  ******************************************************************************
3  *
4  * Copyright (C) 1996-2005 Jason Evans <jasone@canonware.com>.
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice(s), this list of conditions and the following disclaimer
12  *    unmodified other than the allowable addition of one or more
13  *    copyright notices.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice(s), this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  ******************************************************************************
32  *
33  * Version: Onyx 5.1.2
34  *
35  ******************************************************************************/
36 
37 #ifndef CW_USE_INLINES
38 bool
39 nxoe_l_stack_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter);
40 
41 cw_nxoe_t *
42 nxoe_l_stack_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset);
43 #endif
44 
45 #if (defined(CW_USE_INLINES) || defined(CW_NXO_STACK_C_))
46 CW_INLINE bool
nxoe_l_stack_delete(cw_nxoe_t * a_nxoe,uint32_t a_iter)47 nxoe_l_stack_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter)
48 {
49     cw_nxoe_stack_t *stack;
50     uint32_t i, count;
51 
52     stack = (cw_nxoe_stack_t *) a_nxoe;
53 
54     cw_check_ptr(stack);
55     cw_dassert(stack->nxoe.magic == CW_NXOE_MAGIC);
56     cw_assert(stack->nxoe.type == NXOT_STACK);
57 
58     /* Deallocate spares. */
59     for (i = 0; i < stack->nspare; i++)
60     {
61 	nxa_free(stack->spare[i], sizeof(cw_nxo_t));
62     }
63 
64     /* Deallocate nxo's. */
65     for (i = 0, count = stack->aend - stack->abeg; i < count; i++)
66     {
67 	nxa_free(stack->a[stack->abase + stack->abeg + i], sizeof(cw_nxo_t));
68     }
69 
70 #ifdef CW_THREADS
71     if (stack->nxoe.locking)
72     {
73 	mtx_delete(&stack->lock);
74     }
75 #endif
76 
77     /* Deallocate internal array. */
78     nxa_free(stack->a, stack->ahlen * 2 * sizeof(cw_nxo_t *));
79 
80     /* Deallocate the container structure. */
81     nxa_free(stack, sizeof(cw_nxoe_stack_t));
82 
83     return false;
84 }
85 
86 CW_INLINE cw_nxoe_t *
nxoe_l_stack_ref_iter(cw_nxoe_t * a_nxoe,bool a_reset)87 nxoe_l_stack_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset)
88 {
89     cw_nxoe_t *retval;
90     cw_nxoe_stack_t *stack;
91     uint32_t count;
92     /* Used for remembering the current state of reference iteration.  This
93      * function is only called by the garbage collector, so using a static
94      * variable works fine. */
95     static uint32_t iter;
96 
97     stack = (cw_nxoe_stack_t *) a_nxoe;
98 
99     if (a_reset)
100     {
101 	iter = 0;
102     }
103 
104     retval = NULL;
105 #ifdef CW_THREADS
106     switch (stack->rstate)
107     {
108 	case RSTATE_NONE:
109 	{
110 #endif
111 	    for (count = stack->aend - stack->abeg;
112 		 retval == NULL && iter < count;
113 		 iter++)
114 	    {
115 		retval = nxo_nxoe_get(stack->a[stack->abase + stack->abeg
116 					       + iter]);
117 	    }
118 #ifdef CW_THREADS
119 	    break;
120 	}
121 	case RSTATE_RMASK:
122 	{
123 	    for (count = stack->aend - stack->abeg;
124 		 retval == NULL && iter < count;
125 		 iter++)
126 	    {
127 		if (stack->abeg + iter >= stack->rbeg
128 		    && stack->abeg + iter < stack->rend)
129 		{
130 		    /* Masked. */
131 		    retval = nxo_nxoe_get(stack->r[stack->rbase + stack->rbeg
132 						   + iter]);
133 		}
134 		else
135 		{
136 		    retval = nxo_nxoe_get(stack->a[stack->abase + stack->abeg
137 						   + iter]);
138 		}
139 	    }
140 	    break;
141 	}
142 	case RSTATE_RONLY:
143 	{
144 	    for (count = stack->rend - stack->rbeg;
145 		 retval == NULL && iter < count;
146 		 iter++)
147 	    {
148 		retval = nxo_nxoe_get(stack->r[stack->rbase + stack->rbeg
149 					       + iter]);
150 	    }
151 	    break;
152 	}
153 	default:
154 	{
155 	    cw_not_reached();
156 	}
157     }
158 #endif
159 
160     return retval;
161 }
162 #endif /* (defined(CW_USE_INLINES) || defined(CW_NXO_STACK_C_)) */
163