Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)
Gavin Ray
ray.gavin97 at gmail.com
Fri Jan 13 14:59:10 UTC 2023
I probably should have mentioned, the equivalent in C++ is the
below:
```cpp
#include <cstddef>
#include <span>
#include <sys/mman.h>
static constexpr size_t PAGE_SIZE = 4096;
static constexpr size_t BUF_POOL_NUM_PAGES = 1024;
class BufferPool
{
private:
alignas(PAGE_SIZE) std::byte data[BUF_POOL_NUM_PAGES *
PAGE_SIZE];
public:
BufferPool()
{
mmap(data, BUF_POOL_NUM_PAGES * PAGE_SIZE,
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
}
// Return a fat-pointer of PAGE_SIZE bytes over the page,
containing { T* ptr, len }
std::span<std::byte, PAGE_SIZE> get_page(size_t page_num)
{
return std::span<std::byte, PAGE_SIZE>(data + page_num *
PAGE_SIZE, PAGE_SIZE);
}
};
```
More information about the Digitalmars-d-learn
mailing list