Debian Codegraph @

src/mpt/osinfo/dos_memory.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_OSINFO_DOS_MEMORY_HPP
4 #define MPT_OSINFO_DOS_MEMORY_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/namespace.hpp"
10
11 #if MPT_OS_DJGPP
12 #include "mpt/string/buffer.hpp"
13 #endif // MPT_OS_DJGPP
14
15 #if MPT_OS_DJGPP
16 #include <array>
17 #include <limits>
18 #include <optional>
19 #include <string_view>
20 #endif // MPT_OS_DJGPP
21
22 #if MPT_OS_DJGPP
23 #include <cstddef>
24 #endif // MPT_OS_DJGPP
25
26 #if MPT_OS_DJGPP
27 #include <dpmi.h>
28 #endif // MPT_OS_DJGPP
29
30
31
32 namespace mpt {
33 inline namespace MPT_INLINE_NS {
34
35
36
37 namespace osinfo {
38
39 namespace dos {
40
41
42
43 #if MPT_OS_DJGPP
44
45 struct memory_info {
46
47 std::optional<std::size_t> total_virtual;
48 std::optional<std::size_t> free_virtual;
49 std::optional<std::size_t> free_contiguous_virtual;
50
51 std::optional<std::size_t> total_physical;
52 std::optional<std::size_t> free_physical;
53 std::optional<std::size_t> free_contiguous_physical;
54
55 std::optional<std::size_t> unlocked_physical;
56
57 std::optional<std::size_t> total_swap;
58
59 inline std::optional<std::size_t> get_virtual_total() const noexcept {
60 if (!total_virtual) {
61 if (total_physical && total_swap) {
62 return *total_physical + *total_swap;
63 } else if (total_physical) {
64 return total_physical;
65 } else {
66 return std::nullopt;
67 }
68 }
69 return total_virtual;
70 }
71 inline std::optional<std::size_t> get_virtual_used() const noexcept {
72 if (!get_virtual_total() || !get_virtual_free()) {
73 return std::nullopt;
74 }
75 if (*get_virtual_total() < *get_virtual_free()) {
76 return 0;
77 }
78 return *get_virtual_total() - *get_virtual_free();
79 }
80 inline std::optional<std::size_t> get_virtual_free() const noexcept {
81 if (free_virtual) {
82 return free_virtual;
83 } else if (free_contiguous_virtual) {
84 return free_contiguous_virtual;
85 } else {
86 return std::nullopt;
87 }
88 }
89 inline std::optional<std::size_t> get_virtual_external_fragmentation() const noexcept {
90 if (!get_virtual_free() || !free_contiguous_virtual) {
91 return std::nullopt;
92 }
93 if (*get_virtual_free() < *free_contiguous_virtual) {
94 return 0;
95 }
96 return *get_virtual_free() - *free_contiguous_virtual;
97 }
98 inline std::optional<std::size_t> get_virtual_free_contiguous() const noexcept {
99 return free_contiguous_virtual;
100 }
101
102 inline std::optional<std::size_t> get_physical_total() const noexcept {
103 return total_physical;
104 }
105 inline std::optional<std::size_t> get_physical_used() const noexcept {
106 if (!total_physical || !get_physical_free()) {
107 return std::nullopt;
108 }
109 if (*total_physical < *get_physical_free()) {
110 return 0;
111 }
112 return *total_physical - *get_physical_free();
113 }
114 inline std::optional<std::size_t> get_physical_free() const noexcept {
115 if (free_physical) {
116 return free_physical;
117 } else if (free_contiguous_physical) {
118 return free_contiguous_physical;
119 } else {
120 return std::nullopt;
121 }
122 }
123 inline std::optional<std::size_t> get_physical_external_fragmentation() const noexcept {
124 if (!get_physical_free() || !free_contiguous_physical) {
125 return std::nullopt;
126 }
127 if (*get_physical_free() < *free_contiguous_physical) {
128 return 0;
129 }
130 return *get_physical_free() - *free_contiguous_physical;
131 }
132 inline std::optional<std::size_t> get_physical_free_contiguous() const noexcept {
133 return free_contiguous_physical;
134 }
135
136 inline std::optional<std::size_t> get_physical_used_locked() const noexcept {
137 if (!total_physical || !unlocked_physical) {
138 return std::nullopt;
139 }
140 if (*total_physical < *unlocked_physical) {
141 return 0;
142 }
143 return *total_physical - *unlocked_physical;
144 }
145 inline std::optional<std::size_t> get_physical_freeable() const noexcept {
146 if (!unlocked_physical) {
147 return get_physical_free();
148 }
149 return unlocked_physical;
150 }
151
152 inline std::optional<std::size_t> get_swap_total() const noexcept {
153 return total_swap;
154 }
155 };
156
157 inline memory_info get_memory_info() {
158 memory_info result;
159 __dpmi_free_mem_info dpmi_free_mem_info{};
160 if (__dpmi_get_free_memory_information(&dpmi_free_mem_info) == 0) {
161 unsigned long page_size = 0;
162 if (__dpmi_get_page_size(&page_size) != 0) {
163 page_size = 0;
164 }
165 if (dpmi_free_mem_info.largest_available_free_block_in_bytes != 0xffffffffu) {
166 result.free_contiguous_virtual = dpmi_free_mem_info.largest_available_free_block_in_bytes;
167 } else if ((dpmi_free_mem_info.maximum_unlocked_page_allocation_in_pages != 0xffffffffu) && (page_size > 0)) {
168 result.free_contiguous_virtual = dpmi_free_mem_info.maximum_unlocked_page_allocation_in_pages * page_size;
169 }
170 if (page_size > 0) {
171 if (dpmi_free_mem_info.maximum_locked_page_allocation_in_pages != 0xffffffffu) {
172 result.free_contiguous_physical = dpmi_free_mem_info.maximum_locked_page_allocation_in_pages * page_size;
173 }
174 if (dpmi_free_mem_info.linear_address_space_size_in_pages != 0xffffffffu) {
175 result.total_virtual = dpmi_free_mem_info.linear_address_space_size_in_pages * page_size;
176 }
177 if (dpmi_free_mem_info.total_number_of_unlocked_pages != 0xffffffffu) {
178 result.unlocked_physical = dpmi_free_mem_info.total_number_of_unlocked_pages * page_size;
179 int dpmi_flags = 0;
180 std::array<char, 128> dpmi_vendor = {};
181 if (__dpmi_get_capabilities(&dpmi_flags, dpmi_vendor.data()) == 0) {
182 char buf[126] = {};
183 std::memcpy(buf, dpmi_vendor.data() + 2, 126);
184 // CWSDPMI does not track locked memory, thus all physical is reported unlocked
185 if (std::string_view(mpt::ReadAutoBuf(buf)) == std::string_view("CWSDPMI")) {
186 result.unlocked_physical = std::nullopt;
187 }
188 }
189 }
190 if (dpmi_free_mem_info.total_number_of_free_pages != 0xffffffffu) {
191 result.free_physical = dpmi_free_mem_info.total_number_of_free_pages * page_size;
192 }
193 if (dpmi_free_mem_info.total_number_of_physical_pages != 0xffffffffu) {
194 result.total_physical = dpmi_free_mem_info.total_number_of_physical_pages * page_size;
195 }
196 if (dpmi_free_mem_info.free_linear_address_space_in_pages != 0xffffffffu) {
197 result.free_virtual = dpmi_free_mem_info.free_linear_address_space_in_pages * page_size;
198 }
199 if (dpmi_free_mem_info.size_of_paging_file_partition_in_pages != 0xffffffffu) {
200 result.total_swap = dpmi_free_mem_info.size_of_paging_file_partition_in_pages * page_size;
201 }
202 }
203 }
204 return result;
205 }
206
207 #endif // MPT_OS_DJGPP
208
209
210
211 } // namespace dos
212
213 } // namespace osinfo
214
215
216
217 } // namespace MPT_INLINE_NS
218 } // namespace mpt
219
220
221
222 #endif // MPT_OSINFO_DOS_MEMORY_HPP