I'm allocating memory using mmap
Linux syscall.
mov x5, 0 ; offset is zero
mov x4, -1 ; no file descriptor
mov x3, 0x22 ; MAP_PRIVATE MAP_ANONYMOUS
mov x2, 3 ; PROT_READ PROT_WRITE
mov x1, 4096 ; initial region size in bytes
mov x0, 0 ; let Linux choose region address
mov x8, 222 ; mmap
svc 0
Is it possible to increase the size of allocated memory region preserving its start address and contents? How to do it properly?
CodePudding user response:
If there's free virtual address space behind your original region, just create an additional mmap-ed region right behind the original one, using the MAP_FIXED
flag and identical permissions. If the page size of both regions are identical, they'll be coalesced into single mapping.
CodePudding user response:
On Linux, use the mremap(2)
Linux-specific system call without MREMAP_MAYMOVE
to extend the existing mapping, without considering the option of remapping those physical pages to a different virtual address where there's enough room.
It will return an error if some other mapping already exists for the pages you want to grow into. (Unlike mmap(MAP_FIXED)
which will silently replace those mappings.)
With just portable POSIX calls, mmap()
with a non-NULL hint address = right after you existing mapping, but without MAP_FIXED
; it will pick that address if the pages are free (and as @datenwolf says, merge with the earlier mapping into one long extent). Otherwise it will pick somewhere else. (Then you have to munmap
that mapping that ended up not where you wanted it.)
But if you're writing in asm, portability is irrelevant; other OSes will have different call numbers and maybe ABIs, so just look up __NR_mremap
in asm/unistd.h
, and get the flags
patterns from sys/mman.h
.