blob: 28de8a4c2ce8ec1bd4ab9bad40f1b8af8e14bfd7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03002/*
3 * Simple pointer stack
4 *
5 * (c) 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
6 */
7
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03008#include "pstack.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +02009#include "debug.h"
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030010#include <linux/kernel.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030011#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030012#include <stdlib.h>
13
14struct pstack {
15 unsigned short top;
16 unsigned short max_nr_entries;
17 void *entries[0];
18};
19
20struct pstack *pstack__new(unsigned short max_nr_entries)
21{
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030022 struct pstack *pstack = zalloc((sizeof(*pstack) +
23 max_nr_entries * sizeof(void *)));
24 if (pstack != NULL)
25 pstack->max_nr_entries = max_nr_entries;
26 return pstack;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030027}
28
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030029void pstack__delete(struct pstack *pstack)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030030{
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030031 free(pstack);
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030032}
33
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030034bool pstack__empty(const struct pstack *pstack)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030035{
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030036 return pstack->top == 0;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030037}
38
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030039void pstack__remove(struct pstack *pstack, void *key)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030040{
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030041 unsigned short i = pstack->top, last_index = pstack->top - 1;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030042
43 while (i-- != 0) {
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030044 if (pstack->entries[i] == key) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030045 if (i < last_index)
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030046 memmove(pstack->entries + i,
47 pstack->entries + i + 1,
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030048 (last_index - i) * sizeof(void *));
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030049 --pstack->top;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030050 return;
51 }
52 }
53 pr_err("%s: %p not on the pstack!\n", __func__, key);
54}
55
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030056void pstack__push(struct pstack *pstack, void *key)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030057{
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030058 if (pstack->top == pstack->max_nr_entries) {
59 pr_err("%s: top=%d, overflow!\n", __func__, pstack->top);
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030060 return;
61 }
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030062 pstack->entries[pstack->top++] = key;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030063}
64
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030065void *pstack__pop(struct pstack *pstack)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030066{
67 void *ret;
68
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030069 if (pstack->top == 0) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030070 pr_err("%s: underflow!\n", __func__);
71 return NULL;
72 }
73
Arnaldo Carvalho de Melo61e94512012-11-10 19:41:15 -030074 ret = pstack->entries[--pstack->top];
75 pstack->entries[pstack->top] = NULL;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030076 return ret;
77}
Namhyung Kimc8539e32015-04-24 10:15:32 +090078
79void *pstack__peek(struct pstack *pstack)
80{
81 if (pstack->top == 0)
82 return NULL;
83 return pstack->entries[pstack->top - 1];
84}