Stack

Specifies the stack structure. The following module follow the LIFO (Last-In-First-Out) order.

Author

Matheus T. dos Santos (tenoriomatheus0@gmail.com)

Version

0.1.0

Date

19/09/2021

Copyright

Matheus T. dos Santos all rights reserved (c) 2021

Defines

BP_STACK_INIT(buffer)

Macro to initialize a bp_stack.

Parameters
  • buffer – Buffer where the elements will be stored.

BP_STACK_START(buffer, size_)

Macro to initialize a bp_stack, which have initials elements.

Parameters
  • buffer – Buffer where the elements will be stored.

  • size_ – Number of elements already presentes in the buffer.

Functions

int bp_stack_pop(bp_stack_t *stack, void *el)

Remove the top element in the stack and put it in el argument variable.

Parameters
  • stack – Reference to the stack.

  • el – [out] Reference to a variable, where the removed element will be put.

Returns

0 on success.

Returns

-ENODEV if the ‘stack’ argument is NULL.

Returns

-ENOENT if the stack is empty.

void *bp_stack_peek(bp_stack_t *stack)

Get the top element in the stack.

Parameters

stack – Reference to the stack.

Returns

A reference to the desired element.

Returns

NULL if the ‘stack’ argument is NULL or the stack is empty.

int bp_stack_push(bp_stack_t *stack, void *el)

Push an element at the top of the stack.

Parameters
  • stack – Reference to the stack.

  • el – Reference to the element to be pushed.

Returns

0 on success, errno otherwise.

Returns

-ENODEV if the ‘stack’ or the ‘el’ argument is NULL.

Returns

-ENOMEM if the stack is full.

int bp_stack_clear(bp_stack_t *stack)

Drop all elements in the stack.

Warning

After this function the stack size is zero, but the elements stay in the buffer.

Parameters

stack – Reference to the stack.

Returns

0 on success.

Returns

-ENODEV if the ‘stack’ argument is NULL.

usize bp_stack_size(bp_stack_t *stack)

Get a iterator to walk through the stack.

Parameters

stack – Reference to the stack.

Returns

The size of the stack.

Returns

0 if the ‘stack’ argument is NULL.

bp_iter_t bp_stack_iter(bp_stack_t *stack)

Get a iterator to walk through the stack. The traversal order is from top to the bottom of the stack.

Warning

This function doesn’t check if the stack argument is null. So if this argument is null, a crash will occur. That check must be done outside the function.

Parameters

stack – Reference to the stack.

Returns

A new iterator instance for the stack.

struct bp_stack_t
#include <bp_stack.h>

Struct with metadata about an external buffer.

Note

This struct need an external buffer to work properly.

Public Members

usize _element_size

Size (in bytes) of a single element in the stack.

usize _capacity

Maximum number of elements in the stack

usize _size

Current number of elements in the array.

u8_t *_buffer

Reference to the buffer, where the elements will be stored.