Memory Allocation
sys_brk
Change data segment size.New program break address. If 0, returns current break without changing it.
Returns the new program break address on success. Returns current break on failure.
- Requested address would overlap existing mappings
- System is out of memory
Memory Mapping
Syscalls for mapping files and anonymous memory into the process address space.sys_mmap / sys_mmap2
Map files or devices into memory.Preferred starting address for the mapping. If 0, kernel chooses address. Must be page-aligned if MMAP_FIXED is set.
Length of mapping in bytes. Rounded up to page size.
Memory protection flags:
PROT_READ(0x1): Pages may be readPROT_WRITE(0x2): Pages may be writtenPROT_EXEC(0x4): Pages may be executedPROT_NONE(0x0): Pages may not be accessed
Mapping flags:
MMAP_SHARED(0x1): Share mapping with other processesMMAP_PRIVATE(0x2): Create copy-on-write private mappingMMAP_FIXED(0x10): Map at exact address, replacing existing mappingsMMAP_ANONYMOUS(0x20): Mapping not backed by file (fd ignored)
File descriptor to map. Ignored if MMAP_ANONYMOUS is set.
Offset in file to start mapping, in pages for mmap2, in bytes for mmap64.
Returns starting address of mapping on success, negative errno on error.
EINVAL(-22): Invalid parameters (len=0, unaligned addr with FIXED, invalid prot/flags)EBADF(-9): Invalid file descriptorENOMEM(-12): Out of memory or address spaceENODEV(-19): File descriptor doesn’t support mmap
sys_munmap
Unmap files or devices from memory.Starting address of region to unmap. Must be page-aligned.
Length of region to unmap in bytes. Rounded up to page size.
Returns 0 on success, negative errno on error.
EINVAL(-22): addr not page-aligned or len is 0
sys_mprotect
Change memory protection.Starting address of region. Must be page-aligned.
Length of region in bytes. Rounded up to page size.
New protection flags (same as mmap: PROT_READ | PROT_WRITE | PROT_EXEC).
Returns 0 on success, negative errno on error.
EINVAL(-22): Invalid parametersENOMEM(-12): Address range not mappedEACCES(-13): Protection conflict with file mapping
sys_mremap
Expand or shrink an existing memory mapping.Starting address of existing mapping. Must be page-aligned.
Current size of mapping in bytes.
Desired new size of mapping in bytes.
Remapping flags:
MREMAP_MAYMOVE(0x1): Allow kernel to relocate mapping if necessaryMREMAP_FIXED(0x2): Place mapping at specific address
Returns address of resized mapping on success, negative errno on error.
EINVAL(-22): Invalid parameters (unaligned addr, zero length, invalid flags)EFAULT(-14): Invalid existing mappingENOMEM(-12): Cannot expand mapping in place and MAYMOVE not set
sys_madvise
Give advice about memory usage patterns.Starting address of region.
Length of region in bytes.
Advice about memory usage:
MADV_NORMAL: No special treatmentMADV_RANDOM: Expect random page referencesMADV_SEQUENTIAL: Expect sequential page referencesMADV_WILLNEED: Expect access in near futureMADV_DONTNEED: Don’t expect access in near future
Returns 0 on success, negative errno on error.
sys_msync
Synchronize a mapped region with filesystem.Starting address of region to sync. Should be page-aligned.
Length of region in bytes.
Sync flags:
MS_ASYNC: Schedule sync but return immediatelyMS_SYNC: Perform sync and wait for completionMS_INVALIDATE: Invalidate cached data
Returns 0 on success, negative errno on error.
sys_mlock
Lock pages in memory.Starting address of region to lock.
Length of region in bytes.
Returns 0 on success, negative errno on error.
NUMA Memory Management
sys_mbind
Set NUMA memory policy for a memory range.Starting address of memory region.
Length of region in bytes.
Memory policy mode (MPOL_DEFAULT, MPOL_BIND, MPOL_INTERLEAVE, MPOL_PREFERRED).
Bitmask of NUMA nodes.
Maximum node number + 1.
Policy flags.
Returns 0 on success, negative errno on error.