summaryrefslogtreecommitdiff
path: root/Source/Kernel/Library/Bitset.class.cpp
blob: ec4e62c6477044c331ba67d51792b36f92949da8 (plain) (blame)
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
#include "Bitset.class.h"

Bitset::Bitset() {
}

Bitset::Bitset(u32int size) {
	init(size, (u32int*)Mem::kalloc(INDEX_FROM_BIT(size)));
}

Bitset::Bitset(u32int size, u32int *ptr) {
	init(size, ptr);
}

Bitset::~Bitset() {
	Mem::kfree(m_data);
}

void Bitset::init(u32int size, u32int *ptr) {
	m_size = size;
	m_data = ptr;
	for (u32int i = 0; i < INDEX_FROM_BIT(m_size); i++) {
		m_data[i] = 0;
	}
	m_usedCount = 0;
}

void Bitset::setBit(u32int number) {
	u32int idx = INDEX_FROM_BIT(number);
	u32int off = OFFSET_FROM_BIT(number);
	m_data[idx] |= (0x1 << off);
	m_usedCount++;
}

void Bitset::clearBit(u32int number) {
	u32int idx = INDEX_FROM_BIT(number);
	u32int off = OFFSET_FROM_BIT(number);
	m_data[idx] &= ~(0x1 << off);
	m_usedCount--;
}

bool Bitset::testBit(u32int number) {
	u32int idx = INDEX_FROM_BIT(number);
	u32int off = OFFSET_FROM_BIT(number);
	return (m_data[idx] & (0x1 << off));
}

u32int Bitset::firstFreeBit() {
	for (u32int i = 0; i < INDEX_FROM_BIT(m_size); i++) {
		if (m_data[i] != 0xFFFFFFFF) {
			for (int j = 0; j < 32; j++) {
				if (!(m_data[i] & (0x1 << j))) {
					return (i * 4 * 8) + j;
				}
			}
		}
	}
	return (u32int) - 1;
}

u32int Bitset::usedBits() {
	return m_usedCount;
}