0

我正在寻找我在另一篇文章中发现的问题的解决方案。此代码已发布 Jerry Coffin。但是,我很难理解“show_page()”和“alloc_page”函数。

#include <windows.h>
#include <iostream>
#include <iomanip>

std::ostream &operator<<(std::ostream &os, MEMORY_BASIC_INFORMATION const &mi) {
    return os   << std::setw(20) << "Allocation Base: " << mi.AllocationBase << "\n"
                << std::setw(20) << "BaseAddress: " << mi.BaseAddress << "\n"
                << std::setw(20) << "Protection: " << mi.Protect << "\n"
                << std::setw(20) << "Region size: " << mi.RegionSize;
}

void show_page(void *page) {
    MEMORY_BASIC_INFORMATION info;

    VirtualQuery(page, &info, sizeof(info));
    std::cout << info << "\n\n";
}

static const int page_size = 4096;

void *alloc_page(char *address) {

    void *ret = VirtualAlloc(address, page_size, MEM_COMMIT, PAGE_READWRITE);
    show_page(ret);
    return ret;
}

int main() {
    static const int region_size = 65536;

    char * alloc = static_cast<char *>(VirtualAlloc(NULL, region_size, MEM_RESERVE, PAGE_READWRITE));

    for (int i = 0; i < 4; i++)
        alloc_page(alloc + page_size * i);
}

我是一名初学者 C++ 程序员,并试图理解 VirtualAlloc 来分配连续的内存页面。

4

0 回答 0