-
Notifications
You must be signed in to change notification settings - Fork 1
/
airscan-devid.c
74 lines (63 loc) · 1.25 KB
/
airscan-devid.c
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
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Unique device IDs
*/
#include "airscan.h"
#include <string.h>
/* DEVID_RANGE defines device ID range, 0 ... DEVID_RANGE-1
* It must be power of two
*/
#define DEVID_RANGE 65536
static uint16_t devid_next;
static uint32_t devid_bits[DEVID_RANGE/32];
/* Get bit in devid_bits[]
*/
static bool
devid_bits_get(unsigned int id)
{
uint32_t mask = 1 << (id & 31);
return (devid_bits[id / 32] & mask) != 0;
}
/* Set bit in devid_bits[]
*/
static void
devid_bits_set(unsigned int id, bool v)
{
uint32_t mask = 1 << (id & 31);
if (v) {
devid_bits[id / 32] |= mask;
} else {
devid_bits[id / 32] &= ~mask;
}
}
/* Allocate unique device ID
*/
unsigned int
devid_alloc (void )
{
while (devid_bits_get(devid_next)) {
devid_next ++;
}
devid_bits_set(devid_next, true);
return devid_next ++;
}
/* Free device ID
*/
void
devid_free (unsigned int id)
{
devid_bits_set(id, false);
}
/* Initialize device ID allocator
*/
void
devid_init (void)
{
devid_next = 0;
memset(devid_bits, 0, sizeof(devid_bits));
}
/* vim:ts=8:sw=4:et
*/