SystemCall run(block) can now exit the run if it returns false
[io/quag.git] / libs / basekit / source / Stack_inline.h
blob893c8a7ad89a9c41727c0c2809d73ccf379eb931
1 /*
2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
4 docDescription("""
5 Stack - array of void pointers
6 supports setting marks - when a mark is popped,
7 all stack items above it are popped as well
8 """)
9 */
11 #ifdef STACK_C
12 #define IO_IN_C_FILE
13 #endif
14 #include "Common_inline.h"
15 #ifdef IO_DECLARE_INLINES
17 IOINLINE void Stack_do_(const Stack *self, StackDoCallback *callback)
19 void **itemP = self->top;
20 intptr_t mark = self->lastMark;
22 while (itemP > self->items)
24 if (itemP - self->items == mark)
26 mark = (intptr_t)(*itemP);
28 else
30 (*callback)(*itemP);
33 itemP --;
37 IOINLINE void Stack_doUntilMark_(const Stack *self, StackDoCallback *callback)
39 void **itemP = self->top;
40 intptr_t mark = self->lastMark;
42 while (itemP > self->items)
44 if (itemP - self->items == mark)
46 return;
48 else
50 (*callback)(*itemP);
53 itemP --;
57 IOINLINE void Stack_clear(Stack *self)
59 self->top = self->items;
60 self->lastMark = 0;
63 IOINLINE size_t Stack_totalSize(const Stack *self)
65 return (self->top - self->items);
68 IOINLINE int Stack_count(const Stack *self)
70 return (self->top - self->items);
73 IOINLINE void Stack_push_(Stack *self, void *item)
75 self->top ++;
77 if (self->top == self->memEnd)
79 Stack_resize(self);
82 *(self->top) = item;
85 IOINLINE void Stack_pushMark(Stack *self)
87 Stack_push_(self, (void *)self->lastMark);
88 self->lastMark = self->top - self->items;
91 IOINLINE intptr_t Stack_pushMarkPoint(Stack *self)
93 Stack_push_(self, (void *)self->lastMark);
94 self->lastMark = self->top - self->items;
95 return self->lastMark;
98 IOINLINE void *Stack_pop(Stack *self)
100 void *top = *(self->top);
102 if (self->items != self->top)
104 #ifdef STACK_POP_CALLBACK
105 if(self->popCallback) self->popCallback(*(self->top));
106 #endif
107 self->top --;
110 return top;
113 IOINLINE void Stack_popMark(Stack *self)
115 #ifdef STACK_POP_CALLBACK
116 if(self->popCallback) Stack_doUntilMark_(self, self->popCallback);
117 #endif
119 self->top = self->items + self->lastMark - 1;
121 if (self->lastMark)
123 self->lastMark = (intptr_t)(self->items[self->lastMark]);
127 IOINLINE int Stack_popMarkPoint_(Stack *self, intptr_t mark)
129 while (self->lastMark && self->lastMark != mark)
131 Stack_popMark(self);
134 if (self->lastMark != mark)
136 return 0;
139 Stack_popMark(self);
140 return 1;
143 IOINLINE void Stack_clearTop(Stack *self)
145 Stack_popMark(self);
146 Stack_pushMark(self);
147 //self->top = self->items + self->lastMark;
150 IOINLINE void *Stack_top(const Stack *self)
152 return *(self->top);
155 IOINLINE void *Stack_at_(const Stack *self, int i)
157 return self->items[i + 1];
162 #undef IO_IN_C_FILE
163 #endif