Stack API

Libmodule offers an easy to use stack implementation, provided by <module/stack.h> header.
It is used internally to store module’s stack for become/unbecome methods.

Structures

typedef enum {
    STACK_WRONG_PARAM = -4,
    STACK_MISSING,
    STACK_ERR,
    STACK_OMEM,
    STACK_OK
} stack_ret_code;

/* Callback for stack_iterate */
typedef stack_ret_code (*stack_cb)(void *, void *);

/* Incomplete struct declaration for stack */
typedef struct _stack stack_t;

API

Where not specified, these functions return a stack_ret_code.

stack_new()

Create a new stack_t object.

Returns:pointer to newly allocated stack_t.
stack_iterate(s, fn, userptr)

Iterate a stack calling cb on each element until STACK_OK is returned (or end of stack is reached). Returns STACK_MISSING if stack is NULL.

Parameters:
  • s (stack_t *) – pointer to stack_t
  • fn (const stack_cb) – callback to be called
  • userptr (void *) – userdata to be passed to callback as first parameter
stack_push(s, val, autofree)

Push a value on top of stack. Note that if autofree is true, data willbe automatically freed when calling stack_free() on the stack.

Parameters:
  • s (stack_t *) – pointer to stack_t
  • val (void *) – value to be put inside stack
  • autofree (const bool) – whether to autofree val upon stack_pop/stack_clear/stack_free
stack_pop(s)

Pop a value from top of stack, removing it from stack.

Parameters:
  • s (stack_t *) – pointer to stack_t
Returns:

void pointer to value, on NULL on error.

stack_peek(s)

Return top-of-stack element, without removing it.

Parameters:
  • s (stack_t *) – pointer to stack_t
Returns:

void pointer to value, on NULL on error.

stack_clear(s)

Clears a stack object by deleting any object inside stack, and eventually freeing it too if marked with autofree.

Parameters:
  • s (stack_t *) – pointer to stack_t
stack_free(s)

Free a stack object (it internally calls stack_clear too).

Parameters:
  • s (stack_t *) – pointer to stack_t
stack_length(s)

Get stack length.

Parameters:
  • s (stack_t *) – pointer to stack_t
Returns:

stack length or a stack_ret_code if any error happens (stack_t is null).