Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Member-only story

CPU-Driven Circular Buffer

--

Did you know that you can delegate the wrap-around logic of a circular buffer to the hardware?

We can do this with virtual memory aliasing: by mapping multiple virtual addresses to the same physical memory location.

For context, we normally map virtual memory (memory in the application) 1 to 1 with physical memory (RAM).

Press enter or click to view image in full size
Normal memory layout is 1 to 1

Normal memory layout is 1 to 1

But we can force this map to something different. For example, we map several pages of our virtual memory to the same page in RAM.

Press enter or click to view image in full size
Virtual memory aliasing we map many to the same physical address

Virtual memory aliasing we map many to the same physical address

The hardware takes care of wrapping around the data in the physical memory and we get to use the buffer normally, no index arithmetic, no wrap-around logic.

In the case of a circular buffer, conceptually:

Press enter or click to view image in full size

I discovered this in Casey’s Computer Enhance course, and was compelled to test its performance, benchmarking it against a normal buffer.

--

--