summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/src/Swap/DiskSwap.cc
blob: c50a96a956a3fb73cf31e068dbeb611a194b6c11 (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
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
/* -------------------------------------------

	Copyright (C) 2024-2025 Amlal EL Mahrouss Labs, all rights reserved.

------------------------------------------- */

#include <SwapKit/DiskSwap.h>
#include <KernelKit/FileMgr.h>

namespace NeOS
{
	/***********************************************************************************/
	/// @brief Write memory chunk onto disk.
	/// @param fork_name The swap name to recognize this memory region.
	/// @param fork_name_len length of fork name.
	/// @param data the data packet.
	/// @return Whether the swap was written to disk, or not.
	/***********************************************************************************/
	BOOL SwapDiskInterface::Write(const Char* fork_name, const SizeT fork_name_len, SWAP_DISK_HEADER* data)
	{
		if (!fork_name || !fork_name_len)
			return NO;

		if (*fork_name == 0)
			return NO;

		if (!data)
			return NO;

		FileStream file(kSwapPageFile, "wb");

		auto ret = file.Write(fork_name, data, sizeof(SWAP_DISK_HEADER) + data->fBlobSz);

		if (ret.Error())
			return NO;

		return YES;
	}

	/***********************************************************************************/
	/// @brief Read memory chunk from disk.
	/// @param fork_name The swap name to recognize this memory region.
	/// @param fork_name_len length of fork name.
	/// @param data the data packet length.
	/// @return Whether the swap was fetched to disk, or not.
	/***********************************************************************************/
	SWAP_DISK_HEADER* SwapDiskInterface::Read(const Char* fork_name, const SizeT fork_name_len, const SizeT data_len)
	{
		if (!fork_name || !fork_name_len)
			return nullptr;

		if (*fork_name == 0)
			return nullptr;

		if (data_len > kSwapBlockMaxSize)
			return nullptr;

		if (data_len == 0)
			return nullptr;

		FileStream file(kSwapPageFile, "rb");

		VoidPtr blob = file.Read(fork_name, sizeof(SWAP_DISK_HEADER) + data_len);

		return reinterpret_cast<SWAP_DISK_HEADER*>(blob);
	}
} // namespace NeOS