# Memory - [Basic Concepts](#section1392116583424) - [When to Use](#section159581619194319) - [API Description](#section114001032104317) ## 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. ## API Description **Table 1** Standard APIs in the C library

Header File

Function

Description

strings.h

int bcmp(const void *s1, const void *s2, size_t n)

Compares byte sequences.

strings.h

void bcopy(const void *src, void *dest, size_t n)

Copies byte sequences.

strings.h

void bzero(void *s, size_t n)

Sets byte sequences to zero.

string.h

void *memccpy(void *dest, const void *src, int c, size_t n)

Copies the first n bytes from the source memory area pointed to by src to the destination memory area pointed to by dest. The copy checks whether a character specified by c is found. If c is found, this function returns the next character of character c in the destination memory area.

string.h

void *memchr(const void *s, int c, size_t n)

Searches for the first occurrence of the character specified by c in the n-byte memory area pointed to by s.

string.h

int memcmp(const void *s1, const void *s2, size_t n)

Compares two memory areas.

string.h

void *memcpy(void *dest, const void *src, size_t n)

Copies n bytes from the source memory area pointed to by src to the destination memory area pointed to by dest.

string.h

void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)

Searches for a needle string in its haystack string.

string.h

void *memmove(void *dest, const void *src, size_t n)

Copies n bytes from the source memory area pointed to by src to the destination memory area pointed to by dest.

string.h

void *mempcpy(void *dest, const void *src, size_t n)

Copies n bytes from the source memory area pointed to by src to the destination memory area pointed to by dest.

string.h

void *memset(void *s, int c, size_t n)

Copies a character to the specified memory area.

stdlib.h

void *malloc(size_t size)

Dynamically allocates a memory block of size.

stdlib.h

void *calloc(size_t nmemb, size_t size)

Dynamically allocates nmemb memory blocks of size.

stdlib.h

void *realloc(void *ptr, size_t size)

Changes the size of the memory block pointed to by ptr to size bytes.

stdlib.h/malloc.h

void *valloc(size_t size)

Allocates a block of memory with the specified size and aligns the allocated memory by page size.

stdlib.h

void free(void *ptr)

Releases the memory space pointed to by ptr.

malloc.h

size_t malloc_usable_size(void *ptr)

Obtains the size of the memory block pointed to by ptr.

unistd.h

int getpagesize(void)

Obtains the page size.

unistd.h

void *sbrk(intptr_t increment)

Changes the data segment size.

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:**

Parameter

Description

addr

Indicates the pointer to the start address of the mapping between the virtual address space of the calling process and a file or device. If this parameter is NULL, the kernel determines the address to start (recommended). Otherwise, the portability of the program will deteriorate, because the available addresses vary depending on the OS.

length

Indicates the length of the mapping.

prot

Indicates the permission to be granted on the mapping area. The options are as follows:

  • PROT_READ: The mapping area is readable.
  • PROT_WRITE: The mapping area is writable.
  • PROT_EXEC: The mapping area is executable.
  • PROT_NONE: The mapping area cannot be accessed.

flags

Specifies whether updates are visible to other processes mapping the same segment. The options are as follows:

  • MAP_PRIVATE: The mapping area is private, and updates to the mapping are invisible to other processes mapping the same segment.
  • MAP_SHARED: Updates to the mapping are visible to other processes mapping the same segment, and are stored to the disk file.

fd

Indicates the file descriptor.

offset

Indicates the offset into the file where the mapping will start.

>![](../public_sys-resources/icon-note.gif) **NOTE:** >For details about the implementation differences between **mmap** and Linux function, see [Differences from the Linux Standard Library](kernel-lite-small-lib-differ.md). **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:**

Parameter

Description

addr

Indicates the pointer to the start address of the memory region to unmap.

length

Indicates the length of the address range 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:**

Parameter

Description

addr

Indicates the pointer to the start address of the memory region to modify, which must be a multiple of the page size. If the access permission is abnormal, the kernel throws an exception and kills the process rather than send SIGSEGV signals to the current process.

length

Indicates the length of the memory region to modify.

prot

Indicates the permission of the memory region to modify. The options are as follows:

  • PROT_READ: The memory region is readable.
  • PROT_WRITE: The memory region is writable.
  • PROT_EXEC: The memory region is executable.
  • PROT_NONE: The memory region cannot be accessed.
**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:**

Parameter

Description

old_address

Indicates the old address of the virtual memory block that needs to be expanded or shrunk. The old_address must be page-aligned.

old_size

Indicates the old size of the virtual memory block.

new_size

Indicates the new size of the virtual memory block.

flags

Indicates the flags to control the remapping. If there is no sufficient space to expand the mapping in the current location, the operation will fail.

  • MREMAP_MAYMOVE: allows the kernel to relocate the mapping to a new virtual address.
  • MREMAP_FIXED: enables the mremap() function to accept the fifth parameter void *new_address, which specifies that the mapping address must be page-aligned. All previous mappings within the address range specified by new_address and new_size are unmapped. If MREMAP_FIXED is specified, MREMAP_MAYMOVE must also be specified.
**Return values:** - Returns the pointer to the new virtual memory area if the operation is successful. - Returns **\(void \*\)-1** if the operation fails.