~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/intlist.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Based on intlist.c by:
  4  * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
  5  */
  6 
  7 #include <errno.h>
  8 #include <stdlib.h>
  9 #include <linux/compiler.h>
 10 
 11 #include "intlist.h"
 12 
 13 static struct rb_node *intlist__node_new(struct rblist *rblist __maybe_unused,
 14                                          const void *entry)
 15 {
 16         unsigned long i = (unsigned long)entry;
 17         struct rb_node *rc = NULL;
 18         struct int_node *node = malloc(sizeof(*node));
 19 
 20         if (node != NULL) {
 21                 node->i = i;
 22                 node->priv = NULL;
 23                 rc = &node->rb_node;
 24         }
 25 
 26         return rc;
 27 }
 28 
 29 static void int_node__delete(struct int_node *ilist)
 30 {
 31         free(ilist);
 32 }
 33 
 34 static void intlist__node_delete(struct rblist *rblist __maybe_unused,
 35                                  struct rb_node *rb_node)
 36 {
 37         struct int_node *node = container_of(rb_node, struct int_node, rb_node);
 38 
 39         int_node__delete(node);
 40 }
 41 
 42 static int intlist__node_cmp(struct rb_node *rb_node, const void *entry)
 43 {
 44         unsigned long i = (unsigned long)entry;
 45         struct int_node *node = container_of(rb_node, struct int_node, rb_node);
 46 
 47         if (node->i > i)
 48                 return 1;
 49         else if (node->i < i)
 50                 return -1;
 51 
 52         return 0;
 53 }
 54 
 55 int intlist__add(struct intlist *ilist, unsigned long i)
 56 {
 57         return rblist__add_node(&ilist->rblist, (void *)i);
 58 }
 59 
 60 void intlist__remove(struct intlist *ilist, struct int_node *node)
 61 {
 62         rblist__remove_node(&ilist->rblist, &node->rb_node);
 63 }
 64 
 65 static struct int_node *__intlist__findnew(struct intlist *ilist,
 66                                            unsigned long i, bool create)
 67 {
 68         struct int_node *node = NULL;
 69         struct rb_node *rb_node;
 70 
 71         if (ilist == NULL)
 72                 return NULL;
 73 
 74         if (create)
 75                 rb_node = rblist__findnew(&ilist->rblist, (void *)i);
 76         else
 77                 rb_node = rblist__find(&ilist->rblist, (void *)i);
 78 
 79         if (rb_node)
 80                 node = container_of(rb_node, struct int_node, rb_node);
 81 
 82         return node;
 83 }
 84 
 85 struct int_node *intlist__find(struct intlist *ilist, unsigned long i)
 86 {
 87         return __intlist__findnew(ilist, i, false);
 88 }
 89 
 90 struct int_node *intlist__findnew(struct intlist *ilist, unsigned long i)
 91 {
 92         return __intlist__findnew(ilist, i, true);
 93 }
 94 
 95 static int intlist__parse_list(struct intlist *ilist, const char *s)
 96 {
 97         char *sep;
 98         int err;
 99 
100         do {
101                 unsigned long value = strtol(s, &sep, 10);
102                 err = -EINVAL;
103                 if (*sep != ',' && *sep != '\0')
104                         break;
105                 err = intlist__add(ilist, value);
106                 if (err)
107                         break;
108                 s = sep + 1;
109         } while (*sep != '\0');
110 
111         return err;
112 }
113 
114 struct intlist *intlist__new(const char *slist)
115 {
116         struct intlist *ilist = malloc(sizeof(*ilist));
117 
118         if (ilist != NULL) {
119                 rblist__init(&ilist->rblist);
120                 ilist->rblist.node_cmp    = intlist__node_cmp;
121                 ilist->rblist.node_new    = intlist__node_new;
122                 ilist->rblist.node_delete = intlist__node_delete;
123 
124                 if (slist && intlist__parse_list(ilist, slist))
125                         goto out_delete;
126         }
127 
128         return ilist;
129 out_delete:
130         intlist__delete(ilist);
131         return NULL;
132 }
133 
134 void intlist__delete(struct intlist *ilist)
135 {
136         if (ilist != NULL)
137                 rblist__delete(&ilist->rblist);
138 }
139 
140 struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx)
141 {
142         struct int_node *node = NULL;
143         struct rb_node *rb_node;
144 
145         rb_node = rblist__entry(&ilist->rblist, idx);
146         if (rb_node)
147                 node = container_of(rb_node, struct int_node, rb_node);
148 
149         return node;
150 }
151 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php