mfiutil(8): Use MAN instead of MAN8.
[dragonfly.git] / contrib / tre / lib / tre-stack.c
blob8ad89c8ce65eded74c2f7095440b94b1427b7ab1
1 /*
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.
7 */
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif /* HAVE_CONFIG_H */
12 #include <stdlib.h>
13 #include <assert.h>
15 #include "tre-internal.h"
16 #include "tre-stack.h"
17 #include "xmalloc.h"
19 union tre_stack_item {
20 void *voidptr_value;
21 int int_value;
24 struct tre_stack_rec {
25 int size;
26 int max_size;
27 int increment;
28 int ptr;
29 union tre_stack_item *stack;
33 tre_stack_t *
34 tre_stack_new(int size, int max_size, int increment)
36 tre_stack_t *s;
38 s = xmalloc(sizeof(*s));
39 if (s != NULL)
41 s->stack = xmalloc(sizeof(*s->stack) * size);
42 if (s->stack == NULL)
44 xfree(s);
45 return NULL;
47 s->size = size;
48 s->max_size = max_size;
49 s->increment = increment;
50 s->ptr = 0;
52 return s;
55 void
56 tre_stack_destroy(tre_stack_t *s)
58 xfree(s->stack);
59 xfree(s);
62 int
63 tre_stack_num_objects(tre_stack_t *s)
65 return s->ptr;
68 static reg_errcode_t
69 tre_stack_push(tre_stack_t *s, union tre_stack_item value)
71 if (s->ptr < s->size)
73 s->stack[s->ptr] = value;
74 s->ptr++;
76 else
78 if (s->size >= s->max_size)
80 DPRINT(("tre_stack_push: stack full\n"));
81 return REG_ESPACE;
83 else
85 union tre_stack_item *new_buffer;
86 int new_size;
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"));
95 return REG_ESPACE;
97 DPRINT(("tre_stack_push: realloc succeeded.\n"));
98 assert(new_size > s->size);
99 s->size = new_size;
100 s->stack = new_buffer;
101 tre_stack_push(s, value);
104 return REG_OK;
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 *)
125 /* EOF */