00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CUCON_STACK_H
00019 #define CUCON_STACK_H
00020
00021 #include <cucon/fwd.h>
00022
00023 CU_BEGIN_DECLARATIONS
00024
00025 void *cuconP_stack_expand(cucon_stack_t stack, size_t size);
00026 void cuconP_stack_shrink(cucon_stack_t stack);
00027 #define cuconP_STACK_NOADDRESS NULL
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 struct cucon_stack
00042 {
00043 struct cucon_stack *prev;
00044 char *begin;
00045 char *sp;
00046 char *end;
00047 };
00048
00049
00050 void cucon_stack_init(cucon_stack_t stack);
00051
00052
00053 void cucon_stack_init_copy(cucon_stack_t dst, cucon_stack_t src);
00054
00055
00056 CU_SINLINE cu_bool_t cucon_stack_is_empty(cucon_stack_t stack)
00057 { return stack->begin == cuconP_STACK_NOADDRESS; }
00058
00059
00060 void cucon_stack_swap(cucon_stack_t stack0, cucon_stack_t stack1);
00061
00062
00063
00064 size_t cucon_stack_size(cucon_stack_t stack);
00065
00066
00067
00068 CU_SINLINE size_t cucon_stack_size_ptr(cucon_stack_t stack)
00069 { return cucon_stack_size(stack)/sizeof(void *); }
00070
00071
00072 CU_SINLINE size_t cucon_stack_size_int(cucon_stack_t stack)
00073 { return cucon_stack_size(stack)/sizeof(int); }
00074
00075
00076 void *cucon_stack_at(cucon_stack_t stack, size_t pos);
00077
00078 CU_SINLINE void *cucon_stack_at_ptr(cucon_stack_t stack, size_t pos)
00079 { return *(void **)cucon_stack_at(stack, pos*sizeof(void *)); }
00080
00081 CU_SINLINE int cucon_stack_at_int(cucon_stack_t stack, size_t pos)
00082 { return *(int *)cucon_stack_at(stack, pos*sizeof(int)); }
00083
00084
00085 CU_SINLINE void *cucon_stack_top(cucon_stack_t stack)
00086 { return stack->sp; }
00087
00088
00089 CU_SINLINE void *cucon_stack_top_ptr(cucon_stack_t stack)
00090 { return *(void **)cucon_stack_top(stack); }
00091
00092
00093 CU_SINLINE int cucon_stack_top_int(cucon_stack_t stack)
00094 { return *(int *)cucon_stack_top(stack); }
00095
00096
00097
00098
00099 void *cucon_stack_continuous_top(cucon_stack_t stack, size_t size);
00100
00101
00102
00103 CU_SINLINE void *
00104 cucon_stack_alloc(cucon_stack_t stack, size_t size)
00105 {
00106 char *sp = stack->sp -= size;
00107 if (sp - stack->begin < 0)
00108 return cuconP_stack_expand(stack, size);
00109 else
00110 return sp;
00111 }
00112
00113
00114 CU_SINLINE void
00115 cucon_stack_free(cucon_stack_t stack, size_t size)
00116 {
00117 if (size) {
00118 stack->sp += size;
00119 if (stack->sp - stack->end >= 0)
00120 cuconP_stack_shrink(stack);
00121 }
00122 }
00123
00124
00125 CU_SINLINE void cucon_stack_free_ptr(cucon_stack_t stack)
00126 { cucon_stack_free(stack, sizeof(void *)); }
00127
00128
00129 CU_SINLINE void cucon_stack_push_ptr(cucon_stack_t stack, void *ptr)
00130 { *(void **)cucon_stack_alloc(stack, sizeof(void *)) = ptr; }
00131
00132
00133 CU_SINLINE void cucon_stack_push_int(cucon_stack_t stack, int i)
00134 { *(int *)cucon_stack_alloc(stack, sizeof(int)) = i; }
00135
00136
00137 CU_SINLINE void *cucon_stack_pop_ptr(cucon_stack_t stack)
00138 {
00139 void *p = cucon_stack_top_ptr(stack);
00140 cucon_stack_free(stack, sizeof(void *));
00141 return p;
00142 }
00143
00144
00145 CU_SINLINE int cucon_stack_pop_int(cucon_stack_t stack)
00146 {
00147 int i = cucon_stack_top_int(stack);
00148 cucon_stack_free(stack, sizeof(int));
00149 return i;
00150 }
00151
00152
00153
00154
00155
00156 typedef struct cucon_stack_mark *cucon_stack_mark_t;
00157
00158
00159
00160
00161 CU_SINLINE cucon_stack_mark_t cucon_stack_mark(cucon_stack_t stack)
00162 { return (cucon_stack_mark_t)stack->sp; }
00163
00164
00165
00166
00167 void cucon_stack_unwind_to_mark(cucon_stack_t sk, cucon_stack_mark_t mark);
00168
00169
00170
00171
00172 struct cucon_stack_itr
00173 {
00174
00175 cucon_stack_t stack;
00176 char *sp;
00177 };
00178
00179
00180 void cucon_stack_itr_init(cucon_stack_itr_t itr, cucon_stack_t stack);
00181
00182
00183
00184 void *cucon_stack_itr_get(cucon_stack_itr_t itr, size_t size);
00185
00186
00187 void *cucon_stack_itr_get_ptr(cucon_stack_itr_t itr);
00188
00189 void cucon_stack_itr_advance(cucon_stack_itr_t itr, size_t size);
00190
00191
00192
00193 cu_ptr_source_t cucon_stack_ptr_source(cucon_stack_t stack);
00194
00195
00196
00197
00198
00199 typedef struct cucon_stack_it_s
00200 {
00201 cucon_stack_t stack;
00202 char *sp;
00203 } cucon_stack_it_t;
00204
00205
00206 CU_SINLINE cucon_stack_it_t
00207 cucon_stack_begin(cucon_stack_t stack)
00208 {
00209 cucon_stack_it_t it;
00210 it.stack = stack;
00211 it.sp = stack->sp;
00212 return it;
00213 }
00214
00215
00216 CU_SINLINE cu_bool_t cucon_stack_it_is_end(cucon_stack_it_t it)
00217 { return it.sp == cuconP_STACK_NOADDRESS; }
00218
00219 void cuconP_stack_it_fix(cucon_stack_it_t *it);
00220
00221
00222 CU_SINLINE void
00223 cucon_stack_it_advance(cucon_stack_it_t *it, size_t size)
00224 {
00225 it->sp += size;
00226 if (it->sp >= it->stack->end)
00227 cuconP_stack_it_fix(it);
00228 }
00229
00230
00231 CU_SINLINE void *cucon_stack_it_get(cucon_stack_it_t it) { return it.sp; }
00232
00233 #define cucon_stack_it_get_ptr(it) CUCON_STACK_IT_GET(it, void *)
00234
00235
00236
00237
00238 #define CUCON_STACK_SIZE(stack, elt_t) (cucon_stack_size(stack)/sizeof(elt_t))
00239
00240
00241 #define CUCON_STACK_TOP(stack, elt_t) (*(elt_t*)cucon_stack_top(stack))
00242
00243
00244
00245 #define CUCON_STACK_PUSH(stack, elt_t, obj) \
00246 ((void)(*(elt_t*)cucon_stack_alloc((stack), sizeof(elt_t)) = (obj)))
00247
00248
00249 #define CUCON_STACK_POP(stack, elt_t) cucon_stack_free((stack), sizeof(elt_t))
00250
00251
00252 #define CUCON_STACK_IT_ADVANCE(it, elt_t) \
00253 cucon_stack_it_advance(it, sizeof(elt_t))
00254
00255
00256 #define CUCON_STACK_IT_GET(it, elt_t) (*(elt_t*)cucon_stack_it_get(it))
00257
00258
00259 CU_END_DECLARATIONS
00260
00261 #endif