I heard about using a mmap system call for merging two blocks of memory into one continuous with MAP_ANONYMOUS flag, but I can’t find any simple example how to use this trick.
The example on the Wikipedia which implements an optimized circular buffer, is too complicated for me (Circular Buffer article).
Can you give me any usage example for MAP_ANONYMOUS flag?
Based on the clarification you have provided in your comments, it sounds like you are trying to mis two things that have nothing to do with each other.
You want to manipulate virtual memory so that these two blocks of memory are addressable as 350 contiguous bytes of memory.
This is not possible. First of all, the blocks of memory you have will in general be neither page-aligned nor page-sized. You can only manipulate virtual memory in page-aligned, page-sized chunks. Secondly, even if you are very lucky and they are page-aligned and page-sized, they probably come from the heap area (the area below
brk()). I don’t think you can remap or unmap that area of memory usingmremap()ormunmap(). (There are alternate implementations ofmalloc()that get memory frommmap()and wouldn’t be subject to this problem but they are still subject to the first problem.But let’s say you do have two blocks of memory that are page-aligned, page-sized, and remapable, and you want to remap them so that they are adjacent. Most likely, you obtained those blocks from
mmap()in the first place. Then you could remap them to adjacent addresses usingmremap(). Be aware thatmremap()is Linux-specific though. I’m not aware of a portable way to do this. In pseudocode: