Memory
Basic Concepts
Memory management is an important procedure in software development, including memory allocation, usage, and reclamation.
Sound memory management approaches and strategies help you improve software performance and reliability.
When to Use
For user-space development, the OpenHarmony kernel provides a set of APIs for you to perform memory-related operations, such as memory application, release, remapping, and attribute setting, in addition to standard APIs provided by the C library.
Available APIs
Table 1 Standard APIs in the C library
Details on API differences:
-
mmap
Function prototype:
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
Function description: applies for virtual memory.
Parameter description:
NOTE: For details about the implementation differences of mmap between the OS and Linux, see Differences from the Linux Standard Library.
Return values:
- Returns the pointer to the page-aligned address where the mapping is placed if the operation is successful.
- Returns (void *)-1 if the operation fails.
-
munmap
Function prototype:
int munmap(void *addr, size_t length);
Function description: releases virtual memory.
Parameter description:
Indicates the pointer to the start address of the memory region to unmap.
Return values:
- Returns 0 if the operation is successful.
- Returns -1 if the operation fails.
-
mprotect
Function prototype:
int mprotect(void *addr, size_t length, int prot);
Function description: modifies the access permission on a memory region.
Parameter description:
Return values:
- Returns 0 if the operation is successful.
- Returns -1 if the operation fails.
-
mremap
Function prototype:
void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, void new_address);
Function description: remaps the virtual memory address.
Parameter description:
Return values:
- Returns the pointer to the new virtual memory area if the operation is successful.
- Returns (void *)-1 if the operation fails.