xref: /netbsd/external/bsd/tre/dist/lib/tre-stack.h (revision d714042b)
163d4abf0Sagc /*
263d4abf0Sagc   tre-stack.h: Stack definitions
363d4abf0Sagc 
463d4abf0Sagc   This software is released under a BSD-style license.
563d4abf0Sagc   See the file LICENSE for details and copyright.
663d4abf0Sagc 
763d4abf0Sagc */
863d4abf0Sagc 
963d4abf0Sagc 
1063d4abf0Sagc #ifndef TRE_STACK_H
1163d4abf0Sagc #define TRE_STACK_H 1
1263d4abf0Sagc 
1363d4abf0Sagc #include "tre.h"
1463d4abf0Sagc 
1563d4abf0Sagc typedef struct tre_stack_rec tre_stack_t;
1663d4abf0Sagc 
1763d4abf0Sagc /* Creates a new stack object.	`size' is initial size in bytes, `max_size'
1863d4abf0Sagc    is maximum size, and `increment' specifies how much more space will be
1963d4abf0Sagc    allocated with realloc() if all space gets used up.	Returns the stack
2063d4abf0Sagc    object or NULL if out of memory. */
2163d4abf0Sagc tre_stack_t *
2263d4abf0Sagc tre_stack_new(int size, int max_size, int increment);
2363d4abf0Sagc 
2463d4abf0Sagc /* Frees the stack object. */
2563d4abf0Sagc void
2663d4abf0Sagc tre_stack_destroy(tre_stack_t *s);
2763d4abf0Sagc 
2863d4abf0Sagc /* Returns the current number of objects in the stack. */
2963d4abf0Sagc int
3063d4abf0Sagc tre_stack_num_objects(tre_stack_t *s);
3163d4abf0Sagc 
3263d4abf0Sagc /* Each tre_stack_push_*(tre_stack_t *s, <type> value) function pushes
3363d4abf0Sagc    `value' on top of stack `s'.  Returns REG_ESPACE if out of memory.
3463d4abf0Sagc    This tries to realloc() more space before failing if maximum size
3563d4abf0Sagc    has not yet been reached.  Returns REG_OK if successful. */
3663d4abf0Sagc #define declare_pushf(typetag, type)					      \
3763d4abf0Sagc   reg_errcode_t tre_stack_push_ ## typetag(tre_stack_t *s, type value)
3863d4abf0Sagc 
3963d4abf0Sagc declare_pushf(voidptr, void *);
40*d714042bSchristos declare_pushf(long, long);
4163d4abf0Sagc 
4263d4abf0Sagc /* Each tre_stack_pop_*(tre_stack_t *s) function pops the topmost
4363d4abf0Sagc    element off of stack `s' and returns it.  The stack must not be
4463d4abf0Sagc    empty. */
4563d4abf0Sagc #define declare_popf(typetag, type)		  \
4663d4abf0Sagc   type tre_stack_pop_ ## typetag(tre_stack_t *s)
4763d4abf0Sagc 
4863d4abf0Sagc declare_popf(voidptr, void *);
49*d714042bSchristos declare_popf(long, long);
50*d714042bSchristos 
51*d714042bSchristos #define tre_stack_pop_int(stack)	(int)tre_stack_pop_long(stack)
5263d4abf0Sagc 
5363d4abf0Sagc /* Just to save some typing. */
5463d4abf0Sagc #define STACK_PUSH(s, typetag, value)					      \
5563d4abf0Sagc   do									      \
5663d4abf0Sagc     {									      \
5763d4abf0Sagc       status = tre_stack_push_ ## typetag(s, value);			      \
5863d4abf0Sagc     }									      \
5963d4abf0Sagc   while (/*CONSTCOND*/0)
6063d4abf0Sagc 
6163d4abf0Sagc #define STACK_PUSHX(s, typetag, value)					      \
6263d4abf0Sagc   {									      \
6363d4abf0Sagc     status = tre_stack_push_ ## typetag(s, value);			      \
6463d4abf0Sagc     if (status != REG_OK)						      \
6563d4abf0Sagc       break;								      \
6663d4abf0Sagc   }
6763d4abf0Sagc 
6863d4abf0Sagc #define STACK_PUSHR(s, typetag, value)					      \
6963d4abf0Sagc   {									      \
7063d4abf0Sagc     reg_errcode_t _status;						      \
7163d4abf0Sagc     _status = tre_stack_push_ ## typetag(s, value);			      \
7263d4abf0Sagc     if (_status != REG_OK)						      \
7363d4abf0Sagc       return _status;							      \
7463d4abf0Sagc   }
7563d4abf0Sagc 
7663d4abf0Sagc #endif /* TRE_STACK_H */
7763d4abf0Sagc 
7863d4abf0Sagc /* EOF */
79