* <!-- description --> * @brief This function allocates read/write virtual memory from the * kernel. This memory is not physically contiguous. The resulting * pointer is at least 4k aligned, so use this function sparingly * as it will always allocate at least one page. Use platform_free() * to release this memory. * * @note This function must zero the allocated memory *
| 126 | * Returns a nullptr on failure. |
| 127 | */ |
| 128 | NODISCARD void * |
| 129 | platform_alloc(uint64_t const size) NOEXCEPT |
| 130 | { |
| 131 | uint64_t mut_size = size; |
| 132 | if (!bf_is_page_aligned(size)) { |
| 133 | mut_size = bf_page_aligned(size + HYPERVISOR_PAGE_SIZE); |
| 134 | } |
| 135 | |
| 136 | if (g_mut_platform_alloc > 0) { |
| 137 | --g_mut_platform_alloc; |
| 138 | |
| 139 | if (0 == g_mut_platform_alloc) { |
| 140 | return NULLPTR; |
| 141 | } |
| 142 | |
| 143 | bf_touch(); |
| 144 | } |
| 145 | else { |
| 146 | bf_touch(); |
| 147 | } |
| 148 | |
| 149 | #ifdef _WIN32 |
| 150 | return memset(_aligned_malloc(mut_size, HYPERVISOR_PAGE_SIZE), 0, mut_size); // NOLINT |
| 151 | #else |
| 152 | return memset(aligned_alloc(HYPERVISOR_PAGE_SIZE, mut_size), 0, mut_size); // NOLINT |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * <!-- description --> |
no test coverage detected