1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 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;
31 * Return the address of the items
33 uint32_t *stack_items(struct stack *st)
38 /* Return the size of the stack
41 stack_size(struct stack *st)
46 /* Check if the stack is empty
49 stack_is_empty(struct stack *st)
51 return st->top == STACK_EMPTY;
54 /* Check if the stack is full
57 stack_is_full(struct stack *st)
59 return st->top == st->max - 1;
62 /* Add element x to the stack
65 stack_push(struct stack *st, uint32_t x)
67 if (stack_is_full(st))
70 /* add an element and increments the top index
72 st->items[++st->top] = x;
77 /* Pop top element x from the stack and return
78 * in user provided location.
81 stack_pop(struct stack *st, uint32_t *x)
83 if (stack_is_empty(st))
86 *x = st->items[st->top];
94 void stack_dump(struct stack *st)
98 printf("top=%d\n", st->top);
99 printf("max=%d\n", st->max);
102 printf("stack is empty\n");
106 for (i = 0; i < st->max + 7 / 8; i++) {
107 printf("item[%d] 0x%08x", i, st->items[i]);
109 for (j = 0; j < 7; j++) {
110 if (i++ < st->max - 1)
111 printf(" 0x%08x", st->items[i]);