目录

ddk_api.h

Overview

Declares the BASE DDK APIs used by the host and provides APIs for creating, mapping, unmapping, and destroying the shared memory. This module helps you efficiently manage shared memory resources in drivers. It is applicable to scenarios where data needs to be shared with the driver, helping simplify memory management and improve data transmission efficiency.

File to include: <ddk/ddk_api.h>

Library: libddk_base.z.so

System capability: SystemCapability.Driver.DDK.Extension

Since: 12

Related module: Ddk

Summary

[h2]Function

Name Description
DDK_RetCode OH_DDK_CreateAshmem(const uint8_t *name, uint32_t size, DDK_Ashmem **ashmem) Creates shared memory. To prevent resource leaks, call OH_DDK_DestroyAshmem() to destroy the shared memory when it is no longer needed.
DDK_RetCode OH_DDK_MapAshmem(DDK_Ashmem *ashmem, const uint8_t ashmemMapType) Maps the created shared memory to the user space. Call OH_DDK_CreateAshmem() to create shared memory first. Call OH_DDK_UnmapAshmem() to unmap the shared memory that is no longer needed.
DDK_RetCode OH_DDK_UnmapAshmem(DDK_Ashmem *ashmem) Unmaps the shared memory. Call OH_DDK_MapAshmem() to map the shared memory first. Unmap the shared memory when it is no longer accessed to save address space.
DDK_RetCode OH_DDK_DestroyAshmem(DDK_Ashmem *ashmem) Destroys the shared memory created by OH_DDK_CreateAshmem(). You are advised to call OH_DDK_UnmapAshmem() to unmap the shared memory first. Destroy the shared memory in a timely manner when it is no longer used to release system resources.

Function Description

[h2]OH_DDK_CreateAshmem()

DDK_RetCode OH_DDK_CreateAshmem(const uint8_t *name, uint32_t size, DDK_Ashmem **ashmem)

Description

Creates shared memory. To prevent resource leaks, call OH_DDK_DestroyAshmem() to destroy the shared memory when it is no longer needed.

Since: 12

Parameters

Name Description
const uint8_t *name Pointer to the name string of the shared memory. The name is used to identify the shared memory. The value of name cannot be null. You are advised to use a unique name to avoid conflicts.
uint32_t size Size of the buffer corresponding to the shared memory, in bytes. The value must be greater than 0. If the value is too large, the memory usage will be too high, affecting the running performance.
DDK_Ashmem **ashmem Pointer to the shared memory object created.

Returns

Type Description
DDK_RetCode

DDK_SUCCESS: The API call is successful. The shared memory has been successfully created and initialized.

DDK_INVALID_PARAMETER: The input name is a null pointer, the value of size is 0, or the input ashmem is a null pointer. Check the input parameter values.

DDK_FAILURE: The shared memory or the DDK_Ashmem struct fails to be created. Check the size of the allocated memory and related permissions.

[h2]OH_DDK_MapAshmem()

DDK_RetCode OH_DDK_MapAshmem(DDK_Ashmem *ashmem, const uint8_t ashmemMapType)

Description

Maps the created shared memory to the user space. Call OH_DDK_CreateAshmem() to create shared memory first. Call OH_DDK_UnmapAshmem() to unmap the shared memory that is no longer needed.

Since: 12

Parameters

Name Description
DDK_Ashmem *ashmem Pointer to the descriptor of the shared memory to be mapped.
const uint8_t ashmemMapType Protection permission value of the shared memory. For details, see the general definition of the Ashmem protection permission.

Returns

Type Description
DDK_RetCode

DDK_SUCCESS: The API call is successful. The shared memory has been mapped to the user space.

DDK_NULL_PTR: The input ashmem is a null pointer. Check the input parameter values.

DDK_FAILURE: The file descriptor of the shared memory is invalid or the shared memory has not been created. Check whether the shared memory has been created and ensure that the file descriptor is not closed.

DDK_INVALID_OPERATION: The shared memory fails to be mapped. The possible causes include abnormal mapping status or insufficient permissions. Check the mapping status and permissions of the shared memory and try again.

[h2]OH_DDK_UnmapAshmem()

DDK_RetCode OH_DDK_UnmapAshmem(DDK_Ashmem *ashmem)

Description

Unmaps the shared memory. Call OH_DDK_MapAshmem() to map the shared memory first. Unmap the shared memory when it is no longer accessed to save address space.

Since: 12

Parameters

Name Description
DDK_Ashmem *ashmem Pointer to the descriptor of the shared memory to be unmapped.

Returns

Type Description
DDK_RetCode

DDK_SUCCESS: The API call is successful. The shared memory has been successfully unmapped from the user space.

DDK_NULL_PTR: The input ashmem is a null pointer. Check the input parameter values.

DDK_FAILURE: The file descriptor of the shared memory is invalid or the mapping has not been performed.

[h2]OH_DDK_DestroyAshmem()

DDK_RetCode OH_DDK_DestroyAshmem(DDK_Ashmem *ashmem)

Description

Destroys the shared memory created by OH_DDK_CreateAshmem(). You are advised to call OH_DDK_UnmapAshmem() to unmap the shared memory first. Destroy the shared memory in a timely manner when it is no longer used to release system resources.

Since: 12

Parameters

Name Description
DDK_Ashmem *ashmem Pointer to the descriptor of the shared memory to be destroyed.

Returns

Type Description
DDK_RetCode

DDK_SUCCESS: The API call is successful. The shared memory has been successfully destroyed and released.

DDK_NULL_PTR: The input ashmem is a null pointer. Check the input parameter values.

DDK_FAILURE: The file descriptor of the shared memory is invalid. The possible cause is that the shared memory has been released or destroyed multiple times. Check the status of the shared memory.