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

  Copyright (C) 2024-2025 Amlal El Mahrouss , licensed under the Apache 2.0 license.

======================================== */

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

namespace Kernel {
static constexpr UInt32 kSwapDiskHeaderMagic = 0x44535750;  // 'DSWP'

/***********************************************************************************/
/// @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 DiskSwapInterface::Write(const Char* fork_name, SizeT fork_name_len, SWAP_DISK_HEADER* data) {
  if (!fork_name || !fork_name_len) return NO;

  if (*fork_name == 0) return NO;

  if (!data || data->fMagic != kSwapDiskHeaderMagic) return NO;

  FileStream file(kSwapPageFilePath, kRestrictWRB);

  ErrorOr<Int64> 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* DiskSwapInterface::Read(const Char* fork_name, SizeT fork_name_len,
                                          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(kSwapPageFilePath, kRestrictRB);

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

  if (!blob || ((SWAP_DISK_HEADER*) blob)->fMagic != kSwapDiskHeaderMagic) {
    if (blob) mm_free_ptr(blob);
    return nullptr;
  }

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