While programming with buffers like uint8_t* in C++ I often arrive at situations on where I'm forced to do copies. For example, if I receive packets in chuncks from the world and I want to parse something big that is formed of these packets, I'd have to create a bigger buffer and store them, in this buffer, sequentially as they arrive. Then I can parse this big buffer efficiently.

Since the kernel can have virtual memory through page tables, shouldn't it be possible for someone to create a syscall that gets a pointer to a list of all these small buffers (and their sizes), and create a big virtual buffer that acts as if I've copied all the mini buffers inside it?


No, that isn't possible in general, because the page tables work at a page granularity. If the length of every buffer is an exact multiple of the page table size, and every buffer starts at a page table boundary, you could do what you're proposing, but otherwise you can't. Since buffers usually won't satisfy those conditions, it's not very useful to create such a syscall.

There are techniques for structuring software to avoid making copies. However, this often requires that you restructure the code to support this. See https://en.wikipedia.org/wiki/Zero-copy.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.