#include <stdbool.h>
#define STACK_ENTRY(node, type, member) \
((type*)((char*)(node) - (size_t)&((type*)0)->member))
struct h_stack_head {
struct h_stack_head *next;
};
typedef void (*free_stack_node_func) (struct h_stack_head *node);
struct h_stack {
struct h_stack_head *elements;
free_stack_node_func free_func;
};
static inline bool hstack_is_empty(struct h_stack *stack)
{
return stack->elements == NULL ? true : false;
}
static inline struct h_stack_head *hstack_pop(struct h_stack *stack)
{
if (hstack_is_empty(stack))
return NULL;
struct h_stack_head *result = stack->elements;
stack->elements = stack->elements->next;
result->next = NULL;
return result;
}
static inline void hstack_push(struct h_stack *stack, struct h_stack_head *element)
{
element->next = stack->elements;
stack->elements = element;
return;
}
static inline struct h_stack_head *hstack_peek(struct h_stack *stack)
{
return stack->elements;
}
static inline struct h_stack *hstack_create(free_stack_node_func free_func)
{
struct h_stack *hstack = (struct h_stack*) malloc(sizeof(*hstack));
hstack->free_func = free_func;
return hstack;
}
static inline void hstack_free(struct h_stack *hstack)
{
struct h_stack_head *node = hstack->elements;
while (node) {
struct h_stack_head *next = node->next;
hstack->free_func(node);
node = next;
}
return;
}