内容简介
#define _GNU_SOURCE%uA0#include <unistd.h>%uA0
#include <sys/mman.h>
void * mremap(void *old_address, size_t%uA0old_size%uA0, size_t%uA0new_size, int%uA0flags)
描述
mremap()%uA0扩大(或缩小)现有的内存映射,潜在的移动它在同一时间(由flags参数和可用的虚拟地址空间控制)。old_address%uA0is the old address of the virtual memory block that you want to expand (or shrink). Note that%uA0old_address%uA0has to be page aligned.%uA0old_size%uA0is the old size of the virtual memory block.%uA0new_size%uA0is the requested size of the virtual memory block after the resize.
In Linux the memory is divided into pages. A user process has (one or) several linear virtual memory segments. Each virtual memory segment has one or more mappings to real memory pages (in the page table). Each virtual memory segment has its own protection (access rights), which may cause a segmentation violation if the memory is accessed incorrectly (e.g., writing to a read-only segment). Accessing virtual memory outside of the segments will also cause a segmentation violation.
mremap() uses the Linux page table scheme.%uA0mremap() changes the mapping between virtual addresses and memory pages. This can be used to implement a very efficientrealloc().
The%uA0flags%uA0bit-mask argument may be 0, or include the following flag:
标签 | 描述 |
---|---|
MREMAP_MAYMOVE | |
%uA0 | By default, if there is not sufficient space to expand a mapping at its current location, then%uA0mremap() fails. If this flag is specified, then the kernel is permitted to relocate the mapping to a new virtual address, if necessary. If the mapping is relocated, then absolute yiibaiers into the old mapping location become invalid (offsets relative to the starting address of the mapping should be employed). |
MREMAP_FIXED%uA0(since Linux 2.3.31) | |
%uA0 | This flag serves a similar purpose to the%uA0MAP_FIXED%uA0flag ofmmap(2). If this flag is specified, then%uA0mremap() accepts a fifth argument,%uA0void *new_address, which specifies a page-aligned address to which the mapping must be moved. Any previous mapping at the address range specified by%uA0new_address%uA0andnew_size%uA0is unmapped. If%uA0MREMAP_FIXED%uA0is specified, thenMREMAP_MAYMOVE%uA0must also be specified. |
返回值
On success%uA0mremap() returns a yiibaier to the new virtual memory area. On error, the value%uA0MAP_FAILED%uA0(that is, (void *) -1) is returned, and%uA0errno%uA0is set appropriately.错误
标签 | 描述 |
---|---|
EAGAIN | The caller tried to expand a memory segment that is locked, but this was not possible without exceeding the RLIMIT_MEMLOCK resource limit. |
EFAULT | "Segmentation fault." Some address in the range%uA0old_address%uA0toold_address+old_size%uA0is an invalid virtual memory address for this process. You can also get EFAULT even if there exist mappings that cover the whole address space requested, but those mappings are of different types. |
EINVAL | An invalid argument was given. Possible causes are:%uA0old_addresswas not page aligned a value other than%uA0MREMAP_MAYMOVEor%uA0MREMAP_FIXED%uA0was specified in%uA0flags%uA0new_size%uA0was zeronew_size%uA0or%uA0new_address%uA0was invalid or the new address range specified by%uA0new_address%uA0and%uA0new_size%uA0overlapped the old address range specified by%uA0old_address%uA0and%uA0old_size orMREMAP_FIXED%uA0was specified without also specifyingMREMAP_MAYMOVE. |
ENOMEM | The memory area cannot be expanded at the current virtual address, and the%uA0MREMAP_MAYMOVE%uA0flag is not set in%uA0flags. Or, there is not enough (virtual) memory available. |