AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
bitmap.h
Go to the documentation of this file.
1#ifndef _DS_BITMAP_H
2#define _DS_BITMAP_H
3
4#include <core/system.h>
5#include <core/string.h>
6
7/*
8 * Bitmaps: one bit per thing (a page, a block), set or clear. The bits
9 * are stored in 32-bit blocks; bit i is bit (i % 32) of block (i / 32),
10 * whatever the CPU's byte order.
11 */
12
13/* A block of a bitmap's bits. */
15
17struct bitmap {
19 size_t max_idx; /* the index of its last bit */
20};
21
22/* The number of bits in a block, and the mask of a bit's place in it. */
23#define BITMAP_BLOCK_SIZE (32)
24#define BITMAP_BLOCK_MASK (BITMAP_BLOCK_SIZE - 1)
25/* log2 of the block size */
26#define BITMAP_LOG2_BLOCK_SIZE (5)
27/* The block that bit i is in, and its place in that block. */
28#define BITMAP_BLOCK_OFFSET(__bitmap__i__) ((__bitmap__i__) >> BITMAP_LOG2_BLOCK_SIZE)
29#define BITMAP_BIT_OFFSET(__bitmap__i__) ((__bitmap__i__) & (BITMAP_BLOCK_SIZE - 1))
30
31/* The size in bytes of a bitmap of n bits, and a new static one, clear. */
32#define BITMAP_SIZE(n) ((n) + BITMAP_BLOCK_MASK) / BITMAP_BLOCK_SIZE * MEMBER_SIZE(struct bitmap, map[0])
33#define BITMAP_NEW(n) (&(struct bitmap){.map = (bitmap_t[BITMAP_SIZE(n)]){0}, .max_idx = (n) - 1})
34
36static inline size_t bitmap_size(size_t n)
37{
38 return (n + BITMAP_BLOCK_MASK) / BITMAP_BLOCK_SIZE * MEMBER_SIZE(struct bitmap, map[0]);
39}
40
42static inline void bitmap_set(struct bitmap *bitmap, size_t index)
43{
44 bitmap->map[BITMAP_BLOCK_OFFSET(index)] |= 1U << BITMAP_BIT_OFFSET(index);
45}
46
48static inline void bitmap_clear(struct bitmap *bitmap, size_t index)
49{
50 bitmap->map[BITMAP_BLOCK_OFFSET(index)] &= ~(1U << BITMAP_BIT_OFFSET(index));
51}
52
54static inline int bitmap_check(struct bitmap *bitmap, size_t index)
55{
56 return bitmap->map[BITMAP_BLOCK_OFFSET(index)] & (1U << BITMAP_BIT_OFFSET(index));
57}
58
60static inline void bitmap_set_range(struct bitmap *bitmap, size_t findex, size_t lindex)
61{
62 if (findex > lindex)
63 return;
64
65 size_t i = findex;
66
67 /* the bits before the first whole block, one by one */
68 while (i & BITMAP_BLOCK_MASK && i <= lindex) {
70 ++i;
71 }
72
73 if (i > lindex)
74 return;
75
76 /* the whole blocks, all at once */
77 if (i < lindex)
78 memset(&bitmap->map[BITMAP_BLOCK_OFFSET(i)], -1, (lindex - i)/BITMAP_BLOCK_SIZE * MEMBER_SIZE(struct bitmap, map[0]));
79
80 i += (lindex - i)/BITMAP_BLOCK_SIZE * BITMAP_BLOCK_SIZE;
81
82 /* the bits after the last whole block, one by one */
83 while (i <= lindex) {
84 bitmap_set(bitmap, i); ++i;
85 }
86}
87
89static inline void bitmap_clear_range(struct bitmap *bitmap, size_t findex, size_t lindex)
90{
91 if (findex > lindex)
92 return;
93
94 size_t i = findex;
95
96 /* the bits before the first whole block, one by one */
97 while (i & BITMAP_BLOCK_MASK && i <= lindex) {
99 ++i;
100 }
101
102 if (i > lindex)
103 return;
104
105 /* the whole blocks, all at once */
106 if (i < lindex)
107 memset(&bitmap->map[BITMAP_BLOCK_OFFSET(i)], 0, (lindex - i)/BITMAP_BLOCK_SIZE * MEMBER_SIZE(struct bitmap, map[0]));
108
109 i += (lindex - i)/BITMAP_BLOCK_SIZE * BITMAP_BLOCK_SIZE;
110
111 /* the bits after the last whole block, one by one */
112 while (i <= lindex) {
114 ++i;
115 }
116}
117
118#endif /* ! _DS_BITMAP_H */
static void bitmap_clear(struct bitmap *bitmap, size_t index)
Clears a bit.
Definition bitmap.h:48
static int bitmap_check(struct bitmap *bitmap, size_t index)
Returns whether a bit is set (non-zero if it is).
Definition bitmap.h:54
#define BITMAP_BLOCK_OFFSET(__bitmap__i__)
Definition bitmap.h:28
static void bitmap_set_range(struct bitmap *bitmap, size_t findex, size_t lindex)
Sets the bits from findex to lindex, inclusive.
Definition bitmap.h:60
#define BITMAP_BLOCK_SIZE
Definition bitmap.h:23
uint32_t bitmap_t
Definition bitmap.h:14
#define BITMAP_BIT_OFFSET(__bitmap__i__)
Definition bitmap.h:29
static void bitmap_set(struct bitmap *bitmap, size_t index)
Sets a bit.
Definition bitmap.h:42
#define BITMAP_BLOCK_MASK
Definition bitmap.h:24
static size_t bitmap_size(size_t n)
Returns the size in bytes of a bitmap of n bits.
Definition bitmap.h:36
static void bitmap_clear_range(struct bitmap *bitmap, size_t findex, size_t lindex)
Clears the bits from findex to lindex, inclusive.
Definition bitmap.h:89
#define MEMBER_SIZE(type, member)
Definition system.h:15
void * memset(void *dst, int c, size_t n)
Sets n bytes to c.
Definition libc.c:23
unsigned int uint32_t
Definition stddef.h:22
A bitmap: its blocks, and its last bit's index.
Definition bitmap.h:17
size_t max_idx
Definition bitmap.h:19
bitmap_t * map
Definition bitmap.h:18