Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

Ali Çehreli acehreli at yahoo.com
Fri Jan 13 14:57:40 UTC 2023


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



More information about the Digitalmars-d-learn mailing list