Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Simple pointer stack |
| 3 | * |
| 4 | * (c) 2010 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 5 | */ |
| 6 | |
| 7 | #include "util.h" |
| 8 | #include "pstack.h" |
| 9 | #include <linux/kernel.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | struct pstack { |
| 13 | unsigned short top; |
| 14 | unsigned short max_nr_entries; |
| 15 | void *entries[0]; |
| 16 | }; |
| 17 | |
| 18 | struct pstack *pstack__new(unsigned short max_nr_entries) |
| 19 | { |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 20 | struct pstack *pstack = zalloc((sizeof(*pstack) + |
| 21 | max_nr_entries * sizeof(void *))); |
| 22 | if (pstack != NULL) |
| 23 | pstack->max_nr_entries = max_nr_entries; |
| 24 | return pstack; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 25 | } |
| 26 | |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 27 | void pstack__delete(struct pstack *pstack) |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 28 | { |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 29 | free(pstack); |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 30 | } |
| 31 | |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 32 | bool pstack__empty(const struct pstack *pstack) |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 33 | { |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 34 | return pstack->top == 0; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 35 | } |
| 36 | |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 37 | void pstack__remove(struct pstack *pstack, void *key) |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 38 | { |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 39 | unsigned short i = pstack->top, last_index = pstack->top - 1; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 40 | |
| 41 | while (i-- != 0) { |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 42 | if (pstack->entries[i] == key) { |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 43 | if (i < last_index) |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 44 | memmove(pstack->entries + i, |
| 45 | pstack->entries + i + 1, |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 46 | (last_index - i) * sizeof(void *)); |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 47 | --pstack->top; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | } |
| 51 | pr_err("%s: %p not on the pstack!\n", __func__, key); |
| 52 | } |
| 53 | |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 54 | void pstack__push(struct pstack *pstack, void *key) |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 55 | { |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 56 | if (pstack->top == pstack->max_nr_entries) { |
| 57 | pr_err("%s: top=%d, overflow!\n", __func__, pstack->top); |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 58 | return; |
| 59 | } |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 60 | pstack->entries[pstack->top++] = key; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 61 | } |
| 62 | |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 63 | void *pstack__pop(struct pstack *pstack) |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 64 | { |
| 65 | void *ret; |
| 66 | |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 67 | if (pstack->top == 0) { |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 68 | pr_err("%s: underflow!\n", __func__); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
Arnaldo Carvalho de Melo | 61e9451 | 2012-11-10 19:41:15 -0300 | [diff] [blame] | 72 | ret = pstack->entries[--pstack->top]; |
| 73 | pstack->entries[pstack->top] = NULL; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 74 | return ret; |
| 75 | } |