xref: /f-stack/dpdk/drivers/net/bnxt/tf_core/stack.c (revision 2d9fd380)
1*2d9fd380Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2*2d9fd380Sjfb8856606  * Copyright(c) 2019-2020 Broadcom
3*2d9fd380Sjfb8856606  * All rights reserved.
4*2d9fd380Sjfb8856606  */
5*2d9fd380Sjfb8856606 
6*2d9fd380Sjfb8856606 #include <stdio.h>
7*2d9fd380Sjfb8856606 #include <stdlib.h>
8*2d9fd380Sjfb8856606 #include <stdbool.h>
9*2d9fd380Sjfb8856606 #include <stdint.h>
10*2d9fd380Sjfb8856606 #include <errno.h>
11*2d9fd380Sjfb8856606 #include "stack.h"
12*2d9fd380Sjfb8856606 
13*2d9fd380Sjfb8856606 #define STACK_EMPTY -1
14*2d9fd380Sjfb8856606 
15*2d9fd380Sjfb8856606 /* Initialize stack
16*2d9fd380Sjfb8856606  */
17*2d9fd380Sjfb8856606 int
stack_init(int num_entries,uint32_t * items,struct stack * st)18*2d9fd380Sjfb8856606 stack_init(int num_entries, uint32_t *items, struct stack *st)
19*2d9fd380Sjfb8856606 {
20*2d9fd380Sjfb8856606 	if (items == NULL || st == NULL)
21*2d9fd380Sjfb8856606 		return -EINVAL;
22*2d9fd380Sjfb8856606 
23*2d9fd380Sjfb8856606 	st->max = num_entries;
24*2d9fd380Sjfb8856606 	st->top = STACK_EMPTY;
25*2d9fd380Sjfb8856606 	st->items = items;
26*2d9fd380Sjfb8856606 
27*2d9fd380Sjfb8856606 	return 0;
28*2d9fd380Sjfb8856606 }
29*2d9fd380Sjfb8856606 
30*2d9fd380Sjfb8856606 /*
31*2d9fd380Sjfb8856606  * Return the address of the items
32*2d9fd380Sjfb8856606  */
stack_items(struct stack * st)33*2d9fd380Sjfb8856606 uint32_t *stack_items(struct stack *st)
34*2d9fd380Sjfb8856606 {
35*2d9fd380Sjfb8856606 	return st->items;
36*2d9fd380Sjfb8856606 }
37*2d9fd380Sjfb8856606 
38*2d9fd380Sjfb8856606 /* Return the size of the stack
39*2d9fd380Sjfb8856606  */
40*2d9fd380Sjfb8856606 int32_t
stack_size(struct stack * st)41*2d9fd380Sjfb8856606 stack_size(struct stack *st)
42*2d9fd380Sjfb8856606 {
43*2d9fd380Sjfb8856606 	return st->top + 1;
44*2d9fd380Sjfb8856606 }
45*2d9fd380Sjfb8856606 
46*2d9fd380Sjfb8856606 /* Check if the stack is empty
47*2d9fd380Sjfb8856606  */
48*2d9fd380Sjfb8856606 bool
stack_is_empty(struct stack * st)49*2d9fd380Sjfb8856606 stack_is_empty(struct stack *st)
50*2d9fd380Sjfb8856606 {
51*2d9fd380Sjfb8856606 	return st->top == STACK_EMPTY;
52*2d9fd380Sjfb8856606 }
53*2d9fd380Sjfb8856606 
54*2d9fd380Sjfb8856606 /* Check if the stack is full
55*2d9fd380Sjfb8856606  */
56*2d9fd380Sjfb8856606 bool
stack_is_full(struct stack * st)57*2d9fd380Sjfb8856606 stack_is_full(struct stack *st)
58*2d9fd380Sjfb8856606 {
59*2d9fd380Sjfb8856606 	return st->top == st->max - 1;
60*2d9fd380Sjfb8856606 }
61*2d9fd380Sjfb8856606 
62*2d9fd380Sjfb8856606 /* Add  element x to  the stack
63*2d9fd380Sjfb8856606  */
64*2d9fd380Sjfb8856606 int
stack_push(struct stack * st,uint32_t x)65*2d9fd380Sjfb8856606 stack_push(struct stack *st, uint32_t x)
66*2d9fd380Sjfb8856606 {
67*2d9fd380Sjfb8856606 	if (stack_is_full(st))
68*2d9fd380Sjfb8856606 		return -EOVERFLOW;
69*2d9fd380Sjfb8856606 
70*2d9fd380Sjfb8856606 	/* add an element and increments the top index
71*2d9fd380Sjfb8856606 	 */
72*2d9fd380Sjfb8856606 	st->items[++st->top] = x;
73*2d9fd380Sjfb8856606 
74*2d9fd380Sjfb8856606 	return 0;
75*2d9fd380Sjfb8856606 }
76*2d9fd380Sjfb8856606 
77*2d9fd380Sjfb8856606 /* Pop top element x from the stack and return
78*2d9fd380Sjfb8856606  * in user provided location.
79*2d9fd380Sjfb8856606  */
80*2d9fd380Sjfb8856606 int
stack_pop(struct stack * st,uint32_t * x)81*2d9fd380Sjfb8856606 stack_pop(struct stack *st, uint32_t *x)
82*2d9fd380Sjfb8856606 {
83*2d9fd380Sjfb8856606 	if (stack_is_empty(st))
84*2d9fd380Sjfb8856606 		return -ENOENT;
85*2d9fd380Sjfb8856606 
86*2d9fd380Sjfb8856606 	*x = st->items[st->top];
87*2d9fd380Sjfb8856606 	st->top--;
88*2d9fd380Sjfb8856606 
89*2d9fd380Sjfb8856606 	return 0;
90*2d9fd380Sjfb8856606 }
91*2d9fd380Sjfb8856606 
92*2d9fd380Sjfb8856606 /* Dump the stack
93*2d9fd380Sjfb8856606  */
stack_dump(struct stack * st)94*2d9fd380Sjfb8856606 void stack_dump(struct stack *st)
95*2d9fd380Sjfb8856606 {
96*2d9fd380Sjfb8856606 	int i, j;
97*2d9fd380Sjfb8856606 
98*2d9fd380Sjfb8856606 	printf("top=%d\n", st->top);
99*2d9fd380Sjfb8856606 	printf("max=%d\n", st->max);
100*2d9fd380Sjfb8856606 
101*2d9fd380Sjfb8856606 	if (st->top == -1) {
102*2d9fd380Sjfb8856606 		printf("stack is empty\n");
103*2d9fd380Sjfb8856606 		return;
104*2d9fd380Sjfb8856606 	}
105*2d9fd380Sjfb8856606 
106*2d9fd380Sjfb8856606 	for (i = 0; i < st->max + 7 / 8; i++) {
107*2d9fd380Sjfb8856606 		printf("item[%d] 0x%08x", i, st->items[i]);
108*2d9fd380Sjfb8856606 
109*2d9fd380Sjfb8856606 		for (j = 0; j < 7; j++) {
110*2d9fd380Sjfb8856606 			if (i++ < st->max - 1)
111*2d9fd380Sjfb8856606 				printf(" 0x%08x", st->items[i]);
112*2d9fd380Sjfb8856606 		}
113*2d9fd380Sjfb8856606 		printf("\n");
114*2d9fd380Sjfb8856606 	}
115*2d9fd380Sjfb8856606 }
116