| 1 |
|
| #ifndef _XH_STACK_H_ |
| 2 |
|
| #define _XH_STACK_H_ |
| 3 |
|
| |
| 4 |
|
| #include "xh_config.h" |
| 5 |
|
| #include "xh_core.h" |
| 6 |
|
| |
| 7 |
|
| typedef struct { |
| 8 |
|
| void *elts; |
| 9 |
|
| size_t size; |
| 10 |
|
| size_t nelts; |
| 11 |
|
| size_t top; |
| 12 |
|
| } xh_stack_t; |
| 13 |
|
| |
| 14 |
|
| XH_INLINE void * |
| 15 |
0 |
| xh_stack_push(xh_stack_t *st) |
| 16 |
|
| { |
| 17 |
0 |
▶ | if (st->top >= st->nelts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->top >= st->nelts) { |
0 |
0 |
0 |
|
| 18 |
0 |
| st->nelts *= 2; |
| 19 |
0 |
▶ | if ((st->elts = realloc(st->elts, st->nelts * st->size)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((st->elts = realloc(st->elts, st->nelts * st->size)) == NULL) { |
0 |
0 |
0 |
|
| 20 |
0 |
| croak("Memory allocation error"); |
| 21 |
|
| } |
| 22 |
|
| } |
| 23 |
0 |
| return (void *) (XH_CHAR_CAST st->elts + st->top++ * st->size); |
| 24 |
|
| } |
| 25 |
|
| |
| 26 |
|
| XH_INLINE void * |
| 27 |
|
| xh_stack_pop(xh_stack_t *st) |
| 28 |
|
| { |
| 29 |
|
| return st->top == 0 ? NULL : (void *) (XH_CHAR_CAST st->elts + --st->top * st->size); |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| XH_INLINE xh_bool_t |
| 33 |
|
| xh_stack_empty(xh_stack_t *st) |
| 34 |
|
| { |
| 35 |
|
| return st->top == 0 ? TRUE : FALSE; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| void xh_stack_init(xh_stack_t *st, xh_uint_t nelts, size_t size); |
| 39 |
|
| void xh_stack_destroy(xh_stack_t *st); |
| 40 |
|
| |
| 41 |
|
| #endif /* _XH_STACK_H_ */ |