1 // SPDX-License-Identifier: GPL-2.0
5 * (c) 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
10 #include <linux/kernel.h>
11 #include <linux/zalloc.h>
17 unsigned short max_nr_entries
;
21 struct pstack
*pstack__new(unsigned short max_nr_entries
)
23 struct pstack
*pstack
= zalloc((sizeof(*pstack
) +
24 max_nr_entries
* sizeof(void *)));
26 pstack
->max_nr_entries
= max_nr_entries
;
30 void pstack__delete(struct pstack
*pstack
)
35 bool pstack__empty(const struct pstack
*pstack
)
37 return pstack
->top
== 0;
40 void pstack__remove(struct pstack
*pstack
, void *key
)
42 unsigned short i
= pstack
->top
, last_index
= pstack
->top
- 1;
45 if (pstack
->entries
[i
] == key
) {
47 memmove(pstack
->entries
+ i
,
48 pstack
->entries
+ i
+ 1,
49 (last_index
- i
) * sizeof(void *));
54 pr_err("%s: %p not on the pstack!\n", __func__
, key
);
57 void pstack__push(struct pstack
*pstack
, void *key
)
59 if (pstack
->top
== pstack
->max_nr_entries
) {
60 pr_err("%s: top=%d, overflow!\n", __func__
, pstack
->top
);
63 pstack
->entries
[pstack
->top
++] = key
;
66 void *pstack__pop(struct pstack
*pstack
)
70 if (pstack
->top
== 0) {
71 pr_err("%s: underflow!\n", __func__
);
75 ret
= pstack
->entries
[--pstack
->top
];
76 pstack
->entries
[pstack
->top
] = NULL
;
80 void *pstack__peek(struct pstack
*pstack
)
84 return pstack
->entries
[pstack
->top
- 1];