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