]>
Commit | Line | Data |
---|---|---|
ad3c9f2a A |
1 | /* |
2 | tre-stack.h: Stack definitions | |
3 | ||
4 | This software is released under a BSD-style license. | |
5 | See the file LICENSE for details and copyright. | |
6 | ||
7 | */ | |
8 | ||
9 | ||
10 | #ifndef TRE_STACK_H | |
11 | #define TRE_STACK_H 1 | |
12 | ||
13 | #include "tre.h" | |
14 | ||
15 | typedef struct tre_stack_rec tre_stack_t; | |
16 | ||
17 | /* Creates a new stack object. `size' is initial size in bytes, `max_size' | |
18 | is maximum size, and `increment' specifies how much more space will be | |
19 | allocated with realloc() if all space gets used up. Returns the stack | |
20 | object or NULL if out of memory. */ | |
21 | __private_extern__ tre_stack_t * | |
22 | tre_stack_new(int size, int max_size, int increment); | |
23 | ||
24 | /* Frees the stack object. */ | |
25 | __private_extern__ void | |
26 | tre_stack_destroy(tre_stack_t *s); | |
27 | ||
28 | /* Returns the current number of objects in the stack. */ | |
29 | __private_extern__ int | |
30 | tre_stack_num_objects(tre_stack_t *s); | |
31 | ||
32 | /* Each tre_stack_push_*(tre_stack_t *s, <type> value) function pushes | |
33 | `value' on top of stack `s'. Returns REG_ESPACE if out of memory. | |
34 | This tries to realloc() more space before failing if maximum size | |
35 | has not yet been reached. Returns REG_OK if successful. */ | |
36 | #define declare_pushf(typetag, type) \ | |
37 | __private_extern__ reg_errcode_t tre_stack_push_ ## typetag(tre_stack_t *s, \ | |
38 | type value) | |
39 | ||
40 | declare_pushf(voidptr, void *); | |
41 | declare_pushf(int, int); | |
42 | ||
43 | /* Each tre_stack_pop_*(tre_stack_t *s) function pops the topmost | |
44 | element off of stack `s' and returns it. The stack must not be | |
45 | empty. */ | |
46 | #define declare_popf(typetag, type) \ | |
47 | __private_extern__ type tre_stack_pop_ ## typetag(tre_stack_t *s) | |
48 | ||
49 | declare_popf(voidptr, void *); | |
50 | declare_popf(int, int); | |
51 | ||
52 | /* Just to save some typing. */ | |
53 | #define STACK_PUSH(s, typetag, value) \ | |
54 | do \ | |
55 | { \ | |
56 | status = tre_stack_push_ ## typetag(s, value); \ | |
57 | } \ | |
58 | while (/*CONSTCOND*/0) | |
59 | ||
60 | #define STACK_PUSHX(s, typetag, value) \ | |
61 | { \ | |
62 | status = tre_stack_push_ ## typetag(s, value); \ | |
63 | if (status != REG_OK) \ | |
64 | break; \ | |
65 | } | |
66 | ||
67 | #define STACK_PUSHR(s, typetag, value) \ | |
68 | { \ | |
69 | reg_errcode_t _status; \ | |
70 | _status = tre_stack_push_ ## typetag(s, value); \ | |
71 | if (_status != REG_OK) \ | |
72 | return _status; \ | |
73 | } | |
74 | ||
75 | #endif /* TRE_STACK_H */ | |
76 | ||
77 | /* EOF */ |