| 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 | tre_stack_t * |
| 22 | tre_stack_new(int size, int max_size, int increment); |
| 23 | |
| 24 | /* Frees the stack object. */ |
| 25 | void |
| 26 | tre_stack_destroy(tre_stack_t *s); |
| 27 | |
| 28 | /* Returns the current number of objects in the stack. */ |
| 29 | 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 | reg_errcode_t tre_stack_push_ ## typetag(tre_stack_t *s, type value) |
| 38 | |
| 39 | declare_pushf(voidptr, void *); |
| 40 | declare_pushf(int, int); |
| 41 | |
| 42 | /* Each tre_stack_pop_*(tre_stack_t *s) function pops the topmost |
| 43 | element off of stack `s' and returns it. The stack must not be |
| 44 | empty. */ |
| 45 | #define declare_popf(typetag, type) \ |
| 46 | type tre_stack_pop_ ## typetag(tre_stack_t *s) |
| 47 | |
| 48 | declare_popf(voidptr, void *); |
| 49 | declare_popf(int, int); |
| 50 | |
| 51 | /* Just to save some typing. */ |
| 52 | #define STACK_PUSH(s, typetag, value) \ |
| 53 | do \ |
| 54 | { \ |
| 55 | status = tre_stack_push_ ## typetag(s, value); \ |
| 56 | } \ |
| 57 | while (/*CONSTCOND*/0) |
| 58 | |
| 59 | #define STACK_PUSHX(s, typetag, value) \ |
| 60 | { \ |
| 61 | status = tre_stack_push_ ## typetag(s, value); \ |
| 62 | if (status != REG_OK) \ |
| 63 | break; \ |
| 64 | } |
| 65 | |
| 66 | #define STACK_PUSHR(s, typetag, value) \ |
| 67 | { \ |
| 68 | reg_errcode_t _status; \ |
| 69 | _status = tre_stack_push_ ## typetag(s, value); \ |
| 70 | if (_status != REG_OK) \ |
| 71 | return _status; \ |
| 72 | } |
| 73 | |
| 74 | #endif /* TRE_STACK_H */ |
| 75 | |
| 76 | /* EOF */ |