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:22:45 UTC 2023


Maybe it would be better to wrap the slice in a new class with an 
invariant?

Because what I want to do is:

1. Ensure that the length of the underlying referenced/pointed-to 
data is `PAGE_SIZE`
2. Benefit from the features of D that I can

The bounds-checking of slices saves a lot of headaches during 
development.

So maybe doing something like this might be better?

```d
struct Frame
{
	ubyte[] data;

	invariant
	{
		assert(data.length == PAGE_SIZE);
	}
}

class BufferPool
{
	align(PAGE_SIZE) ubyte[PAGE_SIZE * BUF_POOL_NUM_PAGES] data;
	Frame[BUF_POOL_NUM_PAGES] frames;

	this()
	{
		// mmap
		foreach (i; 0 .. BUF_POOL_NUM_PAGES)
		{
			frame_idx_t frame_idx = cast(frame_idx_t) i;
			frames[frame_idx].data = data[frame_idx * PAGE_SIZE .. 
(frame_idx + 1) * PAGE_SIZE];
		}
	}
}
```







More information about the Digitalmars-d-learn mailing list