My Kernel v0.1.0
bitmap.h
Go to the documentation of this file.
1
20#ifndef LIBALGO_BITMAP_H
21#define LIBALGO_BITMAP_H
22
23#include <kernel/types.h>
24
25#include <utils/bits.h>
26#include <utils/compiler.h>
27
28#include <stdbool.h>
29#include <stdint.h>
30
36typedef native_t bitmap_block_t;
37
42
43#define BITMAP_BLOCK_SIZE (8 * sizeof(bitmap_block_t))
44
45#define BITMAP_OFFSET(_index) ((_index) / BITMAP_BLOCK_SIZE)
46
48#define BITMAP(_name, _size) \
49 bitmap_block_t _name[BITMAP_OFFSET(NON_ZERO(_size) - 1) + 1]
50
58static ALWAYS_INLINE bool bitmap_read(const bitmap_t bitmap, uint32_t index)
59{
60 return BIT_READ(bitmap[BITMAP_OFFSET(index)], index % BITMAP_BLOCK_SIZE);
61}
62
68static ALWAYS_INLINE void bitmap_set(bitmap_t bitmap, uint32_t index)
69{
70 BIT_SET(bitmap[BITMAP_OFFSET(index)], index % BITMAP_BLOCK_SIZE);
71}
72
78static ALWAYS_INLINE void bitmap_clear(bitmap_t bitmap, uint32_t index)
79{
80 BIT_CLEAR(bitmap[BITMAP_OFFSET(index)], index % BITMAP_BLOCK_SIZE);
81}
82
88static ALWAYS_INLINE void
89bitmap_assign(bitmap_t bitmap, uint32_t index, bool value)
90{
91 if (value)
92 bitmap_set(bitmap, index);
93 else
94 bitmap_clear(bitmap, index);
95}
96
99#endif /* LIBALGO_BITMAP_H */
static ALWAYS_INLINE void bitmap_assign(bitmap_t bitmap, uint32_t index, bool value)
Set a value at a given index inside a bitmap to the given value.
Definition: bitmap.h:89
static ALWAYS_INLINE void bitmap_clear(bitmap_t bitmap, uint32_t index)
Set a value at a given index inside a bitmap as absent.
Definition: bitmap.h:78
static ALWAYS_INLINE void bitmap_set(bitmap_t bitmap, uint32_t index)
Set a value at a given index inside a bitmap as present.
Definition: bitmap.h:68
native_t bitmap_block_t
Basic unit used by the bitmap.
Definition: bitmap.h:36
bitmap_block_t * bitmap_t
A bitmap instance A bitmap is just a simple list of bytes (bitmap_block_t)
Definition: bitmap.h:41
static ALWAYS_INLINE bool bitmap_read(const bitmap_t bitmap, uint32_t index)
Read the value at a given index inside a bitmap.
Definition: bitmap.h:58
#define BIT_SET(_var, _n)
Set the nth bit.
Definition: bits.h:27
#define BIT_CLEAR(_var, _n)
Clear the nth bit.
Definition: bits.h:24
#define BIT_READ(_x, _n)
Read the nth bit.
Definition: bits.h:30