From 57f0f512b273f60d52568b8c6b77e17f5636edc0 Mon Sep 17 00:00:00 2001 From: André Fabian Silva Delgado Date: Wed, 5 Aug 2015 17:04:01 -0300 Subject: Initial import --- tools/perf/util/pstack.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tools/perf/util/pstack.c (limited to 'tools/perf/util/pstack.c') diff --git a/tools/perf/util/pstack.c b/tools/perf/util/pstack.c new file mode 100644 index 000000000..a126e6cc6 --- /dev/null +++ b/tools/perf/util/pstack.c @@ -0,0 +1,76 @@ +/* + * Simple pointer stack + * + * (c) 2010 Arnaldo Carvalho de Melo + */ + +#include "util.h" +#include "pstack.h" +#include "debug.h" +#include +#include + +struct pstack { + unsigned short top; + unsigned short max_nr_entries; + void *entries[0]; +}; + +struct pstack *pstack__new(unsigned short max_nr_entries) +{ + struct pstack *pstack = zalloc((sizeof(*pstack) + + max_nr_entries * sizeof(void *))); + if (pstack != NULL) + pstack->max_nr_entries = max_nr_entries; + return pstack; +} + +void pstack__delete(struct pstack *pstack) +{ + free(pstack); +} + +bool pstack__empty(const struct pstack *pstack) +{ + return pstack->top == 0; +} + +void pstack__remove(struct pstack *pstack, void *key) +{ + unsigned short i = pstack->top, last_index = pstack->top - 1; + + while (i-- != 0) { + if (pstack->entries[i] == key) { + if (i < last_index) + memmove(pstack->entries + i, + pstack->entries + i + 1, + (last_index - i) * sizeof(void *)); + --pstack->top; + return; + } + } + pr_err("%s: %p not on the pstack!\n", __func__, key); +} + +void pstack__push(struct pstack *pstack, void *key) +{ + if (pstack->top == pstack->max_nr_entries) { + pr_err("%s: top=%d, overflow!\n", __func__, pstack->top); + return; + } + pstack->entries[pstack->top++] = key; +} + +void *pstack__pop(struct pstack *pstack) +{ + void *ret; + + if (pstack->top == 0) { + pr_err("%s: underflow!\n", __func__); + return NULL; + } + + ret = pstack->entries[--pstack->top]; + pstack->entries[pstack->top] = NULL; + return ret; +} -- cgit v1.2.3-54-g00ecf