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:49:57 UTC 2023
Suppose that you have a memory manager, or arena-like class,
which contains a buffer used to store memory.
And you want to hand out chunks of this memory to other parts of
your program. These chunks should all be `PAGE_SIZE`.
You might have something like:
```d
enum PAGE_SIZE = 4096;
enum BUF_POOL_NUM_PAGES = 1024;
class BufferPool
{
align(PAGE_SIZE) ubyte[PAGE_SIZE * BUF_POOL_NUM_PAGES] data;
this()
{
mmap(&data, data.sizeof,
PROT_READ | PROT_WRITE, MAP_ANONYMOUS |
MAP_PRIVATE, -1, 0);
}
}
```
Now there should be a function, like `get_page()`, that returns a
`PAGE_SIZE` pointer of `ubyte`. But I can only figure out how to
return a `ubyte[]`:
```d
ubyte[] get_page(frame_idx_t frame_idx)
{
return data[frame_idx * PAGE_SIZE .. (frame_idx + 1) *
PAGE_SIZE];
}
```
I am curious if you can return something like `ubyte[PAGE_SIZE]*`
or `ref ubyte[PAGE_SIZE]`?
Thank you =)
More information about the Digitalmars-d-learn
mailing list