pata_legacy: fix CONFIG_PATA_WINBOND_VLB_MODULE test
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / pstack.c
blob13d36faf64eb4da3e2bed21813f0d791550f5053
1 /*
2 * Simple pointer stack
4 * (c) 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
5 */
7 #include "util.h"
8 #include "pstack.h"
9 #include <linux/kernel.h>
10 #include <stdlib.h>
12 struct pstack {
13 unsigned short top;
14 unsigned short max_nr_entries;
15 void *entries[0];
18 struct pstack *pstack__new(unsigned short max_nr_entries)
20 struct pstack *self = zalloc((sizeof(*self) +
21 max_nr_entries * sizeof(void *)));
22 if (self != NULL)
23 self->max_nr_entries = max_nr_entries;
24 return self;
27 void pstack__delete(struct pstack *self)
29 free(self);
32 bool pstack__empty(const struct pstack *self)
34 return self->top == 0;
37 void pstack__remove(struct pstack *self, void *key)
39 unsigned short i = self->top, last_index = self->top - 1;
41 while (i-- != 0) {
42 if (self->entries[i] == key) {
43 if (i < last_index)
44 memmove(self->entries + i,
45 self->entries + i + 1,
46 (last_index - i) * sizeof(void *));
47 --self->top;
48 return;
51 pr_err("%s: %p not on the pstack!\n", __func__, key);
54 void pstack__push(struct pstack *self, void *key)
56 if (self->top == self->max_nr_entries) {
57 pr_err("%s: top=%d, overflow!\n", __func__, self->top);
58 return;
60 self->entries[self->top++] = key;
63 void *pstack__pop(struct pstack *self)
65 void *ret;
67 if (self->top == 0) {
68 pr_err("%s: underflow!\n", __func__);
69 return NULL;
72 ret = self->entries[--self->top];
73 self->entries[self->top] = NULL;
74 return ret;