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