summaryrefslogtreecommitdiff
path: root/kernel/src/utils/bitmap.hpp
blob: 0ebbb15bcbcb21d31fa4e9ad9a5f703e0ff53f15 (plain)
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
#pragma once
#include <klibc/stdio.hpp>
#include <klibc/stdlib.hpp>
#include <klibc/string.hpp>
#include <utils/align.hpp>

namespace utils {
    class bitmap {
      private:
        std::uint8_t* pool;
        std::uint32_t pool_size;

      public:
        bitmap(std::uint32_t size, std::uint8_t* custom_pool = 0) {
            if (custom_pool) {
                this->pool = custom_pool;
                this->pool_size = (ALIGNUP(size, 8) / 8);
            } else {
                this->pool_size = (ALIGNUP(size, 8) / 8) + 1;
                this->pool = (std::uint8_t*) klibc::malloc(pool_size);
            }
            klibc::memset(pool, 0, pool_size);
        }

        std::uint8_t test(std::uint32_t idx) {
            return pool[ALIGNDOWN(idx, 8) / 8] & (1 << (idx - ALIGNDOWN(idx, 8)));
        }

        void clear(std::uint32_t idx) {
            pool[ALIGNDOWN(idx, 8) / 8] &= ~(1 << (idx - ALIGNDOWN(idx, 8)));
        }

        void set(std::uint32_t idx) {
            pool[ALIGNDOWN(idx, 8) / 8] |= (1 << (idx - ALIGNDOWN(idx, 8)));
        }
    };
};