Home | History | Annotate | Line # | Download | only in lib
      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(long, long);
     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(long, long);
     50 
     51 #define tre_stack_pop_int(stack)	(int)tre_stack_pop_long(stack)
     52 
     53 /* Just to save some typing. */
     54 #define STACK_PUSH(s, typetag, value)					      \
     55   do									      \
     56     {									      \
     57       status = tre_stack_push_ ## typetag(s, value);			      \
     58     }									      \
     59   while (/*CONSTCOND*/(void)0,0)
     60 
     61 #define STACK_PUSHX(s, typetag, value)					      \
     62   {									      \
     63     status = tre_stack_push_ ## typetag(s, value);			      \
     64     if (status != REG_OK)						      \
     65       break;								      \
     66   }
     67 
     68 #define STACK_PUSHR(s, typetag, value)					      \
     69   {									      \
     70     reg_errcode_t _status;						      \
     71     _status = tre_stack_push_ ## typetag(s, value);			      \
     72     if (_status != REG_OK)						      \
     73       return _status;							      \
     74   }
     75 
     76 #endif /* TRE_STACK_H */
     77 
     78 /* EOF */
     79