Small, Fast S-Expression Library
Data Structures | Macros | Typedefs | Functions
faststack.h File Reference

Implementation of a fast stack with smart memory management. More...

Go to the source code of this file.

Data Structures

struct  stack_level
 
struct  stack_wrapper
 

Macros

#define top_data(s)   (s->top->data)
 
#define empty_stack(s)   (s->top == NULL)
 

Typedefs

typedef struct stack_level stack_lvl_t
 
typedef struct stack_wrapper faststack_t
 

Functions

faststack_tmake_stack ()
 
void destroy_stack (faststack_t *s)
 
faststack_tpush (faststack_t *cur_stack, void *data)
 
stack_lvl_tpop (faststack_t *s)
 

Detailed Description

Implementation of a fast stack with smart memory management.

Macro Definition Documentation

◆ empty_stack

#define empty_stack (   s)    (s->top == NULL)

Given a stack s, check to see if the stack is empty or not. Value is boolean true or false.

◆ top_data

#define top_data (   s)    (s->top->data)

Given a stack s, examine the data pointer at the top.

Typedef Documentation

◆ faststack_t

typedef struct stack_wrapper faststack_t

Wrapper around the stack levels - keeps a pointer to the current top and bottom of the stack and a count of the current height. This allows the top to have non-null above pointer resulting from previously allocated stack levels that may be recycled later without malloc overhead.

◆ stack_lvl_t

typedef struct stack_level stack_lvl_t

Structure representing a single level in the stack. Has a pointer to the level above and below itself and a pointer to a generic blob of data associated with this level.

Function Documentation

◆ destroy_stack()

void destroy_stack ( faststack_t s)

Given a stack structure, destroy it and free all of the stack levels. Important note : This function does not free the data pointed to from each level of the stack - the user is responsible for freeing this data themselves before calling this function to prevent memory leakage.

◆ make_stack()

faststack_t* make_stack ( )

functions Return a pointer to an empty stack structure. If the return value is NULL, one should check sexp_errno to determine why.

◆ pop()

stack_lvl_t* pop ( faststack_t s)

Given a stack, pop a level off and return a pointer to that level. The user is responsible for extracting the data, but the stack_lvl_t structures pointed to from the level (above and below) should be left alone. If NULL is returned, either the stack contained nothing, or the incoming stack s was NULL. Consult sexp_errno to determine which was the case – SEXP_ERR_BAD_STACK indicates a null stack was passed in.

◆ push()

faststack_t* push ( faststack_t cur_stack,
void *  data 
)

Given a stack, push a new level on referring to the data pointer. If a new level cannot be allocated, NULL is returned and sexp_errno is set with the appropriate error condition. Memory allocation errors will result in SEXP_ERR_MEMORY, while a null stack will result in SEXP_ERR_BAD_STACK.