1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Broadcom
13 #define STACK_EMPTY -1
18 stack_init(int num_entries, uint32_t *items, struct stack *st)
20 if (items == NULL || st == NULL)
23 st->max = num_entries;
24 st->top = STACK_EMPTY;
30 /* Return the size of the stack
33 stack_size(struct stack *st)
38 /* Check if the stack is empty
41 stack_is_empty(struct stack *st)
43 return st->top == STACK_EMPTY;
46 /* Check if the stack is full
49 stack_is_full(struct stack *st)
51 return st->top == st->max - 1;
54 /* Add element x to the stack
57 stack_push(struct stack *st, uint32_t x)
59 if (stack_is_full(st))
62 /* add an element and increments the top index
64 st->items[++st->top] = x;
69 /* Pop top element x from the stack and return
70 * in user provided location.
73 stack_pop(struct stack *st, uint32_t *x)
75 if (stack_is_empty(st))
78 *x = st->items[st->top];
86 void stack_dump(struct stack *st)
90 printf("top=%d\n", st->top);
91 printf("max=%d\n", st->max);
94 printf("stack is empty\n");
98 for (i = 0; i < st->max + 7 / 8; i++) {
99 printf("item[%d] 0x%08x", i, st->items[i]);
101 for (j = 0; j < 7; j++) {
102 if (i++ < st->max - 1)
103 printf(" 0x%08x", st->items[i]);