1#include <stdlib.h>
2#include <assert.h>
3
4#define INIT_ENV_DEPTH 256
5
6static
7void tagstack_init(struct tagstack* tagstack) {
8  tagstack->depth=INIT_ENV_DEPTH;
9  tagstack->pos=-1;
10  tagstack->entry=(struct tagstack_entry*) malloc (tagstack->depth * sizeof(struct tagstack_entry));
11}
12
13static
14void tagstack_free(struct tagstack* tagstack) {
15  tagstack->depth=-1;
16  tagstack->pos=-1;
17  free(tagstack->entry);
18}
19
20INLINE
21static
22int tagstack_notempty(const struct tagstack* tagstack) {
23  return tagstack->pos >= 0;
24}
25
26/*
27static
28void tagstack_entry_debug(struct tagstack_entry item) {
29 tmpl_log(TMPL_LOG_DEBUG,"vcontext = %d value=%d\n",item.vcontext,item.value);
30}
31*/
32
33INLINE
34static
35struct tagstack_entry* tagstack_top(const struct tagstack* tagstack) {
36  return tagstack->entry+tagstack->pos;
37}
38
39static
40struct tagstack_entry tagstack_pop(struct tagstack* tagstack, int* is_underflow) {
41  if (is_underflow != NULL) *is_underflow=0;
42  if (tagstack->pos<0) {
43    tagstack->pos=0;
44    if (is_underflow != NULL) *is_underflow=1;
45    if (tagstack->depth<0) {
46      tmpl_log(TMPL_LOG_ERROR,"FATAL:stack error:tags stack is uninitialized\n");
47      tagstack_init(tagstack);
48    }
49  }
50  return *(tagstack->entry+ tagstack->pos--);
51}
52
53static
54void tagstack_push(struct tagstack* tagstack, struct tagstack_entry item) {
55  /* overflow check */
56  if (++(tagstack->pos)>=tagstack->depth) {
57    if (tagstack->depth<INIT_ENV_DEPTH) tagstack->depth=INIT_ENV_DEPTH;
58    tagstack->depth*=2;
59    tagstack->entry=(struct tagstack_entry*) realloc (tagstack->entry, tagstack->depth * sizeof(struct tagstack_entry));
60  }
61  *(tagstack->entry+tagstack->pos)=item;
62}
63
64/*
65 * Local Variables:
66 * mode: c
67 * End:
68 */
69