1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/*
* shl - Flagset
*
* Copyright (c) 2013 David Herrmann <dh.herrmann@googlemail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* A dynamic flagset implementation
*/
#ifndef SHL_FLAGSET_H
#define SHL_FLAGSET_H
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "shl_array.h"
#include "shl_misc.h"
static inline int shl_flagset_new(struct shl_array **out)
{
return shl_array_new(out, sizeof(unsigned long), 1);
}
static inline void shl_flagset_free(struct shl_array *arr)
{
return shl_array_free(arr);
}
static inline int shl_flagset_alloc(struct shl_array *arr, unsigned int *out)
{
static const unsigned long one = 1;
unsigned int i, j;
unsigned long *data;
int ret;
if (!arr)
return -EINVAL;
for (i = 0; i < arr->length; ++i) {
data = SHL_ARRAY_AT(arr, unsigned long, i);
for (j = 0; j < SHL_ULONG_BITS; ++j) {
if (!(*data & (1UL << j))) {
*data |= 1UL << j;
*out = i * SHL_ULONG_BITS + j;
return 0;
}
}
}
ret = shl_array_push(arr, &one);
if (ret)
return ret;
*out = (arr->length - 1) * SHL_ULONG_BITS;
return 0;
}
static inline int shl_flagset_reserve(struct shl_array *arr, unsigned int num)
{
int ret;
unsigned int idx, off;
unsigned long *data;
if (!arr)
return -EINVAL;
idx = num / SHL_ULONG_BITS;
off = num % SHL_ULONG_BITS;
if (idx >= arr->length) {
ret = shl_array_zresize(arr, idx + 1);
if (ret)
return ret;
}
data = SHL_ARRAY_AT(arr, unsigned long, idx);
if (*data & (1UL << off))
return -EEXIST;
*data |= 1UL << off;
return 0;
}
static inline int shl_flagset_set(struct shl_array *arr, unsigned int num)
{
int ret;
ret = shl_flagset_reserve(arr, num);
if (ret == -EEXIST)
return 0;
return ret;
}
static inline void shl_flagset_unset(struct shl_array *arr, unsigned int num)
{
unsigned int idx, off;
unsigned long *data;
if (!arr)
return;
idx = num / SHL_ULONG_BITS;
off = num % SHL_ULONG_BITS;
if (idx >= arr->length)
return;
data = SHL_ARRAY_AT(arr, unsigned long, idx);
*data &= ~(1UL << off);
}
#endif /* SHL_FLAGSET_H */
|