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 15:07:25 UTC 2023


On Friday, 13 January 2023 at 14:57:40 UTC, Ali Çehreli wrote:
> On 1/13/23 06:49, Gavin Ray wrote:
>
> > I am curious if you can return something like
> `ubyte[PAGE_SIZE]*` or
> > `ref ubyte[PAGE_SIZE]`?
>
> A simple cast seems to work:
>
> enum PAGE_SIZE = 4096;
> enum BUF_POOL_NUM_PAGES = 1024;
> alias frame_idx_t = size_t;
>
> ubyte[10_000] data;
>
> ubyte[PAGE_SIZE]* get_page(frame_idx_t frame_idx)
> {
>     auto ptr = data.ptr + frame_idx * PAGE_SIZE;
>     return cast(ubyte[PAGE_SIZE]*)ptr;
> }
>
> void main() {
> }
>
> Ali

Ah, much thanks Ali!

This is "valid" D I hope?

I searched Github for `"ubyte[4096]*"` and I found two uses of 
it, in the `xtrix` repo which seems to be an Operating System 
written in D:

![screenshot-of-ubyte-sized-ptr-on-github](https://i.imgur.com/WaAklbq.png)



More information about the Digitalmars-d-learn mailing list