1*2d9fd380Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause 2*2d9fd380Sjfb8856606 * Copyright(c) 2019-2020 Broadcom 3*2d9fd380Sjfb8856606 * All rights reserved. 4*2d9fd380Sjfb8856606 */ 5*2d9fd380Sjfb8856606 #ifndef _STACK_H_ 6*2d9fd380Sjfb8856606 #define _STACK_H_ 7*2d9fd380Sjfb8856606 8*2d9fd380Sjfb8856606 #include <stdio.h> 9*2d9fd380Sjfb8856606 #include <stdlib.h> 10*2d9fd380Sjfb8856606 #include <stdbool.h> 11*2d9fd380Sjfb8856606 #include <stdint.h> 12*2d9fd380Sjfb8856606 13*2d9fd380Sjfb8856606 /** Stack data structure 14*2d9fd380Sjfb8856606 */ 15*2d9fd380Sjfb8856606 struct stack { 16*2d9fd380Sjfb8856606 int max; /**< Maximum number of entries */ 17*2d9fd380Sjfb8856606 int top; /**< maximum value in stack */ 18*2d9fd380Sjfb8856606 uint32_t *items; /**< items in the stack */ 19*2d9fd380Sjfb8856606 }; 20*2d9fd380Sjfb8856606 21*2d9fd380Sjfb8856606 /** Initialize stack of uint32_t elements 22*2d9fd380Sjfb8856606 * 23*2d9fd380Sjfb8856606 * [in] num_entries 24*2d9fd380Sjfb8856606 * maximum number of elements in the stack 25*2d9fd380Sjfb8856606 * 26*2d9fd380Sjfb8856606 * [in] items 27*2d9fd380Sjfb8856606 * pointer to items (must be sized to (uint32_t * num_entries) 28*2d9fd380Sjfb8856606 * 29*2d9fd380Sjfb8856606 * s[in] st 30*2d9fd380Sjfb8856606 * pointer to the stack structure 31*2d9fd380Sjfb8856606 * 32*2d9fd380Sjfb8856606 * return 33*2d9fd380Sjfb8856606 * 0 for success 34*2d9fd380Sjfb8856606 */ 35*2d9fd380Sjfb8856606 int stack_init(int num_entries, 36*2d9fd380Sjfb8856606 uint32_t *items, 37*2d9fd380Sjfb8856606 struct stack *st); 38*2d9fd380Sjfb8856606 39*2d9fd380Sjfb8856606 /** Return the address of the stack contents 40*2d9fd380Sjfb8856606 * 41*2d9fd380Sjfb8856606 * [in] st 42*2d9fd380Sjfb8856606 * pointer to the stack 43*2d9fd380Sjfb8856606 * 44*2d9fd380Sjfb8856606 * return 45*2d9fd380Sjfb8856606 * pointer to the stack contents 46*2d9fd380Sjfb8856606 */ 47*2d9fd380Sjfb8856606 uint32_t *stack_items(struct stack *st); 48*2d9fd380Sjfb8856606 49*2d9fd380Sjfb8856606 /** Return the size of the stack 50*2d9fd380Sjfb8856606 * 51*2d9fd380Sjfb8856606 * [in] st 52*2d9fd380Sjfb8856606 * pointer to the stack 53*2d9fd380Sjfb8856606 * 54*2d9fd380Sjfb8856606 * return 55*2d9fd380Sjfb8856606 * number of elements 56*2d9fd380Sjfb8856606 */ 57*2d9fd380Sjfb8856606 int32_t stack_size(struct stack *st); 58*2d9fd380Sjfb8856606 59*2d9fd380Sjfb8856606 /** Check if the stack is empty 60*2d9fd380Sjfb8856606 * 61*2d9fd380Sjfb8856606 * [in] st 62*2d9fd380Sjfb8856606 * pointer to the stack 63*2d9fd380Sjfb8856606 * 64*2d9fd380Sjfb8856606 * return 65*2d9fd380Sjfb8856606 * true or false 66*2d9fd380Sjfb8856606 */ 67*2d9fd380Sjfb8856606 bool stack_is_empty(struct stack *st); 68*2d9fd380Sjfb8856606 69*2d9fd380Sjfb8856606 /** Check if the stack is full 70*2d9fd380Sjfb8856606 * 71*2d9fd380Sjfb8856606 * [in] st 72*2d9fd380Sjfb8856606 * pointer to the stack 73*2d9fd380Sjfb8856606 * 74*2d9fd380Sjfb8856606 * return 75*2d9fd380Sjfb8856606 * true or false 76*2d9fd380Sjfb8856606 */ 77*2d9fd380Sjfb8856606 bool stack_is_full(struct stack *st); 78*2d9fd380Sjfb8856606 79*2d9fd380Sjfb8856606 /** Add element x to the stack 80*2d9fd380Sjfb8856606 * 81*2d9fd380Sjfb8856606 * [in] st 82*2d9fd380Sjfb8856606 * pointer to the stack 83*2d9fd380Sjfb8856606 * 84*2d9fd380Sjfb8856606 * [in] x 85*2d9fd380Sjfb8856606 * value to push on the stack 86*2d9fd380Sjfb8856606 * return 87*2d9fd380Sjfb8856606 * 0 for success 88*2d9fd380Sjfb8856606 */ 89*2d9fd380Sjfb8856606 int stack_push(struct stack *st, uint32_t x); 90*2d9fd380Sjfb8856606 91*2d9fd380Sjfb8856606 /** Pop top element x from the stack and return 92*2d9fd380Sjfb8856606 * in user provided location. 93*2d9fd380Sjfb8856606 * 94*2d9fd380Sjfb8856606 * [in] st 95*2d9fd380Sjfb8856606 * pointer to the stack 96*2d9fd380Sjfb8856606 * 97*2d9fd380Sjfb8856606 * [in, out] x 98*2d9fd380Sjfb8856606 * pointer to where the value popped will be written 99*2d9fd380Sjfb8856606 * 100*2d9fd380Sjfb8856606 * return 101*2d9fd380Sjfb8856606 * 0 for success 102*2d9fd380Sjfb8856606 */ 103*2d9fd380Sjfb8856606 int stack_pop(struct stack *st, uint32_t *x); 104*2d9fd380Sjfb8856606 105*2d9fd380Sjfb8856606 /** Dump stack information 106*2d9fd380Sjfb8856606 * 107*2d9fd380Sjfb8856606 * Warning: Don't use for large stacks due to prints 108*2d9fd380Sjfb8856606 * 109*2d9fd380Sjfb8856606 * [in] st 110*2d9fd380Sjfb8856606 * pointer to the stack 111*2d9fd380Sjfb8856606 * 112*2d9fd380Sjfb8856606 * return 113*2d9fd380Sjfb8856606 * none 114*2d9fd380Sjfb8856606 */ 115*2d9fd380Sjfb8856606 void stack_dump(struct stack *st); 116*2d9fd380Sjfb8856606 117*2d9fd380Sjfb8856606 #endif /* _STACK_H_ */ 118