The Pedigree Project 0.1
DiskView.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef MACHINE_DISKVIEW_H
3#define MACHINE_DISKVIEW_H
4
5#include "pedigree/kernel/compiler.h"
6#include "pedigree/kernel/machine/DiskPaging.h"
7#include "pedigree/kernel/utilities/BufferView.h"
8
9class Disk;
10class DiskWriteView;
11
15class EXPORTED_PUBLIC DiskReadView {
16 public:
18 DiskReadView(DiskReadView&& other) noexcept;
19 DiskReadView& operator=(DiskReadView&& other) noexcept;
21 DiskReadView(const DiskReadView&) = delete;
22 DiskReadView& operator=(const DiskReadView&) = delete;
23
25 static DiskReadView borrowed(const void* data, size_t size);
26
27 explicit operator bool() const {
28 return m_Data != nullptr;
29 }
30 const void* data() const {
31 return m_Data;
32 }
33 size_t size() const {
34 return m_Size;
35 }
36 bool truncate(size_t length) {
37 if (length > m_Size)
38 return false;
39 m_Size = length;
40 return true;
41 }
42 bool copyTo(void* destination, size_t length, size_t offset = 0) const {
43 if ((!destination && length) || offset > m_Size || length > m_Size - offset)
44 return false;
45 if (length)
46 MemoryCopy(destination, m_Data + offset, length);
47 return true;
48 }
49 template <typename T>
50 bool readAt(T& into, size_t offset = 0) const {
51 static_assert(__is_trivially_copyable(T), "Disk values must be trivially copyable");
52 if (offset > m_Size || sizeof(T) > m_Size - offset)
53 return false;
54 // Packed on-disk values need not have the host type's alignment.
55 __builtin_memcpy(&into, m_Data + offset, sizeof(T));
56 return true;
57 }
58 void reset();
59
60 private:
61 friend class Disk;
62 friend class DiskWriteView;
63 DiskReadView(Disk* owner, uint64_t location, BufferView view, bool writable, DiskUse&& use);
64
65 Disk* m_Owner;
66 uint64_t m_Location;
67 const uint8_t* m_Data;
68 size_t m_Size;
69 bool m_Writable;
70 DiskUse m_Use;
71};
72
74class EXPORTED_PUBLIC DiskWriteView {
75 public:
76 DiskWriteView() = default;
77 DiskWriteView(DiskWriteView&&) noexcept = default;
78 DiskWriteView& operator=(DiskWriteView&&) noexcept = default;
79 DiskWriteView(const DiskWriteView&) = delete;
80 DiskWriteView& operator=(const DiskWriteView&) = delete;
81
82 explicit operator bool() const {
83 return static_cast<bool>(m_View);
84 }
85 void* data() const {
86 return const_cast<void*>(m_View.data());
87 }
88 size_t size() const {
89 return m_View.size();
90 }
91 bool truncate(size_t length) {
92 return m_View.truncate(length);
93 }
94 template <typename T>
95 bool readAt(T& into, size_t offset = 0) const {
96 return m_View.readAt(into, offset);
97 }
98 template <typename T>
99 bool writeAt(const T& value, size_t offset = 0) {
100 static_assert(__is_trivially_copyable(T), "Disk values must be trivially copyable");
101 if (offset > size() || sizeof(T) > size() - offset)
102 return false;
103 __builtin_memcpy(static_cast<uint8_t*>(data()) + offset, &value, sizeof(T));
104 return true;
105 }
106 bool copyFrom(const void* source, size_t length, size_t offset = 0) {
107 if ((!source && length) || offset > size() || length > size() - offset)
108 return false;
109 if (length)
110 MemoryCopy(static_cast<uint8_t*>(data()) + offset, source, length);
111 return true;
112 }
113 void reset() {
114 m_View.reset();
115 }
116
117 private:
118 friend class Disk;
119 DiskWriteView(Disk* owner, uint64_t location, BufferView view, DiskUse&& use)
120 : m_View(owner, location, view, true, static_cast<DiskUse&&>(use)) {}
121 DiskReadView m_View;
122};
123
124#endif
Definition Disk.h:35