1
2
3
4
5
6#include <linux/vmalloc.h>
7#include <linux/bitmap.h>
8#include <linux/bitops.h>
9#include "idset.h"
10#include "css.h"
11
12struct idset {
13 int num_ssid;
14 int num_id;
15 unsigned long bitmap[0];
16};
17
18static inline unsigned long bitmap_size(int num_ssid, int num_id)
19{
20 return BITS_TO_LONGS(num_ssid * num_id) * sizeof(unsigned long);
21}
22
23static struct idset *idset_new(int num_ssid, int num_id)
24{
25 struct idset *set;
26
27 set = vmalloc(sizeof(struct idset) + bitmap_size(num_ssid, num_id));
28 if (set) {
29 set->num_ssid = num_ssid;
30 set->num_id = num_id;
31 memset(set->bitmap, 0, bitmap_size(num_ssid, num_id));
32 }
33 return set;
34}
35
36void idset_free(struct idset *set)
37{
38 vfree(set);
39}
40
41void idset_fill(struct idset *set)
42{
43 memset(set->bitmap, 0xff, bitmap_size(set->num_ssid, set->num_id));
44}
45
46static inline void idset_add(struct idset *set, int ssid, int id)
47{
48 set_bit(ssid * set->num_id + id, set->bitmap);
49}
50
51static inline void idset_del(struct idset *set, int ssid, int id)
52{
53 clear_bit(ssid * set->num_id + id, set->bitmap);
54}
55
56static inline int idset_contains(struct idset *set, int ssid, int id)
57{
58 return test_bit(ssid * set->num_id + id, set->bitmap);
59}
60
61static inline int idset_get_first(struct idset *set, int *ssid, int *id)
62{
63 int bitnum;
64
65 bitnum = find_first_bit(set->bitmap, set->num_ssid * set->num_id);
66 if (bitnum >= set->num_ssid * set->num_id)
67 return 0;
68 *ssid = bitnum / set->num_id;
69 *id = bitnum % set->num_id;
70 return 1;
71}
72
73struct idset *idset_sch_new(void)
74{
75 return idset_new(max_ssid + 1, __MAX_SUBCHANNEL + 1);
76}
77
78void idset_sch_add(struct idset *set, struct subchannel_id schid)
79{
80 idset_add(set, schid.ssid, schid.sch_no);
81}
82
83void idset_sch_del(struct idset *set, struct subchannel_id schid)
84{
85 idset_del(set, schid.ssid, schid.sch_no);
86}
87
88
89void idset_sch_del_subseq(struct idset *set, struct subchannel_id schid)
90{
91 int pos = schid.ssid * set->num_id + schid.sch_no;
92
93 bitmap_clear(set->bitmap, pos, set->num_id - schid.sch_no);
94}
95
96int idset_sch_contains(struct idset *set, struct subchannel_id schid)
97{
98 return idset_contains(set, schid.ssid, schid.sch_no);
99}
100
101int idset_is_empty(struct idset *set)
102{
103 return bitmap_empty(set->bitmap, set->num_ssid * set->num_id);
104}
105
106void idset_add_set(struct idset *to, struct idset *from)
107{
108 int len = min(to->num_ssid * to->num_id, from->num_ssid * from->num_id);
109
110 bitmap_or(to->bitmap, to->bitmap, from->bitmap, len);
111}
112