summaryrefslogtreecommitdiff
path: root/Source/Kernel/VFS/BlockCache.class.cpp
blob: d505a99bd7b51cb3dfbb035dbe88114858ae4bfc (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <DeviceManager/Time.ns.h>

template <typename T>
BlockCache<T>::BlockCache(T* dev) {
	m_dev = dev;
	m_count = 0;
}

template <typename T>
BlockCache<T>::~BlockCache() {
	sync();
	m_count = 0;
	delete m_cache;
	delete m_cacheInfo;
}

template <typename T>
void BlockCache<T>::sync() {
	for (u32int i = 0; i < m_count; i++) {
		if (m_cacheInfo[i].dirty) m_dev->writeBlocks(m_cacheInfo[i].id, 1, m_cache + (i * m_dev->blockSize()));
	}	
}

template <typename T>
void BlockCache<T>::init(u32int count) {
	m_count = count;
	m_cacheInfo = new cached_block_t[count];
	for (u32int i = 0; i < m_count; i++) {
		m_cacheInfo[i].id = 0;
		m_cacheInfo[i].lastuse = 0;
		m_cacheInfo[i].dirty = false;
	}
	m_cache = (u8int*)Mem::alloc(m_count * m_dev->blockSize());
}

template <typename T>
bool BlockCache<T>::setCache(u64int block, u8int* data, bool dirty) {
	u32int best = 0;
	for (u32int i = 0; i < m_count; i++) {
		if (m_cacheInfo[i].id == block) {
			best = i;
			break;
		}
		if (m_cacheInfo[i].lastuse < m_cacheInfo[best].lastuse) best = i;
	}
	if (best >= m_count) return false;
	if (m_cacheInfo[best].dirty && (m_cacheInfo[best].id != block or !dirty))
	   m_dev->writeBlocks(m_cacheInfo[best].id, 1, m_cache + (best * m_dev->blockSize()));
	m_cacheInfo[best].id = block;
	m_cacheInfo[best].lastuse = Time::uptime();
	m_cacheInfo[best].dirty = dirty;
	memcpy(m_cache + (best * m_dev->blockSize()), data, m_dev->blockSize());
	return true;
}

template <typename T>
bool BlockCache<T>::getCache(u64int block, u8int* data) {
	for (u32int i = 0; i < m_count; i++) {
		if (m_cacheInfo[i].id == block && m_cacheInfo[i].lastuse != 0) {
			m_cacheInfo[i].lastuse = Time::uptime();
			memcpy(data, m_cache + (i * m_dev->blockSize()), m_dev->blockSize());
			return true;
		}
	}
	return false;
}

template <typename T>
bool BlockCache<T>::readBlocks(u64int startblock, u32int count, u8int* data) {
	if (count == 1) {
		if (getCache(startblock, data)) return true;
		if (!m_dev->readBlocks(startblock, count, data)) return false;
		setCache(startblock, data);
		return true;
	} else {
		return m_dev->readBlocks(startblock, count, data);
	}
}

template <typename T>
bool BlockCache<T>::writeBlocks(u64int startblock, u32int count, u8int* data) {
	if (m_dev->readOnly()) return false;
	if (count == 1) {
		if (!setCache(startblock, data, true)) return m_dev->writeBlocks(startblock, count, data);
		return true;
	} else {
		return m_dev->writeBlocks(startblock, count, data);
	}
}