Xyris  0.5
MemorySection.hpp
Go to the documentation of this file.
1 /**
2  * @file MemoryRange.hpp
3  * @author Keeton Feavel ([email protected])
4  * @brief Class used to categorize and range various sections of memory
5  * @version 0.1
6  * @date 2021-09-12
7  *
8  * @copyright Copyright the Xyris Contributors (c) 2021
9  *
10  */
11 #pragma once
12 #include <Arch/Memory.hpp>
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 namespace Memory {
17 
18 enum Type {
22  NVS,
23  Bad,
27 };
28 
29 class Section {
30 public:
32  : m_base(0)
33  , m_end(0)
34  , m_type(Unknown)
35  {
36  }
37 
38  Section(const uintptr_t base, const uintptr_t end)
39  : m_base(base)
40  , m_end(end)
41  , m_type(Unknown)
42  {
43  }
44 
45  Section(const uintptr_t base, const uintptr_t end, const Type type)
46  : m_base(base)
47  , m_end(end)
48  , m_type(type)
49  {
50  }
51 
52  [[gnu::always_inline]] bool initialized()
53  {
54  return m_base && size();
55  }
56 
57  [[gnu::always_inline]] uintptr_t base()
58  {
59  return m_base;
60  }
61 
62  [[gnu::always_inline]] uintptr_t end()
63  {
64  return m_end;
65  }
66 
67  [[gnu::always_inline]] uintptr_t size()
68  {
69  return m_end - m_base;
70  }
71 
72  [[gnu::always_inline]] uintptr_t pages()
73  {
74  return size() / ARCH_PAGE_SIZE;
75  }
76 
77  [[gnu::always_inline]] enum Type type()
78  {
79  return m_type;
80  }
81 
82  const char* typeString()
83  {
84  switch (type()) {
85  case Available:
86  return "Available";
87  case Reserved:
88  return "Reserved";
89  case ACPI:
90  return "ACPI";
91  case NVS:
92  return "NVS";
93  case Bad:
94  return "Bad";
95  case Bootloader:
96  return "Bootloader";
97  case Kernel:
98  return "Kernel";
99  case Unknown:
100  return "Unknown";
101  default:
102  return "Invalid!";
103  }
104  }
105 
106  [[gnu::always_inline]] void setType(enum Type type)
107  {
108  m_type = type;
109  }
110 
111  [[gnu::always_inline]] bool empty()
112  {
113  return size() == 0;
114  }
115 
116  [[gnu::always_inline]] bool contains(uintptr_t addr)
117  {
118  return end() >= addr && addr <= base();
119  }
120 
121 private:
122  uintptr_t m_base;
123  uintptr_t m_end;
124  enum Type m_type;
125 };
126 
127 } // !namespace Memory
Memory::Type
Type
Definition: MemorySection.hpp:18
Memory
Definition: MemoryMap.hpp:15
Memory::ACPI
@ ACPI
Definition: MemorySection.hpp:21
Memory::Section::contains
bool contains(uintptr_t addr)
Definition: MemorySection.hpp:116
Memory::Section::m_base
uintptr_t m_base
Definition: MemorySection.hpp:122
Memory::Section::setType
void setType(enum Type type)
Definition: MemorySection.hpp:106
Memory::Section::pages
uintptr_t pages()
Definition: MemorySection.hpp:72
Memory::Section::type
enum Type type()
Definition: MemorySection.hpp:77
Memory::Section::size
uintptr_t size()
Definition: MemorySection.hpp:67
Memory::NVS
@ NVS
Definition: MemorySection.hpp:22
Memory::Bad
@ Bad
Definition: MemorySection.hpp:23
Memory::Section::initialized
bool initialized()
Definition: MemorySection.hpp:52
Memory::Reserved
@ Reserved
Definition: MemorySection.hpp:20
Memory::Section::Section
Section()
Definition: MemorySection.hpp:31
Memory::Section
Definition: MemorySection.hpp:29
Memory::Section::typeString
const char * typeString()
Definition: MemorySection.hpp:82
Memory::Section::empty
bool empty()
Definition: MemorySection.hpp:111
Memory::Section::m_end
uintptr_t m_end
Definition: MemorySection.hpp:123
Memory::Section::Section
Section(const uintptr_t base, const uintptr_t end)
Definition: MemorySection.hpp:38
Memory.hpp
Architecture memory management & paging API.
Memory::Section::m_type
enum Type m_type
Definition: MemorySection.hpp:124
Memory::Section::base
uintptr_t base()
Definition: MemorySection.hpp:57
Memory::Section::end
uintptr_t end()
Definition: MemorySection.hpp:62
ARCH_PAGE_SIZE
#define ARCH_PAGE_SIZE
Definition: Memory.i686.hpp:21
Memory::Kernel
@ Kernel
Definition: MemorySection.hpp:25
Memory::Available
@ Available
Definition: MemorySection.hpp:19
Memory::Unknown
@ Unknown
Definition: MemorySection.hpp:26
Memory::Bootloader
@ Bootloader
Definition: MemorySection.hpp:24
Memory::Section::Section
Section(const uintptr_t base, const uintptr_t end, const Type type)
Definition: MemorySection.hpp:45