]>
git.saurik.com Git - apple/libc.git/blob - regex/TRE/lib/tre-stack.c
8ad89c8ce65eded74c2f7095440b94b1427b7ab1
2 tre-stack.c - Simple stack implementation
4 This software is released under a BSD-style license.
5 See the file LICENSE for details and copyright.
11 #endif /* HAVE_CONFIG_H */
15 #include "tre-internal.h"
16 #include "tre-stack.h"
19 union tre_stack_item
{
24 struct tre_stack_rec
{
29 union tre_stack_item
*stack
;
34 tre_stack_new(int size
, int max_size
, int increment
)
38 s
= xmalloc(sizeof(*s
));
41 s
->stack
= xmalloc(sizeof(*s
->stack
) * size
);
48 s
->max_size
= max_size
;
49 s
->increment
= increment
;
56 tre_stack_destroy(tre_stack_t
*s
)
63 tre_stack_num_objects(tre_stack_t
*s
)
69 tre_stack_push(tre_stack_t
*s
, union tre_stack_item value
)
73 s
->stack
[s
->ptr
] = value
;
78 if (s
->size
>= s
->max_size
)
80 DPRINT(("tre_stack_push: stack full\n"));
85 union tre_stack_item
*new_buffer
;
87 DPRINT(("tre_stack_push: trying to realloc more space\n"));
88 new_size
= s
->size
+ s
->increment
;
89 if (new_size
> s
->max_size
)
90 new_size
= s
->max_size
;
91 new_buffer
= xrealloc(s
->stack
, sizeof(*new_buffer
) * new_size
);
92 if (new_buffer
== NULL
)
94 DPRINT(("tre_stack_push: realloc failed.\n"));
97 DPRINT(("tre_stack_push: realloc succeeded.\n"));
98 assert(new_size
> s
->size
);
100 s
->stack
= new_buffer
;
101 tre_stack_push(s
, value
);
107 #define define_pushf(typetag, type) \
108 declare_pushf(typetag, type) { \
109 union tre_stack_item item; \
110 item.typetag ## _value = value; \
111 return tre_stack_push(s, item); \
114 define_pushf(int, int)
115 define_pushf(voidptr
, void *)
117 #define define_popf(typetag, type) \
118 declare_popf(typetag, type) { \
119 return s->stack[--s->ptr].typetag ## _value; \
122 define_popf(int, int)
123 define_popf(voidptr
, void *)