# MEM ## **Overview** Provides structures and functions for performing memory operations, including memory mapping, remapping, allocation, and release. **Since:** 1.0 **Version:** 1.0 ## **Summary** ## Files
Describes limitations and attributes of system-level shared memory. |
|
Describes system resource information about the shared memory. |
Indicates the return value of functions such as mmap() when the operation fails. |
|
Indicates the mapping attribute that the updates to the mapping are visible to other processes mapping the same file and are carried through to the underlying file. This macro is used as an input parameter passed to functions such as mmap(). |
|
Indicates the mapping attribute that the updates to the mapping are not visible to other mapping processes and are not carried through to the underlying file. This macro is used as an input parameter passed to functions such as mmap(). |
|
Indicates the mapping attribute that specifies the mapping as fixed mapping. This macro is used as an input parameter passed to functions such as mmap(). |
|
Indicates the mapping attribute that specifies the mapping as anonymous mapping without a specified file or device. This macro is used as an input parameter passed to functions such as mmap(). |
|
MAP_ANONYMOUS MAP_ANON |
Indicates the mapping attribute that specifies the mapping as anonymous mapping without a specified file or device. This macro is the synonym for MAP_ANON and is used as an input parameter passed to functions such as mmap(). |
Indicates that no permission is granted to the current process for accessing the mapping area. This macro is used as an input parameter passed to functions such as mmap(). |
|
Indicates that the current process is granted the read permission on the mapping area. This macro is used as an input parameter passed to functions such as mmap(). |
|
Indicates that the current process is granted the write permission on the mapping area. This macro is used as an input parameter passed to functions such as mmap(). |
|
Indicates that the current process is granted the execute permission on the mapping area. This macro is used as an input parameter passed to functions such as mmap(). |
|
Indicates the remapping attribute that allows the mapping to be relocated to a new address. This macro is used as an input parameter passed to functions such as mremap(). |
|
Indicates the remapping attribute that specifies the mapping as fixed mapping. This macro is used as an input parameter passed to functions such as mremap(). |
|
Indicates that the shared memory segment is readable. This macro is used for setting the shmflg parameter passed to functions such as shmget(). |
|
Indicates that the shared memory segment is writable. This macro is used for setting the shmflg parameter passed to functions such as shmget(). |
|
Indicates that the attached shared memory segment is read-only. This macro is used for setting the shmflg parameter passed to functions such as shmat(). |
|
Indicates that the shared memory address can be rounded to a value meeting the requirement (SHMLBA). This macro is used for setting the shmflg parameter passed to functions such as shmat(). |
|
Indicates that the memory segment can be remapped. This macro is used for setting the shmflg parameter passed to functions such as shmat(). |
|
Indicates that the attached shared memory segment is executable. This macro is used for setting the shmflg parameter passed to functions such as shmat(). |
|
Locks the shared memory segment in memory so that it cannot be swapped to the swap partition. This macro is used for setting the shmflg parameter passed to functions such as shmget(). |
|
Unlocks the shared memory segment. This macro is used for setting the shmflg parameter passed to functions such as shmget(). |
|
SHM_STAT (13 | (IPC_STAT & 0x100)) |
Obtains a shmid_ds data structure. This macro is used for setting the cmd parameter passed to shmctl(). |
Obtains a shm_info data structure that includes system resource information about this shared memory segment. This macro is used for setting the cmd parameter passed to shmctl(). |
|
SHM_STAT_ANY (15 | (IPC_STAT & 0x100)) |
Obtains a shmid_ds data structure without permission check. This macro is used for setting the cmd parameter passed to shmctl(). |
malloc (size_t size) |
Dynamically allocates a block of uninitialized memory with the specified size. |
calloc (size_t nmemb, size_t size) |
Dynamically allocates multiple blocks of memory with the specified size. |
realloc (void *ptr, size_t size) |
Changes the size of a previously allocated memory block pointed to by ptr to the specified size. |
free (void *ptr) |
|
memalign (size_t alignment, size_t size) |
Allocates a block of memory with the specified size based on the given alignment mode. |
malloc_usable_size (void *ptr) |
|
mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset) |
Creates a mapping between the virtual address space of the calling process and a file or device. |
munmap (void *addr, size_t length) |
Removes all mappings for the specified virtual address space. |
mprotect (void *addr, size_t len, int prot) |
Sets protection attributes for the memory pages contained in the memory region starting from addr with the specified length. |
mremap (void *old_address, size_t old_size, size_t new_size, int flags,...) |
|
shmat (int shmid, const void *shmaddr, int shmflg) |
Attaches the shared memory segment identified by shmid to the address space of the current process. |
Performs a control operation specified by the cmd parameter on the shared memory segment identified by shmid. |
|
shmdt (const void *shmaddr) |
Detaches the shared memory segment attached to the address pointed to by shmaddr from the address space of the calling process. |
shmget (key_t key, size_t size, int shmflg) |
Obtains or creates a shared memory segment with the specified size based on the ID specified by key. |
nmemb | Indicates the number of memory blocks to allocate. |
size | Indicates the size of the memory block to allocate, in bytes. |
ptr | Indicates the pointer to a memory block previously allocated with malloc, calloc or realloc. |
size | Indicates the size of the memory block to allocate, in bytes. |
ptr | Indicates the pointer to a block of memory previously allocated by functions such as malloc(). |
alignment | Indicates the alignment size of the allocated memory. |
size | Indicates the size of the memory block to allocate, in bytes. |
The file descriptor specified by fd refers to a non-regular file. The file descriptor specified by fd is invalid. MAP_SHARED and PROT_WRITE are specified, but the file identified by fd is not opened in O_RDWR mode. PROT_WRITE is specified, but the file is append-only. |
|
The file descriptor specified by fd is invalid, and MAP_ANONYMOUS is not specified in flags. |
|
The length and offset are too large, addr is not page-aligned, or the length is 0. Neither MAP_SHARED nor MAP_ANONYMOUS is specified in flags, or both are specified. |
|
PROT_EXEC is specified in prot, but the mapped area belongs to a file on a file system that was mounted non-executable, or the file seal does not allow this operation. |
The memory region cannot be granted the specified permission. This error can occur, for example, when you use mmap to map a file with prot set to PROT_READ and then use this function to set prot to PROT_WRITE. |
|
addr is an invalid pointer or it points to an address that is not a multiple of the page size. |
|
Internal kernel structures cannot be allocated due to insufficient memory, or addresses in the specified range are invalid for the address space of the process. The total number of mappings with different attributes exceeds the maximum number allowed by the system if the protection attribute of the memory region is changed. |
old_address | Indicates the old address of the virtual memory region to expand or shrink. |
old_size | Indicates the old size of the virtual memory region. |
new_size | Indicates the requested size of the virtual memory region after the expansion or shrinking. |
flags | Specifies whether the existing memory can be mapped to a new or specified address. |
new_address | Indicates the new address of the virtual memory region. This parameter is optional and is used when MREMAP_FIXED is specified in flags. |
By default, if no enough space is available to expand a mapping at its current location, the operation fails. If this flag is specified, the kernel is allowed to relocate the mapping to a new virtual address when necessary. |
|
If this flag is specified, the new_address parameter is enabled in this function and the memory is mapped to a new address. This flag must be used together with MREMAP_MAYMOVE. |
Some memory addresses in the range from old_address to old_address+old_size are invalid for this process. |
|
old_address is not a multiple of the page size, or a value other than MREMAP_MAYMOVE and MREMAP_FIXED is specified in flags. new_size is 0, new_size or new_address is invalid, or MREMAP_FIXED is specified in flags without also specifying MREMAP_MAYMOVE. old_size is 0 but old_address does not point to a shared memory mapping; old_size is 0 but MREMAP_MAYMOVE is not specified in flags; or the new memory address range specified by new_address and new_size overlaps the old memory address range specified by old_address and old_size. |
|
addr | Indicates the pointer to the start address of the memory region to unmap, which is the return value of mmap(). |
length | Indicates the length of the address range to unmap, which should be the length specified in mmap(). |
ptr | Indicates the pointer to a memory block previously allocated with malloc, calloc or realloc. |
size | Indicates the new size for the memory block, in bytes. |
shmid | Identifies the shared memory segment to attach, which is obtained by shmget(). |
shmaddr | Indicates the space address of the current process to attach. If this parameter is NULL, the system chooses an unused address to attach. |
shmflg | Indicates the attribute of the shared memory segment to attach, which is the bitwise OR combination of one or more of the following flags: |
Round the attached address. If shmaddr is not NULL and SHM_RND is specified in shmflg, the address is rounded down to the nearest multiple of SHMLBA. |
|
Allow the contents of the attached shared memory segment to be executable. |
|
Set the contents of the attached shared memory segment to be read-only. |
|
Replace any existing mapping in the address range starting at shmaddr and continuing for the size of the segment during the current memory mapping. When SHM_REMAP is not specified, an EINVAL error will be returned if a mapping exists in the address range. |
The current process does not have the required permission on the requested mapping type. |
|
The shared memory segment specified by shmid has been removed. |
|
The value of shmid is invalid. shmaddr is not aligned (not page-aligned and SHM_RND is not specified) or its value is invalid. SHM_REMAP is specified, but shmaddr is NULL. |
|
shmid | Identifies the shared memory segment to attach, which is obtained by shmget(). |
cmd | Indicates the control operation to perform. The following table describes available values. |
buf | Indicates the pointer to the buffer for storing the information to set or obtain. |
Copy information from the kernel data structure associated with the shared memory segment identified by shmid into the shmid_ds structure pointed to by buf. |
|
Return a shmid_ds data structure. When SHM_STAT is specified, shmid does not identify a shared memory segment. Instead, it indicates the index to the kernel's internal array that maintains information about all shared memory segments. |
|
Write the values of some fields in the shmid_ds data structure pointed to by buf to the kernel data structure associated with the specified shared memory segment, updating also its shm_ctimemember. The following fields can be modified: shm_perm.uid, shm_perm.gid, and shm_perm.mode. |
|
Return information about system-wide shared memory limits and parameters in the structure pointed to by buf. The returned structure is of the shminfo type, and a cast is required. |
|
Return a shm_info structure that contains information about system resources consumed by the specified shared memory segment. |
IPC_STAT or SHM_STAT is specified in cmb, but the mode field in shm_perm does not allow any read operation on the shared memory segment identified by shmid. |
|
SHM_STAT or IPC_STAT is specified in cmd, but the address pointed to by buf is not accessible. |
|
The value of shmid or cmd is invalid. When SHM_STAT is specified in cmd, the value is considered invalid if the index specified by shmid refers to an unused array slot. |
|
IPC_SET or IPC_RMID is attempted, but the effective user ID of the calling process is not that of the creator or the owner, and the process is not privileged. |
shmaddr | Indicates the pointer to the address of the shared memory segment to detach. The value of this parameter should be the pointer previously returned by shmat(). |
No shared memory segment is attached to shmaddr, or shmaddr is not aligned. |
key | Indicates the key of the shared memory segment. |
size | Indicates the size of the shared memory segment to create. The actual size is rounded up to a multiple of PAGE_SIZE. |
shmflg | Indicates the attribute of the shared memory segment, which is the bitwise OR combination of one or more of the following flags: |
Create a new shared memory segment. If this flag is not specified, this function will find the segment with the specified key and check to see whether the user has permission to access the segment. |
|
Used together with IPC_CREAT to ensure that this function is called to create a shared memory segment. The calling fails if the segment already exists. |
The current process does not have the permission to obtain the specified shared memory segment. |
|
IPC_EXCL and IPC_CREAT are specified, but the shared memory segment associated with key already exists. |
|
The size specified for the shared memory segment to create is less than the value of shmmin in shminfo or greater than the value of shmmax. The shared memory segment associated with key already exists, but the value of size is greater than the size of the segment. |
|
No segment exists for the given key, and IPC_CREAT is not specified. |
|
Allocating a segment of the requested size would cause the system to exceed the system-wide limit on shared memory (shmall in shminfo). |