5.4. Memory Management - hbmem

5.4.1. Module Description

The X5 SoC provides various hardware acceleration units, including ISP, GDC, VSE, GPU, BPU, etc. Data transfer among these hardware acceleration units and between them and the CPU relies on DDR.
Since hardware acceleration units require physically contiguous addresses when accessing DDR data, while Linux user-space memory allocation interfaces can only guarantee virtual address continuity, and as the system runs,
even with sufficient physical memory, large blocks of physically contiguous memory may fail to be allocated due to memory fragmentation. Therefore, physical memory is reserved for these hardware acceleration units in the device tree.
For more information, please refer to: System Reserved Memory.

The hbmem module provides rich interfaces at the application layer, supporting five management methods for system-reserved memory: memory allocation, memory sharing, memory queue management, memory pool management, and shared memory pool management.

Function Name Function Description
Memory Allocation Allocate physically contiguous memory from system-reserved memory, and support DMA copy
Memory Sharing Support users sharing memory space across different threads or processes using memory handles, automatically manage memory reference counts to ensure in-use memory is not accidentally released;
Memory Queue Management Provide input/output queue management. The memory allocator places memory into the input queue for the consumer to use,
and the consumer returns unused memory to the output queue for deallocation by the allocator; (Note: Not supported across multiple processes)
Memory Pool Support users creating a local memory pool via memory allocation, then allocating and freeing small memory blocks from this pool.
Shared Memory Pool Support multi-process sharing, but buffers allocated from the shared memory pool must have the same size, and buffer size can only be specified when creating the shared memory pool

5.4.1.1. Memory Allocation

Memory allocation is the foundation of hbmem functionality, and the other four management functions (memory sharing, memory queue management, memory pool, and shared memory pool) are all related to memory allocation.
Key features of memory allocation interfaces include:

  1. Provide basic interfaces for allocating and freeing physically contiguous memory. See: Memory Allocation Interfaces

  2. After successful allocation, users obtain a corresponding file handle. Based on this file handle, users can perform operations such as Cache control, DMA copy, and retrieving buffer information. See: Memory Allocation Interfaces

  3. Support setting various attribute configurations, including Cache attributes, memory heap attributes, and hardware accelerator identifier attributes. See: Memory Allocation Attributes

  4. The memory allocation software module provides three types of memory units: contiguous memory block, graphic (image) buffer, and graphic buffer group.

Note: It is not recommended for users to directly mmap or pass physical addresses, as these operations do not increase the reference count of the memory, which may lead to accessing memory after it has been released.

Memory Allocation Unit Description

Memory Type Structure Use Case Characteristics
Contiguous Memory Block hb_mem_common_buf_t Bitstreams from encoders or pure featuremaps used by BPU A simple, single block of contiguous physical space
Graphic Buffer hb_mem_graphic_buf_t Image data used by ISP, GDC, VSE Supports multiple planes with multiple contiguous spaces
Graphic Buffer Group hb_mem_graphic_buf_group_t Multi-layer image data used by PYM module Multiple image data stored in one array

Cache Operation Description

Modern CPUs introduce Cache to speed up data access by storing part of main memory (DDR) data to improve access speed:

  1. CPU Write to DDR: When the CPU modifies data, it typically does not immediately write the data back to DDR, but first updates the copy in Cache.
    This mechanism is called “write-back caching,” which effectively reduces direct writes to DDR and improves performance.

  2. CPU Read from DDR: When the CPU reads data from DDR, it first checks the Cache. If the target data exists in Cache,
    the CPU reads directly from Cache instead of accessing DDR. This significantly improves read efficiency.

Cache is a hardware unit dedicated to the CPU. When only the CPU accesses DDR, data consistency is automatically ensured by internal CPU mechanisms without issues.
However, when other hardware acceleration units (e.g., ISP, VSE, GDC, GPU, etc.) also need to access DDR, they cannot perceive the CPU’s Cache state,
leading to data consistency problems.

Therefore, the hbmem module provides two API interfaces to allow applications to actively operate the CPU Cache:

  1. Cache Flush: hb_mem_flush_buf

  2. Cache Invalidate: hb_mem_invalidate_buf

Detailed explanations of these two APIs are provided below.

Cache Flush

Cache flush writes data cached in Cache back to DDR.
Example scenario: Reading video frames from eMMC into DDR and then handing over to VSE for processing.

  1. Left diagram: Without calling Cache flush

  2. Right diagram: With Cache flush called at the appropriate time (hb_mem_flush_buf)

Block Diagram

Explanation of left diagram (cache consistency issue exists):

  • Step 1: CPU reads video data from eMMC, data passes through Cache during transfer

  • Step 2: Video data is stored in DDR. However, after reading completes, some data may remain in Cache and not fully written to DDR

  • Step 3: VSE reads video data from DDR for processing, but due to incomplete data (some still in Cache), the processed result is incorrect

Explanation of right diagram (cache consistency issue resolved):

  • Step 1: Same as left

  • Step 2: Same as left

  • Step 3: Call Cache flush interface hb_mem_flush_buf to write Cache data back to DDR

  • Step 4: VSE reads video data from DDR and processes it, now able to access complete video data

Cache Invalidate

Cache invalidation discards already-cached DDR data in Cache, marking it as invalid (miss).
Subsequent DDR accesses will not use old data in Cache but will force a read from DDR to get the latest data.

Example scenario: VSE reads video data from DDR, processes it, and saves it to eMMC:

  1. Left diagram: Without calling Cache invalidate

  2. Right diagram: With Cache invalidate called at the appropriate time (hb_mem_invalidate_buf)

Block Diagram

Explanation of left diagram (cache consistency issue exists):

  • Step 1: CPU reads video data from DDR and performs algorithm processing; Cache now holds some DDR data

  • Step 2: VSE reads video data from DDR

  • Step 3: VSE internally overlays text and writes the result back to DDR

  • Step 4: CPU continues reading video data from DDR. Since VSE wrote new data in Step 3, but Cache still holds old data from Step 1,
    CPU only reads data not present in Cache

  • Step 5: CPU writes the read data to eMMC. Since only partial new data was read in Step 4, the data written to eMMC is incorrect.

Explanation of right diagram (cache consistency issue resolved):

  • Step 1: Same as left

  • Step 2: Same as left

  • Step 3: Same as left

  • Step 4: Call Cache invalidate interface hb_mem_invalidate_buf to discard Cache data and mark it as invalid

  • Step 5: CPU reads video data from DDR. Since old Cache data was discarded in Step 4, CPU reads all the latest video data from DDR

  • Step 6: CPU writes the read data to eMMC. Since all data read in Step 5 is up-to-date, the data written to eMMC is correct.

5.4.1.2. Memory Sharing

Memory sharing module interfaces enable safe memory sharing among multiple threads/processes. Usage flow:

  1. Users can directly pass the structure corresponding to the allocated memory unit to another thread or process

  2. The receiving process imports the structure using the hb_mem_import_xxx interface (see table below), achieving safe buffer sharing.

Corresponding structures and interfaces for memory allocation units are:

  1. Contiguous memory block: hb_mem_common_buf_t

  2. Graphic buffer: hb_mem_graphic_buf_t

  3. Graphic buffer group: hb_mem_graphic_buf_group_t

Memory Type Structure hb_mem_import_xxx Interface
Contiguous Memory hb_mem_common_buf_t hb_mem_import_com_buf
Graphic Buffer hb_mem_graphic_buf_t hb_mem_import_graph_buf
Graphic Buffer Group hb_mem_graphic_buf_group_t hb_mem_import_graph_buf_group

The diagram below illustrates memory sharing between multiple processes (using hb_mem_common_buf_t as an example):

Block Diagram

5.4.1.3. Memory Queue Management

The memory queue management module provides a general-purpose queue mechanism supporting data flow between producers and consumers.
Internally, the module maintains two queues: empty data queue and valid data queue. Data flows between producer and consumer via these two queues:
Steps:

  1. Producer gets an item from the empty data queue and fills it with valid data

  2. Producer stores the filled item into the valid data queue

  3. Consumer gets an item from the valid data queue and reads its valid data

  4. Consumer returns the processed item back to the empty data queue

Block Diagram

Notes:

  1. Only supports intra-process operations

  2. The memory queue itself uses malloc for memory allocation; stored data can be any of the memory allocation unit structures mentioned

  3. The memory queue is circular; when full, writing a new item overwrites the oldest one

5.4.1.4. Memory Pool

The memory pool module provides interfaces allowing users to create a local memory pool and efficiently allocate and free small memory blocks from it.

  1. Implementation: During program initialization, pre-allocate a large memory block as the pool resource. Subsequent small allocations are made from this block via memory pool interfaces.

  2. Goal: Enables fast user-space memory allocation without frequent kernel transitions, improving memory management efficiency.

5.4.1.5. Shared Memory Pool

Shared memory pool shares the same concept as memory pool—supporting creation of a local memory pool—but differs in the following aspects:

  1. Memory allocated from the shared memory pool supports multi-process sharing

  2. Buffers allocated from the shared memory pool have identical sizes, and buffer size can only be set at creation time.

5.4.2. Reference Examples

  • Some hbmem example code can be found in the sample_hbmem section

5.4.3. API Reference

  1. Library: libhbmem.so

  2. Header Files: hb_mem_mgr.h, hbmem.h, and hb_mem_err.h.

5.4.3.1. Memory Allocation Interfaces

API Interface Functionality
hb_mem_get_version Get module version number
hb_mem_module_open Open memory module
hb_mem_module_close Close memory module
hb_mem_alloc_com_buf Allocate common buffer
hb_mem_get_com_buf Get common buffer info via fd
hb_mem_alloc_graph_buf Allocate graphic buffer
hb_mem_get_graph_buf Get graphic buffer info via fd
hb_mem_free_buf Free buffer via fd
hb_mem_invalidate_buf Invalidate buffer corresponding to fd, when user-allocated buffer attributes require it
hb_mem_flush_buf Flush buffer corresponding to fd, when user-allocated buffer attributes require it
hb_mem_is_valid_buf Determine if input virtual address is a valid address allocated from memory module
hb_mem_get_phys_addr Get physical address corresponding to input virtual address
hb_mem_get_buf_info Get starting virtual address and buffer size for input virtual address
hb_mem_invalidate_buf_with_vaddr Invalidate buffer corresponding to virtual address, when buffer attributes require it
hb_mem_flush_buf_with_vaddr Flush buffer corresponding to virtual address, when buffer attributes require it
hb_mem_get_com_buf_with_vaddr Get common buffer info via virtual address
hb_mem_get_graph_buf_with_vaddr Get graphic buffer info via virtual address
hb_mem_free_buf_with_vaddr Free buffer via virtual address
hb_mem_alloc_graph_buf_group Allocate a group of graphic buffers
hb_mem_get_graph_buf_group Get graphic buffer group via fd (any valid fd in the group)
hb_mem_get_graph_buf_group_with_vaddr Get graphic buffer group via virtual address (any valid virtual address in the group)
hb_mem_dma_copy Copy data from source address to destination address

5.4.3.2. Memory Sharing Interfaces

API Interface Functionality
hb_mem_import_com_buf Import shared common buffer, get new common buffer info
hb_mem_import_com_buf_with_paddr Share memory via physical address; only applicable to memory allocated by hbmem
hb_mem_import_graph_buf Import shared graphic buffer, get new graphic buffer info
hb_mem_import_graph_buf_group Share a group of graphic buffers
hb_mem_get_share_info Get number of sharing clients for buffer corresponding to fd
hb_mem_get_share_info_with_vaddr Get number of sharing clients for buffer corresponding to virtual address
hb_mem_wait_share_status Wait until number of sharing clients for buffer (fd) is ≤ target value
hb_mem_wait_share_status_with_vaddr Wait until number of sharing clients for buffer (virtual address) is ≤ target value

5.4.3.3. Memory Queue Management Interfaces

API Interface Functionality
hb_mem_create_buf_queue Create memory queue
hb_mem_destroy_buf_queue Destroy memory queue
hb_mem_dequeue_buf Producer gets an available slot
hb_mem_queue_buf Producer enqueues filled element into the slot
hb_mem_request_buf Consumer gets enqueued element from queue
hb_mem_release_buf Consumer releases used element index
hb_mem_cancel_buf Producer cancels dequeued slot or consumer cancels requested slot

5.4.3.4. Memory Pool Interfaces

API Interface Functionality
hb_mem_pool_create Create a memory pool
hb_mem_pool_destroy Destroy a memory pool
hb_mem_pool_alloc_buf Allocate a common buffer from memory pool
hb_mem_pool_free_buf Free buffer allocated from memory pool
hb_mem_pool_get_info Get real-time information about memory pool

5.4.3.5. Shared Memory Pool Interfaces

API Interface Functionality
hb_mem_share_pool_create Create a shared memory pool
hb_mem_share_pool_destroy Destroy a shared memory pool
hb_mem_share_pool_alloc_buf Allocate a common buffer from shared memory pool
hb_mem_share_pool_free_buf Free buffer allocated from shared memory pool
hb_mem_share_pool_get_info Get real-time information about shared memory pool

5.4.3.6. General Information Get/Set Interfaces

API Interface Functionality
hb_mem_get_buf_type_with_vaddr Get type of buffer corresponding to virt_addr
hb_mem_get_buf_type_and_buf_with_vaddr Get buffer type and convert it to com buf or graph buf
hb_mem_get_buffer_process_info Get process PID holding buffer via virtual address
hb_mem_get_buffer_process_info_with_share_id Get process PID holding buffer via share_id
hb_mem_get_consume_info Get consume count of buffer (via fd)
hb_mem_get_consume_info_with_vaddr Get consume count of buffer (via virt_addr)
hb_mem_wait_consume_status Wait until consume count of buffer (fd) becomes share_consume_cnt, timeout in ms
hb_mem_wait_consume_status_with_vaddr Wait until consume count of buffer (virt_addr) becomes share_consume_cnt, timeout in ms
hb_mem_inc_com_buf_consume_cnt Increment consume count of specified common buffer
hb_mem_inc_graph_buf_consume_cnt Increment consume count of specified graphic buffer
hb_mem_inc_graph_buf_group_consume_cnt Increment consume count of all buffers in graphic buffer group
hb_mem_dec_consume_cnt Decrement consume count of buffer (via fd)
hb_mem_dec_consume_cnt_with_vaddr Decrement consume count of buffer (via virt_addr)
hb_mem_get_buf_and_type_with_vaddr Get buffer type and corresponding buffer via virtual address (supports graphic buffer group)
hb_mem_inc_user_consume_cnt Increment user-space reference count of specified buffer (via fd)
hb_mem_dec_user_consume_cnt Decrement user-space reference count of specified buffer (via fd)
hb_mem_inc_user_consume_cnt_with_vaddr Increment user-space reference count of specified buffer (via virtual address)
hb_mem_dec_user_consume_cnt_with_vaddr Decrement user-space reference count of specified buffer (via virtual address)

5.4.4. Interface Descriptions

5.4.4.1. hb_mem_get_version

【Function Declaration】

int32_t hb_mem_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);

【Parameter Description】

  • [OUT] major: Major version number

  • [OUT] minor: Minor version number

  • [OUT] patch: Patch version number

【Return Value】

  • 0: Success

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters. Check input parameter validity as per log suggestions

【Function Description】

Get module version number.

【Example Code】

    int main(int argc, char *argv[])
    {
        uint32_t major, minor, patch;
        hb_mem_get_version(&major, &minor, &patch);
        return 0;
    }

5.4.4.2. hb_mem_module_open

【Function Declaration】

int32_t hb_mem_module_open(void);

【Parameter Description】

  • NA

【Return Value】

  • 0: Operation successful

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory. Check ion reserved memory and system memory usage

  • HB_MEM_ERR_MODULE_OPEN_FAIL: Failed to open memory module

【Function Description】

Open the memory module.

【Notes】

  • hbn_vflow_create internally calls hb_mem_module_open, so no additional call is needed.

  • In multi-threaded scenarios, calling hb_mem_module_open before hbn_vflow_create may cause the main process to exceed the file descriptor limit, leading to memory allocation failure.

  • Must be paired with hb_mem_module_close.

【Example Code】

    int main(int argc, char *argv[])
    {
        hb_mem_module_open();
        hb_mem_module_close();
        return 0;
    }

5.4.4.3. hb_mem_module_close

【Function Declaration】

int32_t hb_mem_module_close(void);

【Parameter Description】

  • NA

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module not opened. Open the module first.

【Function Description】

Close the memory module.

【Notes】

  • Must be paired with hb_mem_module_open.

【Example Code】

Refer to: hb_mem_module_open

5.4.4.4. hb_mem_alloc_com_buf

【Function Declaration】

int32_t hb_mem_alloc_com_buf(uint64_t size, int64_t flags, hb_mem_common_buf_t *buf);

【Parameter Description】

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module not opened. Open the module first.

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters. Check input parameter validity as per log suggestions

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory. Check ion reserved memory and system memory usage

  • HB_MEM_ERR_INVALID_FD: File descriptor abnormal. Invalid file descriptor, unable to find corresponding buffer. Check logs for details

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit. Unable to allocate more. Consider increasing user file descriptor limit via ulimit

【Function Description】

Allocate a common buffer.

【Example Code】

    int main(int argc, char *argv[])
    {
        int32_t w = 1920, h = 1080, format = MEM_PIX_FMT_NV12, stride = 0,
        vstride = 0;
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        uint64_t offset = 0, offset_size;
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_common_buf_t com_buf_info = {0, };
        hb_mem_graphic_buf_t graph_buf = {0, };
        hb_mem_module_open();
        // test cached graph buffer
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_graph_buf(w, h, format, flags, stride, vstride,
            &graph_buf);
        offset_size = graph_buf.size[0];
        hb_mem_invalidate_buf(graph_buf.fd[0], offset, offset_size);
        hb_mem_free_buf(graph_buf.fd[0]);
        // test cached common buffer
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        offset_size = size;
        hb_mem_get_com_buf(com_buf.fd, &com_buf_info);
        hb_mem_invalidate_buf(com_buf.fd, offset, offset_size);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.5. hb_mem_get_com_buf

【Function Declaration】

int32_t hb_mem_get_com_buf(int32_t fd, hb_mem_common_buf_t *buf);

【Parameter Description】

  • [IN] fd: file descriptor associated with the buffer

  • [OUT] buf: common buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found. Check logs for detailed information

【Function Description】

Obtain common buffer information via fd.

【Example Code】

See: hb_mem_alloc_com_buf

5.4.4.6. hb_mem_alloc_graph_buf

【Function Declaration】

int32_t hb_mem_alloc_graph_buf(int32_t w, int32_t h, int32_t format, int64_t flags, int32_t stride, int32_t vstride, hb_mem_graphic_buf_t * buf);

【Parameter Description】

  • [IN] w: Image width, (0, ∞)

  • [IN] h: Image height, (0, ∞)

  • [IN] format: Image format, refer to: hbmem Image Format

  • [IN] flags: Buffer attributes, refer to: hbmem Memory Allocation Attributes

  • [IN] stride: Horizontal stride of image, 0 or [w, ∞); 0 means internally determined

  • [IN] vstride: Vertical stride of image, 0 or [h, ∞); 0 means internally determined

  • [OUT] buf: graphic buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found. Check logs for detailed information

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit; no more file descriptors can be allocated. It is recommended to increase the user file descriptor limit using ulimit

【Function Description】

Allocate graphic buffer.

【Example Code】


    int main(int argc, char *argv[])
    {
        int32_t w = 1280, h = 720, format = MEM_PIX_FMT_NV12, stride = 0, vstride = 0;
        int64_t flags = HB_MEM_USAGE_CPU_READ_NEVER;
        hb_mem_graphic_buf_t graph_buf = {0, };
        hb_mem_graphic_buf_t info = {0, };
        hb_mem_module_open();
        hb_mem_alloc_graph_buf(w, h, format, flags, stride, vstride,
            &graph_buf);
        hb_mem_get_graph_buf(graph_buf.fd[0], &info);
        hb_mem_free_buf(graph_buf.fd[0]);
        hb_mem_module_close();
        return 0;
    }

5.4.4.7. hb_mem_get_graph_buf

【Function Declaration】

int32_t hb_mem_get_graph_buf(int32_t fd, hb_mem_graphic_buf_t *buf);

【Parameter Description】

  • [IN] fd: file descriptor associated with the buffer, range [0, ∞)

  • [OUT] buf: graphic buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found. Check logs for detailed information

【Function Description】

Obtain graphic buffer information via fd.

【Example Code】

See: hb_mem_alloc_graph_buf

5.4.4.8. hb_mem_free_buf

【Function Declaration】

int32_t hb_mem_free_buf(int32_t fd);

【Parameter Description】

  • [IN] fd: file descriptor associated with the buffer, range [0, ∞)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found. Check logs for detailed information

【Function Description】

Free buffer via fd.

【Example Code】

See: hb_mem_alloc_graph_buf

5.4.4.9. hb_mem_invalidate_buf

【Function Declaration】

int32_t hb_mem_invalidate_buf(int32_t fd, uint64_t offset, uint64_t size);

【Parameter Description】

  • [IN] fd: file descriptor associated with the buffer, range [0, ∞)

  • [IN] offset: Offset address of the buffer to be invalidated, range [0, ∞)

  • [IN] size: Size of the buffer to be invalidated, range (0, ∞)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found. Check logs for detailed information

【Function Description】

Invalidate the buffer associated with fd. When the allocated buffer has the attribute HB_MEM_USAGE_CACHED (see: hbmem Memory Allocation Attributes), it indicates that the buffer is cacheable. This operation must be performed by the user before reading from the buffer.

【Example Code】

See: hb_mem_alloc_com_buf

5.4.4.10. hb_mem_flush_buf

【Function Declaration】

int32_t hb_mem_flush_buf(int32_t fd, uint64_t offset, uint64_t size);

【Parameter Description】

  • [IN] fd: file descriptor associated with the buffer, range [0, ∞)

  • [IN] offset: Offset address of the buffer to be flushed, range [0, ∞)

  • [IN] size: Size of the buffer to be flushed, range (0, ∞)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found. Check logs for detailed information

【Function Description】

Flush the buffer associated with fd. When the allocated buffer has the attribute HB_MEM_USAGE_CACHED (see: hbmem Memory Allocation Attributes), it indicates that the buffer is cacheable. This operation must be performed by the user after writing to the buffer;

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags = HB_MEM_USAGE_CPU_READ_OFTEN |
        HB_MEM_USAGE_CPU_WRITE_OFTEN | HB_MEM_USAGE_CACHED;
        uint64_t size = 1024 * 4; // 4k
        uint64_t offset = 0;
        int32_t valid;
        uint64_t phys_addr;
        uint64_t start_virt;
        uint64_t total_size;
        int64_t out_flags;
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_module_open();
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_is_valid_buf((uint64_t)com_buf.virt_addr, com_buf.size,
            &valid);
        hb_mem_get_phys_addr((uint64_t)com_buf.virt_addr, &phys_addr);
        hb_mem_get_buf_info((uint64_t)com_buf.virt_addr, &start_virt,
            &total_size, &out_flags);
        hb_mem_flush_buf(com_buf.fd, offset, size);
        hb_mem_module_close();
        return 0;
    }

5.4.4.11. hb_mem_is_valid_buf

【Function Declaration】

int32_t hb_mem_is_valid_buf(uint64_t virt_addr, uint64_t size, int32_t *valid);

【Parameter Description】

  • [IN] virt_addr: Virtual address, can be an offset virtual address, range (0, ∞)

  • [IN] size: Buffer size, range (0, ∞)

  • [OUT] valid: Whether the virtual address is valid; 0: invalid, 1: valid

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

【Function Description】

Determine whether the input virtual address is a valid address allocated from the memory module.

【Example Code】

See: hb_mem_flush_buf

5.4.4.12. hb_mem_get_phys_addr

【Function Declaration】

int32_t hb_mem_get_phys_addr(uint64_t virt_addr, uint64_t *phys_addr);

【Parameter Description】

  • [IN] virt_addr: Virtual address, can be an offset virtual address, range (0, ∞)

  • [OUT] phys_addr: Corresponding physical address, range (0, ∞)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found. Check logs for detailed information

【Function Description】

Obtain the physical address corresponding to the input virtual address.

【Example Code】

See: hb_mem_flush_buf

5.4.4.13. hb_mem_get_buf_info

【Function Declaration】

int32_t hb_mem_get_buf_info(uint64_t virt_addr, uint64_t *start, uint64_t *size, int64_t *flags);

【Parameter Description】

  • [IN] virt_addr: Virtual address, can be an offset virtual address, range (0, ∞)

  • [OUT] start: Corresponding starting virtual address, range (0, ∞)

  • [OUT] size: Corresponding buffer size, range (0, ∞)

  • [OUT] flags: Corresponding buffer attributes, refer to: hbmem Memory Allocation Attributes

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; an invalid virtual address exists and the corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

【Function Description】

Obtain the starting virtual address and buffer size corresponding to the input virtual address.

【Example Code】

See: hb_mem_flush_buf

5.4.4.14. hb_mem_invalidate_buf_with_vaddr

【Function Declaration】

int32_t hb_mem_invalidate_buf_with_vaddr(uint64_t virt_addr, uint64_t size);

【Parameter Description】

  • [IN] virt_addr: Virtual address, can be an offset virtual address, range (0, ∞)

  • [IN] size: Size of the buffer to be invalidated, range (0, ∞)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; an invalid virtual address exists and the corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

【Function Description】

Invalidate the buffer corresponding to the virtual address. When the allocated buffer has the attribute HB_MEM_USAGE_CACHED (see: hbmem Memory Allocation Attributes), it indicates that the buffer is cacheable. This operation must be performed by the user before reading from the buffer.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_invalidate_buf_with_vaddr((uint64_t)com_buf.virt_addr, size);
        hb_mem_module_close();
        return 0;
    }

5.4.4.15. hb_mem_flush_buf_with_vaddr

【Function Declaration】

int32_t hb_mem_flush_buf_with_vaddr(uint64_t virt_addr, uint64_t size);

【Parameter Description】

  • [IN] virt_addr: Virtual address, can be an offset virtual address, range (0, ∞)

  • [IN] size: Size of the buffer to be flushed, range (0, ∞)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; an invalid virtual address exists and the corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

【Function Description】

Flush the buffer corresponding to the virtual address. When the buffer allocated by the user has attributes containing HB_MEM_USAGE_CACHED, as described in: hbmem memory allocation attributes, it indicates that the buffer is cache-coherent. In this case, the user must perform this operation after writing to the buffer.

【Example Code】

    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_common_buf_t info = {0, };
        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_com_buf_with_vaddr((uint64_t)com_buf.virt_addr, &info);
        hb_mem_flush_buf_with_vaddr((uint64_t)com_buf.virt_addr, size);
        hb_mem_module_close();
        return 0;
    }

5.4.4.16. hb_mem_get_com_buf_with_vaddr

【Function Declaration】

int32_t hb_mem_get_com_buf_with_vaddr(uint64_t virt_addr, hb_mem_common_buf_t *buf);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, value range: (0, )

  • [OUT] buf: common buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check input parameter validity as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and no corresponding buffer information can be found. Check logs for detailed information (increase log level)

【Function Description】

Retrieve common buffer information via virtual address.

【Example Code】

Refer to: hb_mem_flush_buf_with_vaddr

5.4.4.17. hb_mem_get_graph_buf_with_vaddr

【Function Declaration】

int32_t hb_mem_get_graph_buf_with_vaddr(uint64_t virt_addr, hb_mem_graphic_buf_t *buf);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, value range: (0, )

  • [OUT] buf: graphic buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check input parameter validity as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and no corresponding buffer information can be found. Check logs for detailed information (increase log level)

【Function Description】

Retrieve graphic buffer information via virtual address.

【Example Code】

    int main(int argc, char *argv[])
    {
        int32_t w = 1920, h = 1080, format = MEM_PIX_FMT_NV12, stride = 0,
        vstride = 0;
        int64_t flags = HB_MEM_USAGE_CPU_READ_NEVER;
        uint64_t tmp_vaddr;
        hb_mem_graphic_buf_t graph_buf = {0, };
        hb_mem_graphic_buf_t info = {0, };
        hb_mem_module_open();
        hb_mem_alloc_graph_buf(w, h, format, flags, stride, vstride,
            &graph_buf);
        for (int i = 0; i < graph_buf.plane_cnt; i++) {
            tmp_vaddr = (uint64_t)graph_buf.virt_addr[i] + graph_buf.size[i]/2;
            hb_mem_get_graph_buf_with_vaddr(tmp_vaddr, &info);
        }
        hb_mem_free_buf_with_vaddr(tmp_vaddr);
        hb_mem_module_close();
        return 0;
    }

5.4.4.18. hb_mem_free_buf_with_vaddr

【Function Declaration】

int32_t hb_mem_free_buf_with_vaddr(uint64_t virt_addr);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, value range: (0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check input parameter validity as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and no corresponding buffer information can be found. Check logs for detailed information (increase log level)

【Function Description】

Free the buffer using its virtual address.

【Example Code】

Refer to: hb_mem_get_graph_buf_with_vaddr

5.4.4.19. hb_mem_import_com_buf

【Function Declaration】

int32_t hb_mem_import_com_buf(hb_mem_common_buf_t  *buf, hb_mem_common_buf_t  *out_buf);

【Parameter Description】

  • [IN] buf: Input common buffer

  • [OUT] out_buf: Exported common buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check input parameter validity as suggested by log messages

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit; unable to allocate more. Consider increasing the user file descriptor limit using ulimit

【Function Description】

Import shared memory of a common buffer and obtain new common buffer information.

【Example Code】

    int main(int argc, char *argv[])
    {
        int32_t ret;
        pid_t child;
        uint64_t size = 1024 * 4; // 4k
        int64_t flags = HB_MEM_USAGE_CPU_READ_NEVER;
        int32_t sd[2];
        socketpair(AF_UNIX, SOCK_STREAM, 0, sd);
        child = fork();
        if (child < 0) {
            printf("%s Fail to create child process(current %d, parent %d)!\n",
                __func__, getpid(), getppid());
            exit(1);
        } else if (child == 0) {
            hb_mem_common_buf_t recv_buf = {0, };
            hb_mem_common_buf_t out_buf = {0, };
            struct msghdr msg;
            struct iovec io;
            printf("%s In child process %d(parent %d).\n",
                getpid(), getppid());
            close(sd[1]);
            memset(&msg, 0x00, sizeof(msg));
            msg.msg_name = NULL;
            msg.msg_namelen = 0;
            msg.msg_iov = &io;
            msg.msg_iovlen = 1;
            io.iov_base = &recv_buf;
            io.iov_len = sizeof(recv_buf);
            ret = recvmsg(sd[0], &msg, 0);
            printf("%s [%d:%d] child recv msg 1\n", __func__, getpid(),
                getppid());
            hb_mem_module_open();
            hb_mem_import_com_buf(&recv_buf, &out_buf);
            ret = sendmsg(sd[0], &msg, 0);
            printf("%s [%d:%d] child send msg to parent to free buffer safely\n",
                __func__, getpid(), getppid());
            // do anything to the buffer
            hb_mem_free_buf(out_buf.fd);
            hb_mem_module_close();
            close(sd[0]);
            printf("%s [%d:%d] child quit\n", __func__, getpid(), getppid());
            exit(0);
        } else {
            hb_mem_common_buf_t recv_buf = {0, };
            hb_mem_common_buf_t in_buf = {0, };
            struct msghdr msg;
            struct iovec io;
            printf( "%s In parent process %d(pparent %d).\n",
                __func__, getpid(), getppid());
            hb_mem_module_open();
            hb_mem_alloc_com_buf(size, flags, &in_buf);
            close(sd[0]);
            memset(&msg, 0x00, sizeof(msg));
            msg.msg_name = NULL;
            msg.msg_namelen = 0;
            msg.msg_iov = &io;
            msg.msg_iovlen = 1;
            io.iov_base = &in_buf;
            io.iov_len = sizeof(in_buf);
            ret = sendmsg(sd[1], &msg, 0);
            printf("%s [%d:%d] parent send msg to share the buffer\n", __func__,
                getpid(), getppid());
            // wait message to free buffer
            io.iov_base = &recv_buf;
            io.iov_len = sizeof(recv_buf);
            ret = recvmsg(sd[1], &msg, 0);
            printf("%s [%d:%d] parent recv msg to free the buffer safely\n",
                __func__, getpid(), getppid());
            hb_mem_free_buf(in_buf.fd);
            hb_mem_module_close();
            printf("%s [%d:%d] parent quit\n", __func__, getpid(), getppid());
            close(sd[1]);
            exit(0);
        }
        return 0;
    }

5.4.4.20. hb_mem_import_graph_buf

【Function Declaration】

int32_t hb_mem_import_graph_buf(hb_mem_graphic_buf_t * buf, hb_mem_graphic_buf_t * out_buf);

【Parameter Description】

  • [IN] buf: Input graphic buffer

  • [OUT] out_buf: Exported graphic buffer

【Return Value】

  • 0: Operation successful

  • HB_MEM_ERR_UNKNOWN: Unknown error

  • HB_MEM_ERR_NOT_ALLOW: Operation not allowed

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check input parameter validity as suggested by log messages

【Function Description】

Import shared memory of a graphic buffer and obtain new graphic buffer information.

【Example Code】

    int main(int argc, char *argv[])
    {
        int32_t ret;
        pid_t child;
        int32_t w = 1920, h = 1080, format = MEM_PIX_FMT_NV12, stride = 0,
        vstride = 0;
        int64_t flags = HB_MEM_USAGE_CPU_READ_OFTEN |
        HB_MEM_USAGE_CPU_WRITE_OFTEN;
        hb_mem_graphic_buf_t in_info = {0, };
        int32_t sd[2];
        int32_t status;
        for (format = MEM_PIX_FMT_RGB565; format <= MEM_PIX_FMT_YUV400;
            format++) {
            printf("%s [%d:%d] Start Test format %d scenario 1.\n", TAG,
            getpid(), getppid(), format);
            socketpair(AF_UNIX, SOCK_STREAM, 0, sd);
            child = fork();
            if (child < 0) {
                printf("%s [%d:%d] Fail to create child process!\n",
                TAG, getpid(), getppid());
                exit(1);
            } else if (child == 0) {
                hb_mem_graphic_buf_t recv_buf = {0, };
                hb_mem_graphic_buf_t out_buf = {0, };
                struct msghdr msg;
                struct iovec io;
                printf("%s [%d:%d] In child process.\n",
                    TAG, getpid(), getppid());
                close(sd[1]);
                memset(&msg, 0x00, sizeof(msg));
                msg.msg_name = NULL;
                msg.msg_namelen = 0;
                msg.msg_iov = &io;
                msg.msg_iovlen = 1;
                io.iov_base = &recv_buf;
                io.iov_len = sizeof(recv_buf);
                ret = recvmsg(sd[0], &msg, 0);
                ASSERT_NE(ret, 0);
                ASSERT_EQ(flags, recv_buf.flags);
                printf("%s [%d:%d] child recv msg 1\n", TAG, getpid(), getppid());
                ASSERT_EQ(hb_mem_import_graph_buf(&recv_buf, &out_buf),
                (int32_t)HB_MEM_ERR_MODULE_NOT_FOUND);
                ASSERT_EQ(hb_mem_module_open(), 0);
                recv_buf.offset[0] = recv_buf.size[0]/2;
                recv_buf.offset[1] = recv_buf.size[1]/2;
                recv_buf.offset[2] = recv_buf.size[2]/2;
                ASSERT_EQ(hb_mem_import_graph_buf(&recv_buf, &out_buf), (int32_t)0);
                //compare_import_graphic_buf(&recv_buf, &out_buf, out_buf.plane_cnt);
                ASSERT_EQ(out_buf.offset[0], recv_buf.offset[0]);
                ASSERT_EQ(out_buf.offset[1], recv_buf.offset[1]);
                ASSERT_EQ(out_buf.offset[2], recv_buf.offset[2]);
                ASSERT_EQ(do_sys_command(flags & HB_MEM_USAGE_PRIV_MASK), 0);
                ret = sendmsg(sd[0], &msg, 0);
                ASSERT_NE(ret, 0);
                printf("%s [%d:%d] child send msg 2\n", TAG, getpid(), getppid());
                ASSERT_EQ(hb_mem_free_buf(out_buf.fd[0]), 0);
                ASSERT_NE(do_sys_command(flags & HB_MEM_USAGE_PRIV_MASK), 0);
                ASSERT_EQ(hb_mem_module_close(), 0);
                ASSERT_NE(do_sys_command(HB_MEM_USAGE_PRIV_MASK), 0);
                printf("%s [%d:%d] child quit\n", TAG, getpid(), getppid());
                close(sd[0]);
                exit(0);
        } else {
            hb_mem_graphic_buf_t in_buf = {0, };
            hb_mem_graphic_buf_t recv_buf = {0, };
            struct msghdr msg;
            struct iovec io;
            printf( "%s [%d:%d] In parent process.\n",
                TAG, getpid(), getppid());
            close(sd[0]);
            ASSERT_EQ(hb_mem_module_open(), 0);
            ASSERT_EQ(hb_mem_alloc_graph_buf(w, h, format, flags, stride,
                vstride, &in_buf),
                (int32_t)0);
            ASSERT_EQ(hb_mem_get_graph_buf(in_buf.fd[0], &in_info), (int32_t)0);
            ASSERT_EQ(do_sys_command(flags & HB_MEM_USAGE_PRIV_MASK), 0);
            memset(&msg, 0x00, sizeof(msg));
            msg.msg_name = NULL;
            msg.msg_namelen = 0;
            msg.msg_iov = &io;
            msg.msg_iovlen = 1;
            io.iov_base = &in_buf;
            io.iov_len = sizeof(in_buf);
            ret = sendmsg(sd[1], &msg, 0);
            ASSERT_NE(ret, 0);
            printf("%s [%d:%d] parent send msg 1\n", TAG, getpid(), getppid());
            io.iov_base = &recv_buf;
            io.iov_len = sizeof(recv_buf);
            ret = recvmsg(sd[1], &msg, 0);
            printf("%s [%d:%d] parent recv msg 2\n", TAG, getpid(), getppid());
            ASSERT_EQ(hb_mem_wait_share_status(in_buf.fd[0], 1, -1), (int32_t)0);
            ASSERT_EQ(hb_mem_free_buf(in_buf.fd[0]), 0);
            ASSERT_EQ(hb_mem_module_close(), 0);
            printf("%s [%d:%d] parent quit\n", TAG, getpid(), getppid());
            waitpid(child, &status, 0);
            close(sd[1]);
        }
        return 0;
    }

5.4.4.21. hb_mem_get_share_info

【Function Declaration】

int32_t hb_mem_get_share_info(int32_t fd, int32_t *share_client_cnt);

【Parameter Description】

  • [IN] fd: File descriptor associated with the buffer, value range: [0, )

  • [OUT] share_client_cnt: Number of shared clients for the buffer corresponding to the fd, value range: [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check input parameter validity as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; the given fd is abnormal and no corresponding buffer information can be found. Check logs for detailed information

【Function Description】

Obtain the number of shared clients for a buffer via its file descriptor.

【Example Code】

    int main(int argc, char *argv[])
    {
        pid_t child;
        uint64_t size = 1024 * 4; // 4k
        int64_t flags = HB_MEM_USAGE_CPU_READ_NEVER;
        hb_mem_common_buf_t in_buf = {0, };
        int32_t status;
        hb_mem_module_open();
        hb_mem_alloc_com_buf(size, flags, &in_buf);
        child = fork();
        if (child < 0) {
            printf("%s [%d:%d] Fail to create child process!\n",
            __func__, getpid(), getppid());
            exit(1);
        } else if (child == 0) {
            int32_t share_client_cnt;
            printf("%s [%d:%d] In child process.\n",
                __func__, getpid(), getppid());
            hb_mem_get_share_info(in_buf.fd, &share_client_cnt);
            hb_mem_free_buf(in_buf.fd);
            hb_mem_module_close();
            printf("%s [%d:%d] child quit\n", __func__, getpid(), getppid());
            exit(0);
        } else {
            printf( "%s [%d:%d] In parent process.\n",
                __func__, getpid(), getppid());
            hb_mem_free_buf(in_buf.fd);
            hb_mem_wait_share_status_with_vaddr((uint64_t)in_buf.virt_addr, 0,
                -1);
            printf("%s [%d:%d] parent wait successfully\n", __func__, getpid(),
                getppid());
            hb_mem_module_close();
            printf("%s [%d:%d] parent quit\n", __func__, getpid(), getppid());
            waitpid(child, &status, 0);
        }
        return 0;
    }

5.4.4.22. hb_mem_get_share_info_with_vaddr

【Function Declaration】

int32_t hb_mem_get_share_info_with_vaddr(uint64_t virt_addr, int32_t *share_client_cnt);

【Parameter Description】

  • [IN] virt_addr: Virtual address, can be an offset virtual address, value range (0, )

  • [OUT] share_client_cnt: Number of sharing clients for the buffer associated with the fd, value range [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; abnormal virtual address exists, unable to find corresponding buffer information; check logs for specific details (increase log level)

【Function Description】

Obtain the number of sharing clients for the buffer corresponding to the given virtual address.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        int32_t share_client_cnt;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_share_info_with_vaddr((uint64_t)com_buf.virt_addr,
            &share_client_cnt);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.23. hb_mem_wait_share_status

【Function Declaration】

int32_t hb_mem_wait_share_status(int32_t fd, int32_t share_client_cnt, int64_t timeout);

【Parameter Description】

  • [IN] fd: File descriptor associated with the buffer, value range [0, )

  • [IN] share_client_cnt: Target number of sharing clients, value range [0, )

  • [IN] timeout: Timeout duration (ms); <0: block and wait; =0: return immediately; >0: timeout duration

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_INVALID_FD: Invalid file handle; abnormal file descriptor exists, unable to find corresponding buffer information; check logs for specific details

  • HB_MEM_ERR_TIMEOUT: Timeout occurred; the number of sharing clients did not reach the target value within the preset time

  • HB_MEM_ERR_WAIT_SHARE_FAILURE: Failed to wait for share client status

【Function Description】

Wait until the number of sharing clients for the buffer corresponding to the fd is less than or equal to the target value.

【Example Code】

See: hb_mem_import_graph_buf

5.4.4.24. hb_mem_wait_share_status_with_vaddr

【Function Declaration】

int32_t hb_mem_wait_share_status_with_vaddr(uint64_t virt_addr, int32_t share_client_cnt, int64_t timeout);

【Parameter Description】

  • [IN] virt_addr: Virtual address, can be an offset virtual address, value range (0, )

  • [IN] share_client_cnt: Target number of sharing clients, value range [0, )

  • [IN] timeout: Timeout duration (ms); <0: block and wait; =0: return immediately; >0: timeout duration

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; abnormal virtual address exists, unable to find corresponding buffer information; check logs for specific details (increase log level)

  • HB_MEM_ERR_TIMEOUT: Timeout occurred; the number of sharing clients did not reach the target value within the preset time

  • HB_MEM_ERR_WAIT_SHARE_FAILURE: Failed to wait for share client status

【Function Description】

Wait until the number of sharing clients for the buffer corresponding to the virtual address is less than or equal to the target value.

【Example Code】

See: hb_mem_get_share_info

5.4.4.25. hb_mem_create_buf_queue

【Function Declaration】

int32_t hb_mem_create_buf_queue(hb_mem_buf_queue_t *queue);

【Parameter Description】

  • [IN] queue->count: Number of elements in the memory queue, value range (0, ]

  • [IN] queue->item_size: Size of each element in the memory queue, value range (0, ]

  • [OUT] queue->unique_id: Unique identifier of the memory queue

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; it is recommended to check ION reserved memory space and system memory usage

  • HB_MEM_ERR_QUEUE_ALREADY_EXIST: Memory queue has already been created

【Function Description】

Create a memory queue.

【Example Code】


    int main(int argc, char *argv[])
    {
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_common_buf_t out_buf = {0, };
        hb_mem_common_buf_t in_buf = {0, };
        hb_mem_buf_queue_t queue;
        #define QUEUE_ITEM_CNT 5
        int64_t timeout = 0;
        int32_t slot_array[QUEUE_ITEM_CNT];
        uint32_t i;
        memset(&queue, 0x00, sizeof(queue));
        // test after open
        hb_mem_module_open();
        queue.count = QUEUE_ITEM_CNT;
        queue.item_size = sizeof(com_buf);
        hb_mem_create_buf_queue(&queue);
        // producer do dequeue/queue
        for (i = 0; i < queue.count; i++) {
            hb_mem_dequeue_buf(&queue, &slot_array[i], &out_buf, timeout);
        }
        for (i = 0; i < queue.count; i++) {
            hb_mem_queue_buf(&queue, slot_array[i], &in_buf);
        }
        for (i = 0; i < queue.count; i++) {
            hb_mem_request_buf(&queue, &slot_array[i], &out_buf, timeout);
        }
        for (i = 0; i < queue.count; i++) {
            hb_mem_cancel_buf(&queue, slot_array[i]);
        }
        // comsumer do request/release
        for (i = 0; i < queue.count; i++) {
            hb_mem_request_buf(&queue, &slot_array[i], &out_buf, timeout);
        }
        for (i = 0; i < queue.count; i++) {
            hb_mem_release_buf(&queue, slot_array[i]);
        }
        hb_mem_destroy_buf_queue(&queue);
        hb_mem_module_close();
        return 0;
    }

5.4.4.26. hb_mem_destroy_buf_queue

【Function Declaration】

int32_t hb_mem_destroy_buf_queue(hb_mem_buf_queue_t *queue);

【Parameter Description】

  • [IN] queue: The memory queue to be destroyed

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_QUEUE_NOT_FOUND: Specified queue not found; please check if the queue parameters are correct

【Function Description】

Destroy the memory queue.

【Example Code】

See: hb_mem_create_buf_queue

5.4.4.27. hb_mem_dequeue_buf

【Function Declaration】

int32_t hb_mem_dequeue_buf(hb_mem_buf_queue_t *queue, int32_t *slot, void  *buf, int64_t timeout);

【Parameter Description】

  • [IN] queue: Memory queue, parameter should be non-null

  • [IN] timeout: Timeout duration (ms); <0: block and wait; =0: return immediately; >0: timeout duration

  • [OUT] slot: Index of the element in the memory queue, value range [0, count)

  • [OUT] buffer: Element information, size equals item_size specified when creating the memory queue, parameter should be non-null

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_QUEUE_NOT_FOUND: Specified queue not found; please check if the queue parameters are correct

  • HB_MEM_ERR_QUEUE_DESTROYED: Queue has already been destroyed; it is recommended to check code logic

  • HB_MEM_ERR_QUEUE_NO_AVAILABLE_SLOT: No available slot; it is recommended to continue dequeuing

  • HB_MEM_ERR_TIMEOUT: Dequeue timeout; possible reason is no available slot in free queue; it is recommended to continue dequeuing

【Function Description】

Producer acquires an available slot information.

【Example Code】

See: hb_mem_create_buf_queue

5.4.4.28. hb_mem_queue_buf

【Function Declaration】

int32_t hb_mem_queue_buf(hb_mem_buf_queue_t * queue, int32_t slot, const void  *buf);

【Parameter Description】

  • [IN] queue: Memory queue, parameter should be non-null

  • [IN] slot: Index of the element in the memory queue, value range [0, )

  • [IN] buf: Element information, size equals item_size specified when creating the memory queue, parameter should be non-null

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_QUEUE_NOT_FOUND: Specified queue not found; please check if the queue parameters are correct

  • HB_MEM_ERR_QUEUE_DESTROYED: Queue has already been destroyed; it is recommended to check code logic

  • HB_MEM_ERR_QUEUE_WRONG_SLOT: Invalid slot; it is recommended to check if the slot was obtained via dequeue

【Function Description】

After the producer fills in element information, enqueue it into the specified slot.

【Example Code】

See: hb_mem_create_buf_queue

5.4.4.29. hb_mem_request_buf

【Function Declaration】

int32_t hb_mem_request_buf(hb_mem_buf_queue_t *queue, int32_t *slot, void  *buf, int64_t timeout);

【Parameter Description】

  • [IN] queue: Memory queue, parameter should be non-null

  • [IN] timeout: Timeout duration (ms); <0: block and wait; =0: return immediately; >0: timeout duration

  • [OUT] slot: Index of the element in the memory queue, value range [0, count)

  • [OUT] buf: Element information, size equals item_size specified when creating the memory queue, parameter should be non-null

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_QUEUE_NOT_FOUND: Specified queue not found; please check if the queue parameters are correct

  • HB_MEM_ERR_QUEUE_DESTROYED: Queue has already been destroyed; it is recommended to check code logic

  • HB_MEM_ERR_QUEUE_NO_AVAILABLE_SLOT: No available slot; it is recommended to continue requesting

  • HB_MEM_ERR_TIMEOUT: Request timeout; possible reason is no available slot in queued queue; it is recommended to continue requesting

【Function Description】

Consumer retrieves element information enqueued by the producer from the queue.

【Example Code】

See: hb_mem_create_buf_queue

5.4.4.30. hb_mem_release_buf

【Function Declaration】

int32_t hb_mem_release_buf(hb_mem_buf_queue_t * queue, int32_t slot);

【Parameter Description】

  • [IN] queue: Memory queue, parameter should be non-null

  • [IN] slot: Index of the element in the memory queue, value range [0, count)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

  • HB_MEM_ERR_QUEUE_NOT_FOUND: Specified queue not found; please check if the queue parameters are correct

  • HB_MEM_ERR_QUEUE_DESTROYED: Queue has already been destroyed; it is recommended to check code logic

  • HB_MEM_ERR_QUEUE_WRONG_SLOT: Invalid slot; it is recommended to check if the slot was obtained via request

【Function Description】

Consumer releases the index of the used element.

【Example Code】

See: hb_mem_create_buf_queue

5.4.4.31. hb_mem_cancel_buf

【Function Declaration】

int32_t hb_mem_cancel_buf(hb_mem_buf_queue_t  *queue, int32_t slot);

【Parameter Description】

  • [IN] queue: Memory queue, parameter should be non-null

  • [IN] slot: Index of the element in the memory queue, value range [0, count)

【Return Value】

  • 0: Operation successful

  • HB_MEM_ERR_UNKNOWN: Unknown error

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters as prompted by logs

【Function Description】

The producer cancels a slot obtained via dequeue, or the consumer cancels a slot obtained via request.

【Example Code】

See: hb_mem_create_buf_queue

5.4.4.32. hb_mem_pool_create

【Function Declaration】

int32_t hb_mem_pool_create(uint64_t size, int64_t flags, hb_mem_pool_t * pool);

【Parameter Description】

  • [IN] size: Size of the memory pool, value range: (0, ]

  • [IN] flags: Buffer attributes of the memory pool, see: hbmem memory allocation attributes

  • [OUT] pool: Output memory pool

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; it is recommended to initialize the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check parameter validity as suggested by logs

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an invalid file descriptor exists and the corresponding buffer information cannot be found; refer to logs for details

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit; no more file descriptors can be allocated; consider increasing the user file descriptor limit using ulimit

【Function Description】

Create a memory pool.

【Example Code】


    int main(int argc, char *argv[])
    {
        uint64_t i, num, size = 1024 * 1024 * 4, alloc_size; // 4M
        int64_t flags;
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_common_buf_t out_buf = {0, };
        hb_mem_pool_t pool = {0, };
        hb_mem_pool_t out_pool = {0, };
        hb_mem_common_buf_t import_buf = {0, };
        char * tmp_vaddr;
        char tmp_val = 0x5a;
        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN |
            HB_MEM_USAGE_CPU_WRITE_OFTEN ;
        hb_mem_pool_create(size, flags, &pool);
        hb_mem_get_com_buf(pool.fd, &com_buf);
        num = size / pool.page_size;
        alloc_size = pool.page_size;
        for (i = 0; i < num; i++) {
            hb_mem_pool_alloc_buf(pool.fd, alloc_size, &out_buf);
            tmp_vaddr = (char *)out_buf.virt_addr;
            *tmp_vaddr = tmp_val;
            hb_mem_pool_get_info(pool.fd, &out_pool);
            hb_mem_pool_free_buf((uint64_t)out_buf.virt_addr);
        }
        hb_mem_pool_destroy(pool.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.33. hb_mem_pool_destroy

【Function Declaration】

int32_t hb_mem_pool_destroy(int32_t fd);

【Parameter Description】

  • [IN] fd: File descriptor of the memory pool, value range: [0,)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize the module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify input parameters as per log suggestions

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; invalid descriptor exists, buffer info not found; check logs for details

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has already been destroyed; review code logic

  • HB_MEM_ERR_POOL_BUSY: Memory pool cannot be destroyed; un-freed memory blocks remain in the pool; free all allocated buffers before destroying the pool

【Function Description】

Destroy a memory pool.

【Example Code】

See: hb_mem_pool_create

5.4.4.34. hb_mem_pool_alloc_buf

【Function Declaration】

int32_t hb_mem_pool_alloc_buf(int32_t fd, uint64_t size, hb_mem_common_buf_t * buf);

【Parameter Description】

  • [IN] fd: File descriptor of the memory pool, value range: [0,)

  • [IN] size: Size of the buffer, value range: [0,)

  • [OUT] buf: Common buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; check parameter validity per logs

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_POOL_NOT_FOUND: Specified memory pool not found; verify input parameters

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has been destroyed; review code logic

【Function Description】

Allocate a common buffer from the memory pool;

【Example Code】

See: hb_mem_pool_create

5.4.4.35. hb_mem_pool_free_buf

【Function Declaration】

int32_t hb_mem_pool_free_buf(uint64_t virt_addr);

【Parameter Description】

  • [IN] virt_addr: Virtual address, value range: (0,]

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify input parameters as per logs

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; no corresponding buffer found; check logs (increase log level for more details)

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has been destroyed; review code logic

  • HB_MEM_ERR_POOL_NOT_FOUND: Specified memory pool not found; verify parameters

【Function Description】

Free a buffer allocated from the memory pool.

【Example Code】

See: hb_mem_pool_create

5.4.4.36. hb_mem_pool_get_info

【Function Declaration】

int32_t hb_mem_pool_get_info(int32_t fd, hb_mem_pool_t  *pool);

【Parameter Description】

  • [IN] fd: File descriptor of the memory pool, value range: [0,)

  • [OUT] pool: Memory pool information

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify inputs per logs

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has been destroyed; review code logic

  • HB_MEM_ERR_POOL_NOT_FOUND: Specified memory pool not found; verify parameters

【Function Description】

Retrieve real-time information of the memory pool.

【Example Code】

See: hb_mem_pool_create

5.4.4.37. hb_mem_share_pool_create

【Function Declaration】

int32_t hb_mem_share_pool_create(uint32_t num, uint64_t size, int64_t flags, hb_mem_share_pool_t * pool);

【Parameter Description】

  • [IN] num: Number of buffers to allocate in the memory share pool, value range: (0, ]

  • [IN] size: Size of each buffer in the memory share pool, value range: (0, ]

  • [IN] flags: Buffer attributes of the memory share pool, see: hbmem memory allocation attributes

  • [OUT] pool: Output memory share pool

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify inputs per logs

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; invalid descriptor exists, buffer info not found; check logs

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit; cannot allocate more; increase ulimit to allow more open file descriptors

【Function Description】

Create a shared memory pool.

【Example Code】


    int main(int argc, char *argv[])
    {
        uint64_t i = 0, num = 5, size = 1024 * 1024 * 4; // 4M
        int64_t flags;
        hb_mem_common_buf_t com_buf[10] = {0, };
        hb_mem_share_pool_t out_pool = {0, };
        hb_mem_share_pool_t pool = {0, };

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN;
        hb_mem_share_pool_create(num, size, flags, &pool);
        for (i = 0; i < num; i++) {
            hb_mem_share_pool_alloc_buf(pool.fd, &com_buf[i]);
        }
        for (i = 0; i < num; i++) {
            hb_mem_share_pool_free_buf((uint64_t)(com_buf[i].virt_addr));
        }
        hb_mem_share_pool_get_info(pool.fd, &out_pool);
        hb_mem_share_pool_destroy(pool.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.38. hb_mem_share_pool_destroy

【Function Declaration】

int32_t hb_mem_share_pool_destroy(int32_t fd);

【Parameter Description】

  • [IN] fd: File descriptor of the memory share pool, value range: [0,)

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify inputs per logs

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; invalid descriptor exists, buffer info not found; check logs

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has been destroyed; review code logic

  • HB_MEM_ERR_POOL_BUSY: Memory pool cannot be destroyed; un-freed memory blocks remain; free all allocated buffers before destroying

【Function Description】

Destroy a shared memory pool.

【Example Code】

See: hb_mem_share_pool_create

5.4.4.39. hb_mem_share_pool_alloc_buf

【Function Declaration】

int32_t hb_mem_share_pool_alloc_buf(int32_t fd, hb_mem_common_buf_t * buf);

【Parameter Description】

  • [IN] fd: File descriptor of the memory share pool, value range: [0,)

  • [OUT] buf: Common buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify inputs per logs

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_POOL_NOT_FOUND: Specified memory share pool not found; verify parameters

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has been destroyed; review code logic

【Function Description】

Allocate a common buffer from the shared memory pool;

【Example Code】

See: hb_mem_share_pool_create

5.4.4.40. hb_mem_share_pool_free_buf

【Function Declaration】

int32_t hb_mem_share_pool_free_buf(uint64_t virt_addr);

【Parameter Description】

  • [IN] virt_addr: Virtual address, value range: (0,]

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify inputs per logs

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; no corresponding buffer found; check logs (increase log level for more details)

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has been destroyed; review code logic

  • HB_MEM_ERR_POOL_NOT_FOUND: Specified memory share pool not found; verify parameters

【Function Description】

Free a buffer allocated from the shared memory pool.

【Example Code】

See: hb_mem_share_pool_create

5.4.4.41. hb_mem_share_pool_get_info

【Function Declaration】

int32_t hb_mem_share_pool_get_info(int32_t fd, hb_mem_share_pool_t * pool);

【Parameter Description】

  • [IN] fd: File descriptor of the memory share pool, value range: [0,)

  • [OUT] pool: Information of the memory share pool

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not initialized; initialize first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; verify inputs per logs

  • HB_MEM_ERR_POOL_DESTROYED: Memory pool has been destroyed; review code logic

  • HB_MEM_ERR_POOL_NOT_FOUND: Specified memory share pool not found; verify parameters【Function Description】

Obtain real-time information of the shared memory pool.

【Example Code】

See: hb_mem_share_pool_create

5.4.4.42. hb_mem_get_buf_type_with_vaddr

【Function Declaration】

int32_t hb_mem_get_buf_type_with_vaddr(uint64_t virt_addr, hb_mem_buffer_type_t * type);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, range: (0, )

  • [OUT] type: Buffer type

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

【Function Description】

Obtain the type of the buffer corresponding to virt_addr.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_buffer_type_t type;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_buf_type_with_vaddr((uint64_t)com_buf.virt_addr, &type);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.43. hb_mem_get_buf_type_and_buf_with_vaddr

【Function Declaration】

int32_t hb_mem_get_buf_type_and_buf_with_vaddr(uint64_t virt_addr, hb_mem_buffer_type_t * type, hb_mem_common_buf_t * com_buf, hb_mem_graphic_buf_t * graph_buf);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, range: (0, )

  • [OUT] type: Buffer type

  • [OUT] com_buf: Common buffer

  • [OUT] graph_buf: Graphic buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

【Function Description】

Obtain the type of the buffer corresponding to virt_addr and convert it into a common buffer or graphic buffer.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_buffer_type_t type;
        hb_mem_common_buf_t com_outbuf = {0, };
        hb_mem_graphic_buf_t graph_outbuf = {0, };

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_buf_type_and_buf_with_vaddr((uint64_t)com_buf.virt_addr, &type, &com_outbuf, &graph_outbuf);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.44. hb_mem_get_buffer_process_info

【Function Declaration】

int32_t hb_mem_get_buffer_process_info(uint64_t virt_addr, int32_t * pid, int32_t num, int32_t * ret_num);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, range: (0, )

  • [IN] pid: Array of process PIDs, used to store returned process PIDs

  • [IN] num: Size of the PID array, range: (0, )

  • [OUT] ret_num: Number of processes holding the buffer corresponding to virt_addr, range: [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

【Function Description】

Obtain the process PIDs that hold the buffer corresponding to the given virtual address.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t pid[16];
        int32_t ret_num = 0;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_buffer_process_info((uint64_t)com_buf.virt_addr, pid, 16, &ret_num);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.45. hb_mem_get_buffer_process_info_with_share_id

【Function Declaration】

int32_t hb_mem_get_buffer_process_info_with_share_id(int32_t share_id, int32_t * pid, int32_t num, int32_t * ret_num);

【Parameter Description】

  • [IN] share_id: Range: (0, )

  • [IN] pid: Array of process PIDs, used to store returned process PIDs

  • [IN] num: Size of the PID array, range: (0, )

  • [OUT] ret_num: Number of processes holding the buffer corresponding to share_id, range: [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

【Function Description】

Obtain the process PIDs that hold the buffer corresponding to the given share_id.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t pid[16];
        int32_t ret_num = 0;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_buffer_process_info_with_share_id(com_buf.share_id, pid, 16, &ret_num);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.46. hb_mem_get_consume_info

【Function Declaration】

int32_t hb_mem_get_consume_info(int32_t fd, int32_t * share_consume_cnt);

【Parameter Description】

  • [IN] fd: Buffer file descriptor, range: (0, )

  • [OUT] share_consume_cnt: Buffer consume count, range: [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; the given file descriptor is abnormal and corresponding buffer information cannot be found. Check logs for detailed information

【Function Description】

Obtain the consume count of the buffer corresponding to the given fd.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t share_consume_cnt;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_consume_info(com_buf.fd, &share_consume_cnt);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.47. hb_mem_get_consume_info_with_vaddr

【Function Declaration】

int32_t hb_mem_get_consume_info_with_vaddr(uint64_t virt_addr, int32_t * share_consume_cnt);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, range: (0, )

  • [OUT] share_consume_cnt: Buffer consume count, range: [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

【Function Description】

Obtain the consume count of the buffer corresponding to the given virt_addr.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t share_consume_cnt;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_get_consume_info_with_vaddr((uint64_t)com_buf.virt_addr, &share_consume_cnt);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.48. hb_mem_wait_consume_status

【Function Declaration】

int32_t hb_mem_wait_consume_status(int32_t fd, int32_t share_consume_cnt, int64_t timeout);

【Parameter Description】

  • [IN] fd: Buffer file descriptor, range: (0, )

  • [IN] share_consume_cnt: Buffer consume count, range: [0, )

  • [IN] timeout: Timeout duration, range: [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; the given file descriptor is abnormal and corresponding buffer information cannot be found. Check logs for detailed information

  • HB_MEM_ERR_WAIT_SHARE_FAILURE: Failed to wait for share client status

【Function Description】

Wait until the consume count of the buffer corresponding to fd becomes share_consume_cnt, with a timeout of timeout.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t share_consume_cnt = 0;
        int32_t timeout = 100;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_wait_consume_status(com_buf.fd, share_consume_cnt, timeout);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.49. hb_mem_wait_consume_status_with_vaddr

【Function Declaration】

int32_t hb_mem_wait_consume_status_with_vaddr(uint64_t virt_addr, int32_t share_consume_cnt, int64_t timeout);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, range: (0, )

  • [IN] share_consume_cnt: Consume count, range: [0, )

  • [IN] timeout: Timeout duration, range: [0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as suggested by log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is abnormal and corresponding buffer information cannot be found. Check logs for detailed information (increase log level)

  • HB_MEM_ERR_WAIT_SHARE_FAILURE: Failed to wait for share client status

【Function Description】

Wait until the consume count of the buffer corresponding to virt_addr becomes share_consume_cnt, with a timeout of timeout.

【Example Code】


    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t share_consume_cnt = 0;
        int32_t timeout = 100;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_wait_consume_status_with_vaddr((uint64_t)com_buf.virt_addr, share_consume_cnt, timeout);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.50. hb_mem_inc_com_buf_consume_cnt

【Function Declaration】

int32_t hb_mem_inc_com_buf_consume_cnt(hb_mem_common_buf_t * buf);

【Parameter Description】

  • [IN] buf: Common buffer【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

【Function Description】

Increment the consume count of the corresponding common buffer

【Example Code】

    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t timeout = 100;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_inc_com_buf_consume_cnt(&com_buf);
        hb_mem_dec_consume_cnt(com_buf.fd);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.51. hb_mem_inc_graph_buf_consume_cnt

【Function Declaration】

int32_t hb_mem_inc_graph_buf_consume_cnt(hb_mem_graphic_buf_t * buf);

【Parameter Description】

  • [IN] buf: graphic buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

【Function Description】

Increment the consume count of the corresponding graphic buffer

【Example Code】

    int main(int argc, char *argv[])
    {
        int64_t flags;
        hb_mem_graphic_buf_t graph_buf = {0, };
        int32_t w = 1920, h = 1080, format = MEM_PIX_FMT_NV12, stride = 0, vstride = 0;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_graph_buf(w, h, format, flags, stride, vstride,
            &graph_buf);
        hb_mem_inc_graph_buf_consume_cnt(&graph_buf);
        hb_mem_dec_consume_cnt(graph_buf.fd[0]);
        hb_mem_free_buf(graph_buf.fd[0]);
        hb_mem_module_close();
        return 0;
    }

5.4.4.52. hb_mem_dec_consume_cnt

【Function Declaration】

int32_t hb_mem_dec_consume_cnt(int32_t fd);

【Parameter Description】

  • [IN] fd: buffer fd, value range (0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an abnormal file descriptor exists and the corresponding buffer information cannot be found. Check the logs for detailed information

【Function Description】

Decrement the consume count of the corresponding buffer via fd

【Example Code】

Refer to: hb_mem_inc_com_buf_consume_cnt

5.4.4.53. hb_mem_dec_consume_cnt_with_vaddr

【Function Declaration】

int32_t hb_mem_dec_consume_cnt_with_vaddr(uint64_t virt_addr);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, value range (0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; an abnormal virtual address exists and the corresponding buffer information cannot be found. Check the logs for detailed information (increase log level)

【Function Description】

Decrement the consume count of the corresponding buffer via virtual address

【Example Code】

    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t com_buf = {0, };
        int32_t timeout = 100;

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN
            | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &com_buf);
        hb_mem_inc_com_buf_consume_cnt(&com_buf);
        hb_mem_dec_consume_cnt_with_vaddr((uint64_t)com_buf.virt_addr);
        hb_mem_free_buf(com_buf.fd);
        hb_mem_module_close();
        return 0;
    }

5.4.4.54. hb_mem_import_com_buf_with_paddr

【Function Declaration】

int32_t hb_mem_import_com_buf_with_paddr(uint64_t phys_addr,
			uint64_t size, int64_t flags, hb_mem_common_buf_t * buf);

【Parameter Description】

  • [IN] phys_addr: Address of the buffer to be imported, may include offset, value range (0, )

  • [IN] size: Size of the imported buffer, must be aligned to PAGE_SIZE, value range (0, )

  • [IN] flags: Flags of the imported buffer, value range [0, )

  • [OUT] buf: Returned common buffer after import

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an abnormal file descriptor exists and the corresponding buffer information cannot be found. Check logs for details

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit; unable to allocate more file descriptors. Use ulimit to increase the user limit on open file descriptors

【Function Description】

Share memory via physical address; can only be used for memory allocated by hbmem.

Since the upper layer cannot directly obtain the physical address, the example code only shows the interface usage. Memory allocated by hbmem should use the interface hb_mem_import_com_buf. Shared memory imported via hb_mem_import_com_buf_with_paddr must be released using hb_mem_free_buf_with_vaddr.

【Example Code】

    int main(int argc, char *argv[])
    {
        uint32_t size = 1024 * 8, out_size; // 8k
        uint64_t flags;
        const char * lable = NULL;
        hbmem_addr_t start, vaddr;
        uint64_t paddr = 0;
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_common_buf_t out_buf = {0, };

        flags = BACKEND_TYPE(BACKEND_ION_CMA) | MEM_CACHEABLE;
        vaddr = hbmem_alloc(size, flags, lable);
        hbmem_info(vaddr, &start, &out_size);
        paddr = hbmem_phyaddr(vaddr);
        hb_mem_get_com_buf_with_vaddr(vaddr, &com_buf);
        hb_mem_import_com_buf_with_paddr(paddr, com_buf.size, flags, &out_buf);
        hb_mem_free_buf_with_vaddr((uint64_t)out_buf.virt_addr);
        hbmem_free(vaddr);
        return 0;
    }

5.4.4.55. hb_mem_dma_copy

【Function Declaration】

int32_t hb_mem_dma_copy(uint64_t dst_vaddr, uint64_t src_vaddr, uint64_t size);

【Parameter Description】

  • [IN] dst_vaddr: Destination virtual address, may be offset, value range (0, )

  • [IN] src_vaddr: Source virtual address, may be offset, value range (0, )

  • [IN] size: Size of memory for DMA, value range (0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; an abnormal virtual address exists and the corresponding buffer information cannot be found. Check logs for details (increase log level)

【Function Description】

Copy data from source address to destination address

【Example Code】

    int main(int argc, char *argv[])
    {
        int64_t flags;
        uint64_t size = 1024 * 4; // 4k
        hb_mem_common_buf_t dst_com_buf = {0, };
        hb_mem_common_buf_t src_com_buf = {0, };

        hb_mem_module_open();
        flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN | HB_MEM_USAGE_CACHED;
        hb_mem_alloc_com_buf(size, flags, &dst_com_buf);
        hb_mem_alloc_com_buf(size, flags, &src_com_buf);
        hb_mem_dma_copy((uint64_t)dst_com_buf.virt_addr, (uint64_t)src_com_buf.virt_addr, size);
        hb_mem_free_buf(dst_com_buf.fd);
        hb_mem_free_buf(src_com_buf.fd);
        hb_mem_module_close();

        return 0;
    }

5.4.4.56. hb_mem_alloc_graph_buf_group

【Function Declaration】

int32_t hb_mem_alloc_graph_buf_group(int32_t * w, int32_t * h, int32_t * format, int64_t * flags, int32_t * stride,
			int32_t * vstride, hb_mem_graphic_buf_group_t * buf_group, uint32_t bitmap);

【Parameter Description】

  • [IN] w: Array of image widths

  • [IN] h: Array of image heights

  • [IN] format: Array of image formats, see: hbmem image format

  • [IN] flags: Array of buffer attributes, see: hbmem memory allocation attributes

  • [IN] stride: Array of horizontal strides

  • [IN] vstride: Array of vertical strides

  • [OUT] buf_group: Graphic buffer group

  • [IN] bitmap: Bitmap indicating valid graphic buffers

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; an abnormal file descriptor exists and the corresponding buffer information cannot be found. Check logs for details

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit; unable to allocate more file descriptors. Use ulimit to increase the user limit on open file descriptors

【Function Description】

Allocate a group of graphic buffers

【Example Code】

    int main(int argc, char *argv[])
    {
        hb_mem_graphic_buf_group_t buf_group = {0, };
        hb_mem_graphic_buf_group_t info = {0, };
        hb_mem_graphic_buf_group_t out_buf = {0, };
        hb_mem_graphic_buf_t graph_buf = {0, };
        hb_mem_common_buf_t com_buf = {0, };
        hb_mem_buffer_type_t *type;
        uint32_t bitmap = 0xFF;
        int32_t w[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t h[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t format[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t stride[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t vstride[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int64_t flags[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t i;

        // open module
        hb_mem_module_open();

        // test DMA heap type
        for (i = 0; i < HB_MEM_MAXIMUM_GRAPH_BUF; i++) {
            if (bitmap & (1u << i)) {
                w[i] = 1280;
                h[i] = 720;
                format[i] = MEM_PIX_FMT_NV12;
                flags[i] = HB_MEM_USAGE_PRIV_HEAP_DMA | HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN;
                stride[i] = 0;
                vstride[i] = 0;
            }
        }

        hb_mem_alloc_graph_buf_group(w, h, format, flags, stride, vstride, &buf_group, bitmap);
        hb_mem_get_graph_buf_group(buf_group.graph_group[0].fd[0], &info);
        hb_mem_get_graph_buf_group_with_vaddr((uint64_t)buf_group.graph_group[0].virt_addr[0], &info);
        hb_mem_get_buf_and_type_with_vaddr((uint64_t)buf_group.graph_group[0].virt_addr[0], type, &com_buf, &graph_buf, &info);
        hb_mem_import_graph_buf_group(&buf_group, &out_buf);
        hb_mem_inc_graph_buf_group_consume_cnt(&buf_group);
        hb_mem_dec_consume_cnt(buf_group.graph_group[0].fd[0]);
        hb_mem_free_buf(buf_group.graph_group[0].fd[0]);
        hb_mem_free_buf(out_buf.graph_group[0].fd[0]);
        hb_mem_module_close();

        return 0;
    }

5.4.4.57. hb_mem_import_graph_buf_group

【Function Declaration】

int32_t hb_mem_import_graph_buf_group(hb_mem_graphic_buf_group_t * in_group, hb_mem_graphic_buf_group_t * out_group);

【Parameter Description】

  • [IN] in_group: Input graphic buffer group

  • [OUT] out_group: Output shared graphic buffer group

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; it is recommended to check the validity of input parameters according to log prompts

  • HB_MEM_ERR_INSUFFICIENT_MEM: Insufficient memory; check ION reserved memory and system memory usage

  • HB_MEM_ERR_TOO_MANY_FD: Number of file descriptors exceeds limit; unable to allocate more file descriptors. Use ulimit to increase the user limit on open file descriptors

【Function Description】

Share a group of graphic buffers

【Example Code】

    int main(int argc, char *argv[])
    {
        int32_t ret;
        pid_t child;
        int32_t input_format = MEM_PIX_FMT_NV12, i;
        hb_mem_graphic_buf_group_t in_info = {0, };
        int32_t sd[2];
        int32_t status, m;
        uint32_t bitmap = 0xFF;
        int32_t w[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t h[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t format[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t stride[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int32_t vstride[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };
        int64_t flags[HB_MEM_MAXIMUM_GRAPH_BUF] = {0, };

        for (m = 0; m < HB_MEM_MAXIMUM_GRAPH_BUF; m++) {
            if (bitmap & (1u << m)) {
                w[m] = 1280;
                h[m] = 720;
                flags[m] = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN;
                stride[m] = 0;
                vstride[m] = 0;
            }
        }

        printf("%s [%d:%d] Start Test format %d scenario 1.\n", TAG, getpid(), getppid(), input_format);
        socketpair(AF_UNIX, SOCK_STREAM, 0, sd);
        child = fork();
        if (child < 0) {
            printf("%s [%d:%d] Fail to create child process!\n",
                TAG, getpid(), getppid());
            exit(1);
        } else if (child == 0) {
            hb_mem_graphic_buf_group_t recv_buf = {0, };
            hb_mem_graphic_buf_group_t out_buf = {0, };
            struct msghdr msg;
            struct iovec io;

            printf("%s [%d:%d] In child process.\n",
                TAG, getpid(), getppid());

            close(sd[1]);
            memset(&msg, 0x00, sizeof(msg));
            msg.msg_name = NULL;
            msg.msg_namelen = 0;
            msg.msg_iov = &io;
            msg.msg_iovlen = 1;

            io.iov_base = &recv_buf;
            io.iov_len = sizeof(recv_buf);

            ret = recvmsg(sd[0], &msg, 0);
            printf("%s [%d:%d] child recv msg 1\n", TAG, getpid(), getppid());

            hb_mem_module_open();
            hb_mem_import_graph_buf_group(&recv_buf, &out_buf);

            ret = sendmsg(sd[0], &msg, 0);
            printf("%s [%d:%d] child send msg 2\n", TAG, getpid(), getppid());

            ret = recvmsg(sd[0], &msg, 0);
            printf("%s [%d:%d] child recv msg 3\n", TAG, getpid(), getppid());

            hb_mem_free_buf(out_buf.graph_group[0].fd[0]);
            ASSERT_EQ(hb_mem_module_close(), 0);
            printf("%s [%d:%d] child quit\n", TAG, getpid(), getppid());
            close(sd[0]);

            exit(0);
        } else {
            hb_mem_graphic_buf_group_t in_buf = {0, };
            hb_mem_graphic_buf_group_t recv_buf = {0, };
            struct msghdr msg;
            struct iovec io;

            printf( "%s [%d:%d] In parent process.\n",
                TAG, getpid(), getppid());
            close(sd[0]);

            hb_mem_module_open();
            for (m = 0; m < HB_MEM_MAXIMUM_GRAPH_BUF; m++) {
                if (bitmap & (1u << m)) {
                    format[m] = input_format;
                }
            }
            hb_mem_alloc_graph_buf_group(w, h, format, flags, stride, vstride, &in_buf, bitmap);
            hb_mem_get_graph_buf_group(in_buf.graph_group[0].fd[0], &in_info);

            memset(&msg, 0x00, sizeof(msg));
            msg.msg_name = NULL;
            msg.msg_namelen = 0;
            msg.msg_iov = &io;
            msg.msg_iovlen = 1;

            io.iov_base = &in_buf;
            io.iov_len = sizeof(in_buf);

            ret = sendmsg(sd[1], &msg, 0);
            printf("%s [%d:%d] parent send msg 1\n", TAG, getpid(), getppid());

            io.iov_base = &recv_buf;
            io.iov_len = sizeof(recv_buf);
            ret = recvmsg(sd[1], &msg, 0);
            printf("%s [%d:%d] parent recv msg 2\n", TAG, getpid(), getppid());

            hb_mem_free_buf(in_buf.graph_group[0].fd[0]);

            io.iov_base = &in_buf;
            io.iov_len = sizeof(in_buf);
            ret = sendmsg(sd[1], &msg, 0);
            printf("%s [%d:%d] parent send msg 3\n", TAG, getpid(), getppid());

            hb_mem_module_close();
            printf("%s [%d:%d] parent quit\n", TAG, getpid(), getppid());
            waitpid(child, &status, 0);
            close(sd[1]);
        }

        return 0;
    }

5.4.4.58. hb_mem_get_graph_buf_group

【Function Declaration】

int32_t hb_mem_get_graph_buf_group(int32_t fd, hb_mem_graphic_buf_group_t * buf_group);

【Parameter Description】

  • [IN] fd: Buffer file descriptor; any valid fd within the graphic buffer group, range: (0, ∞)

  • [OUT] buf_group: The graphic buffer group obtained via the fd

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as prompted in logs

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; the given fd is invalid or no corresponding buffer information can be found; check logs for details

【Function Description】

Obtain the graphic buffer group via a file descriptor (any valid fd within the graphic buffer group).

【Example Code】

See: hb_mem_alloc_graph_buf_group

5.4.4.59. hb_mem_get_graph_buf_group_with_vaddr

【Function Declaration】

int32_t hb_mem_get_graph_buf_group_with_vaddr(uint64_t virt_addr, hb_mem_graphic_buf_group_t * buf_group);

【Parameter Description】

  • [IN] virt_addr: Virtual address; any valid virtual address within the graphic buffer group, including offset virtual addresses, range: (0, ∞)

  • [OUT] buf_group: The graphic buffer group obtained via the virtual address

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as prompted in logs

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is invalid or no corresponding buffer information can be found; check logs for details (increase log level if necessary)

【Function Description】

Obtain the graphic buffer group via a virtual address (any valid virtual address within the graphic buffer group).

【Example Code】

See: hb_mem_alloc_graph_buf_group

5.4.4.60. hb_mem_inc_graph_buf_group_consume_cnt

【Function Declaration】

int32_t hb_mem_inc_graph_buf_group_consume_cnt(hb_mem_graphic_buf_group_t * buf_group);

【Parameter Description】

  • [IN] buf_group: The graphic buffer group

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as prompted in logs

【Function Description】

Increment the consume count for all buffers within the graphic buffer group.

5.4.4.61. hb_mem_get_buf_and_type_with_vaddr

【Function Declaration】

int32_t hb_mem_get_buf_and_type_with_vaddr(uint64_t virt_addr, hb_mem_buffer_type_t * type, hb_mem_common_buf_t * com_buf,
hb_mem_graphic_buf_t * graph_buf, hb_mem_graphic_buf_group_t * graph_group);

【Parameter Description】

  • [IN] virt_addr: Virtual address; can be an offset virtual address, range: (0, ∞)

  • [OUT] type: Buffer type

  • [OUT] com_buf: Common buffer

  • [OUT] graph_buf: Graphic buffer

  • [OUT] graph_group: Graphic buffer group

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not opened; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check the validity of input parameters as prompted in logs

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is invalid or no corresponding buffer information can be found; check logs for details (increase log level if necessary)

【Function Description】

Obtain the buffer type and corresponding buffer via a virtual address. This interface can retrieve a graphic buffer group.

【Example Code】

See: hb_mem_alloc_graph_buf_group

5.4.4.62. hbmem_alloc

【Function Declaration】

hbmem_addr_t hbmem_alloc(uint32_t size, uint64_t flag, const char * label);

【Parameter Description】

  • [IN] size: Size of memory space to allocate

  • [IN] flag: Allocation flag; refer to detailed flag description below

  • [IN] label: String to identify the allocated memory space (used for future debugging features, only first <10 characters are used); set to NULL if no label is needed

【Return Value】

  • 0: Allocation failed

  • Non-zero: Allocation succeeded; the returned hbmem_addr_t value can be used directly as a virtual address by the user

【Function Description】

Allocate hobot memory, returning physically contiguous memory space (compatibility interface; not recommended for new development)

【Example Code】

    int main(int argc, char *argv[])
    {
        uint32_t major, minor, patch;
        uint32_t size = 1024 * 8, i, out_size, size2; // 8k
        uint64_t flags = BACKEND_ION_CMA;
        const char * label = NULL;
        hbmem_addr_t dst_vaddr, src_vaddr, start, vaddr, vaddr2, vaddr_out;
        uint64_t paddr = 0;
        uint8_t *tmp_vaddr;
        uint8_t value = 55;
        int32_t share_id = 1;
        hb_mem_common_buf_t com_buf = {0, };
        flags = BACKEND_TYPE(BACKEND_ION_CMA) | MEM_CACHEABLE;
        hbmem_version(&major, &minor, &patch);
        vaddr = hbmem_alloc(size, flags, label);
        hbmem_info(vaddr, &start, &out_size);
        paddr = hbmem_phyaddr(vaddr);
        vaddr_out = hbmem_virtaddr(vaddr);
        hbmem_is_cacheable(vaddr);
        hb_mem_get_com_buf_with_vaddr(vaddr, &com_buf);
        paddr = com_buf.phys_addr;
        size2 = com_buf.size;
        share_id = com_buf.share_id;
        vaddr2 = hbmem_mmap_with_share_id(paddr, size2, flags, share_id);
        share_id = 0;
        hbmem_get_share_id(vaddr2, &share_id);
        hbmem_munmap(vaddr2);
        vaddr2 = hbmem_mmap(paddr, size, flags);
        hbmem_munmap(vaddr2);
        hbmem_free(vaddr);
        dst_vaddr = hbmem_alloc(size, flags, label);
        src_vaddr = hbmem_alloc(size, flags, label);
        tmp_vaddr = (uint8_t *)src_vaddr;
        for (i = 0; i < size; i++) {
            tmp_vaddr[i] = value;
        }
        hbmem_cache_clean(src_vaddr, size);
        hbmem_dmacpy(dst_vaddr, src_vaddr, size);
        hbmem_cache_invalid(dst_vaddr, size);
        hbmem_free(src_vaddr);
        hbmem_free(dst_vaddr);
        return 0;
    }

5.4.4.63. hbmem_free

【Function Declaration】

void hbmem_free(hbmem_addr_t addr);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t returned by hbmem_alloc

【Return Value】

  • None

【Function Description】

Free memory space allocated by hbmem_alloc (compatibility interface; not recommended for new development)

【Example Code】

See: hbmem_alloc

5.4.4.64. hbmem_mmap

【Function Declaration】

hbmem_addr_t hbmem_mmap(uint64_t phyaddr, uint32_t size, uint64_t flag);

【Parameter Description】

  • [IN] phyaddr: Starting physical address of valid memory space (must be PAGE_SIZE aligned)

  • [IN] size: Size of memory space to map

  • [IN] flag: Allocation flag

【Return Value】

  • 0: Mapping failed

  • Non-zero: Mapping succeeded; the returned hbmem_addr_t value can be used directly as a virtual address by the user

【Function Description】

Map a known physical address memory space into hbmem virtual address space; only memory located in ION heap can use this interface (compatibility interface; not recommended for new development)

【Example Code】

See: hbmem_alloc

5.4.4.65. hbmem_munmap

【Function Declaration】

void hbmem_munmap(hbmem_addr_t addr);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t returned by hbmem_mmap

【Return Value】

  • None

【Function Description】

Unmap memory space previously mapped by hbmem_mmap (compatibility interface; not recommended for new development)

【Example Code】

See: hbmem_alloc

5.4.4.66. hbmem_phyaddr

【Function Declaration】

uint64_t hbmem_phyaddr(hbmem_addr_t addr);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t returned by hbmem_mmap

【Return Value】

  • 0: Invalid physical address, indicating the provided hbmem_addr_t is not from valid hbmem space

  • Non-zero: Physical address

【Function Description】

Obtain the actual DDR physical address corresponding to a hbmem virtual address (compatibility interface; not recommended for new development)

【Example Code】

See: hbmem_alloc

5.4.4.67. hbmem_dmacpy

【Function Declaration】

int32_t hbmem_dmacpy(hbmem_addr_t dst, hbmem_addr_t src, uint32_t size);

【Parameter Description】

  • [IN] dst: Starting address of the destination hbmem space

  • [IN] src: Starting address of the source hbmem space

  • [IN] size: Size of memory to copy

【Return Value】

  • 0: Transfer succeeded

  • <0: Failed

【Function Description】

Use system DMA to copy data between two hbmem memory spaces (compatibility interface; not recommended for new development)

【Example Code】

See: hbmem_alloc

5.4.4.68. hbmem_virtaddr

【Function Declaration】

uint64_t hbmem_virtaddr(hbmem_addr_t addr);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t

【Return Value】

  • 0: Invalid virtual address, indicating that the input hbmem_addr_t is not a valid address within hbmem

  • Non-zero: Virtual address

【Function Description】

Obtain the actual virtual address corresponding to the hbmem memory space address (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.69. hbmem_info

【Function Declaration】

int32_t hbmem_info(hbmem_addr_t addr, hbmem_addr_t *start, uint32_t
*size);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t

  • [IN] start: Pointer to store the starting address of the buffer within the associated hbmem memory space

  • [IN] size: Pointer to store the size of the buffer within the associated hbmem memory space

【Return Value】

  • <0: The hbmem_addr_t does not belong to any hbmem memory space; in this case, the start/size parameters will not be assigned

  • =0: Type of hbmem memory space to which the hbmem_addr_t belongs.
    0: UNKNOWN (unknown)
    1: ALLOCED (space allocated via hbmem_alloc)
    2: MAPPED (space mapped via hbmem_mmap)

【Function Description】

Retrieve information about the given hbmem_addr_t address value (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.70. hbmem_version

【Function Declaration】

int32_t hbmem_version(uint32_t *major, uint32_t *minor, uint32_t *patch);

【Parameter Description】

  • [OUT] major: Major version number

  • [OUT] minor: Minor version number

  • [OUT] patch: Patch version number

【Return Value】

  • 0: Successfully retrieved version

  • <0: Parameter error, one or more input parameters are NULL

【Function Description】

Retrieve the version of the currently used hbmem library (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.71. hbmem_is_cacheable

【Function Declaration】

int32_t hbmem_is_cacheable(hbmem_addr_t addr);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t

【Return Value】

  • <0: Invalid hbmem_addr_t

  • 0: Uncacheable type

  • 0: Cacheable type

【Function Description】

Retrieve the cache type of the hbmem space corresponding to the hbmem_addr_t (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.72. hbmem_cache_invalid

【Function Declaration】

void hbmem_cache_invalid(hbmem_addr_t addr, uint32_t size);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t

  • [IN] size: Size of the memory region to operate on

【Return Value】

None

【Function Description】

Perform a cache invalidation operation on the specified hbmem memory space (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.73. hbmem_cache_clean

【Function Declaration】

void hbmem_cache_clean(hbmem_addr_t addr, uint32_t size);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t

  • [IN] size: Size of the memory region to operate on

【Return Value】

None

【Function Description】

Perform a cache clean operation on the specified hbmem memory space (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.74. hbmem_mmap_with_share_id

【Function Declaration】

hbmem_addr_t hbmem_mmap_with_share_id(uint64_t phyaddr, uint32_t
size,

uint64_t flag, int32_t share_id);

【Parameter Description】

  • [IN] phyaddr: Starting address of a valid physical memory region (must be 0x1000 aligned)

  • [IN] size: Size of memory region to be mapped

  • [IN] flag: Allocation flag

  • [IN] share_id: Globally unique shared ID

【Return Value】

  • 0: Mapping failed

  • Non-zero: Mapping succeeded; the returned hbmem_addr_t value can be directly used as a virtual address by the user

【Function Description】

Map a known physical memory region into hbmem using a share_id (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.75. hbmem_get_share_id

【Function Declaration】

int32_t hbmem_get_share_id(hbmem_addr_t addr, int32_t *share_id);

【Parameter Description】

  • [IN] addr: Valid hbmem_addr_t

  • [OUT] share_id: Share ID corresponding to the address

【Return Value】

  • 0: Successfully retrieved the share_id associated with the virtual address

  • <0: Virtual address not found

【Function Description】

Retrieve the share_id associated with a virtual address (compatibility interface; not recommended for use in new feature development)

【Example Code】

See: hbmem_alloc

5.4.4.76. hb_mem_inc_user_consume_cnt

【Function Declaration】

int32_t hb_mem_inc_user_consume_cnt(int32_t hb_fd);

【Parameter Description】

  • [IN] hb_fd: File descriptor associated with the buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check parameter validity according to log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; the given file descriptor is invalid and the corresponding buffer information cannot be found; refer to logs for details

【Function Description】

Increase the user-space reference count of a specified buffer via file descriptor. Not applicable to graphic buffer group, pool buffer, or share pool buffer.

【Example Code】

    int main(int argc, char *argv[])
    {
        int32_t ret = 0;
        uint64_t size = 4 * 1024 * 1024;
        int64_t flags = HB_MEM_USAGE_CPU_READ_OFTEN | HB_MEM_USAGE_CPU_WRITE_OFTEN | HB_MEM_USAGE_CACHED;
        hb_mem_common_buf_t com_buf = {0, };

        ret = hb_mem_module_open();
        if (ret != 0) {
            printf("hb_mem_module_open failed\n");
            return ret;
        }

        ret = hb_mem_alloc_com_buf(size, flags, &com_buf);
        if (ret != 0) {
            printf("hb_mem_alloc_com_buf failed\n");
            (void)hb_mem_module_close();
            return ret;
        }
        printf("alloc com buf, share_id: %d\n", com_buf.share_id);
        do_sys_command(flags & HB_MEM_USAGE_PRIV_MASK);

        //inc/dec user consume with fd
        ret = hb_mem_inc_user_consume_cnt(com_buf.fd);
        if (ret != 0) {
            printf("hb_mem_inc_user_consume_cnt failed\n");
            (void)hb_mem_module_close();
            return ret;
        }

        ret = hb_mem_dec_user_consume_cnt(com_buf.fd);
        if (ret != 0) {
            printf("hb_mem_dec_user_consume_cnt failed\n");
            (void)hb_mem_module_close();
            return ret;
        }

        //inc/dec user consume with vaddr
        ret = hb_mem_inc_user_consume_cnt_with_vaddr((uint64_t)com_buf.virt_addr);
        if (ret != 0) {
            printf("hb_mem_inc_user_consume_cnt_with_vaddr failed\n");
            (void)hb_mem_module_close();
            return ret;
        }

        ret = hb_mem_dec_user_consume_cnt_with_vaddr((uint64_t)com_buf.virt_addr);
        if (ret != 0) {
            printf("hb_mem_dec_user_consume_cnt_with_vaddr failed\n");
            (void)hb_mem_module_close();
            return ret;
        }

        ret = hb_mem_free_buf(com_buf.fd);
        if (ret != 0) {
            printf("hb_mem_free_buf failed\n");
            (void)hb_mem_module_close();
            return ret;
        }
        printf("free com buf\n");
        do_sys_command(flags & HB_MEM_USAGE_PRIV_MASK);
        ret = hb_mem_module_close();
        if (ret != 0) {
            printf("hb_mem_module_close failed\n");
            return ret;
        }

        return 0;
    }

5.4.4.77. hb_mem_dec_user_consume_cnt

【Function Declaration】

int32_t hb_mem_dec_user_consume_cnt(int32_t hb_fd);

【Parameter Description】

  • [IN] hb_fd: File descriptor associated with the buffer

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check parameter validity according to log messages

  • HB_MEM_ERR_INVALID_FD: Invalid file descriptor; the given file descriptor is invalid and the corresponding buffer information cannot be found; refer to logs for details

【Function Description】

Decrease the user-space reference count of a specified buffer via file descriptor. Not applicable to graphic buffer group, pool buffer, or share pool buffer.

【Example Code】

See: hb_mem_inc_user_consume_cnt

5.4.4.78. hb_mem_inc_user_consume_cnt_with_vaddr

【Function Declaration】

int32_t hb_mem_inc_user_consume_cnt_with_vaddr(uint64_t virt_addr);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, value range (0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check parameter validity according to log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is invalid and the corresponding buffer information cannot be found; refer to logs for details (consider increasing log level)

【Function Description】

Increase the user-space reference count of a specified buffer via virtual address. Not applicable to graphic buffer group, pool buffer, or share pool buffer.

【Example Code】

See: hb_mem_inc_user_consume_cnt

5.4.4.79. hb_mem_dec_user_consume_cnt_with_vaddr

【Function Declaration】

int32_t hb_mem_dec_user_consume_cnt_with_vaddr(uint64_t virt_addr);

【Parameter Description】

  • [IN] virt_addr: Virtual address, which can be an offset virtual address, value range (0, )

【Return Value】

  • 0: Success

  • HB_MEM_ERR_MODULE_NOT_FOUND: Memory module is not open; it is recommended to open the memory module first

  • HB_MEM_ERR_INVALID_PARAMS: Invalid parameters; please check parameter validity according to log messages

  • HB_MEM_ERR_INVALID_VADDR: Invalid virtual address; the given virtual address is invalid and the corresponding buffer information cannot be found; refer to logs for details (consider increasing log level)

【Function Description】

Decrease the user-space reference count of the specified buffer via virtual address. Graphic buffer groups, pool buffers, and shared pool buffers are not supported.

【Example Code】

See: hb_mem_inc_user_consume_cnt

5.4.5. Data Structures

5.4.5.1. hb_mem_common_buf_t

    /**
    * @struct hb_mem_common_buf_t
    * Define the descriptor of common buffer.
    */
    typedef struct hb_mem_common_buf_t {
        int32_t fd;			/**< File descriptors of the buffer.*/
        int32_t share_id;	/**< Share id of the buffer.*/
        int64_t flags;		/**< Buffer flags for allocation */
        uint64_t size;		/**< Total Buffer size specified by user during allocation.*/
        uint8_t *virt_addr;	/**< Buffer starting virtual address.*/
        /**
        * - Note: It's not recommended to use or transfer the physical address directly.
        *         There is no memory object to keep track of the physical address.
        */
        uint64_t phys_addr;	/**< Buffer starting physical address.*/
        uint64_t offset;	/**< Buffer offset.*/
    } hb_mem_common_buf_t;

5.4.5.2. hb_mem_graphic_buf_t

    /**
    * @struct hb_mem_graphic_buf_t
    * Define the descriptor of graphic buffer.
    */
    typedef struct hb_mem_graphic_buf_t {
        //#define MAX_GRAPHIC_BUF_COMP 3
        int32_t fd[MAX_GRAPHIC_BUF_COMP];	/**< File descriptors of the buffer for each component.*/
        /**
        * Values [1, MAX_GRAPHIC_BUF_COMP]
        */
        int32_t plane_cnt;					/**< Plane count of this graphic buffer*/
        int32_t format;						/**< Buffer format for allocation.@mem_pixel_format_t*/
        int32_t width;						/**< Buffer width.*/
        int32_t height;						/**< Buffer height.*/
        int32_t stride;						/**< Buffer horizontal stride.*/
        int32_t vstride;					/**< Buffer vertical stride.*/
        int32_t is_contig;					/**< Buffer physical memory is contiguous.@mem_usage_t*/
        int32_t share_id[MAX_GRAPHIC_BUF_COMP];	/**< Share id of the buffer.*/
        int64_t flags;						/**< Buffer flags for allocation.@mem_usage_t*/
        uint64_t size[MAX_GRAPHIC_BUF_COMP];	/**< Total Buffer size for each component.*/
        uint8_t *virt_addr[MAX_GRAPHIC_BUF_COMP];	/**< Buffer virtual address for each component.*/
        /**
        * - Note: It's not recommended to use or transfer the physical address directly.
        *         There is no memory object to keep track of the physical address.
        */
        uint64_t phys_addr[MAX_GRAPHIC_BUF_COMP];	/**< Buffer physical address for each component.*/
        uint64_t offset[MAX_GRAPHIC_BUF_COMP];	/**< Buffer offset.*/
    } hb_mem_graphic_buf_t;

5.4.5.3. hb_mem_graphic_buf_group_t

    /**
    * @struct hb_mem_graphic_buf_group_t
    * Define the descriptor of graphic buffer group
    */
    typedef struct hb_mem_graphic_buf_group_t {
        //#define HB_MEM_MAXIMUM_GRAPH_BUF 8
        hb_mem_graphic_buf_t graph_group[HB_MEM_MAXIMUM_GRAPH_BUF];	/**graphic buffer array*/
        int32_t group_id;		/**< graphic buffer group id which alloc from ION driver*/
        uint32_t bit_map;		/**< graphic buffer group bitmap*/
    } hb_mem_graphic_buf_group_t;

5.4.5.4. hb_mem_buf_queue_t

    /**
    * @struct hb_mem_buf_queue_t
    * Define the descriptor of buffer queue.
    */
    typedef struct hb_mem_buf_queue_t {
        uint64_t unique_id;	/**< Unique id specified by memory manager. Should not be modified.*/
        uint32_t count;		/**< Total items of the buffer queue.*/
        uint32_t item_size;	/**< Size of each item.*/
    } hb_mem_buf_queue_t;

5.4.5.5. hb_mem_pool_t

    /**
    * @struct hb_mem_pool_t
    * Define the descriptor of memory pool.
    */
    typedef struct hb_mem_pool_t {
        int64_t flags;		/**< Buffer flags for allocation.@mem_usage_t*/
        uint64_t size;		/**< Total Buffer size specified by user during allocation.*/
        int32_t fd;			/**< File descriptors of the pool.*/
        int32_t page_size;	/**< Page size in byte.*/
        int32_t total_page_cnt;	/**< Total page count.*/
        int32_t avail_page_cnt;	/**< Available page count.*/
        int32_t cur_client_cnt;	/**< Current pool client count.*/

        int32_t reserved;		/**< reserved*/
    } hb_mem_pool_t;

5.4.5.6. hb_mem_share_pool_t

    /**
    * @struct hb_mem_share_pool_t
    * Define the descriptor of share memory pool.
    */
    typedef struct hb_mem_share_pool_t {
        int64_t flags;		/**< Buffer flags for allocation.@mem_usage_t*/
        uint32_t size;		/**< Buffer size specified by user during allocation.*/
        int32_t fd;			/**< File descriptors of the pool.*/
        int32_t total_buf_cnt;	/**< Total buf count.*/
        int32_t avail_buf_cnt;	/**< Available buffer count.*/

        int32_t reserved;		/**< reserved*/
    } hb_mem_share_pool_t;

5.4.5.7. hbmem Memory Allocation Attributes

Reference for memory allocation attributes.

Item Description
HB_MEM_USAGE_CPU_READ_NEVER CPU will not read this memory; memory will not be allocated with read permission
HB_MEM_USAGE_CPU_READ_OFTEN CPU frequently reads this memory; memory will be allocated with read permission
HB_MEM_USAGE_CPU_READ_MASK Mask to extract read-related attributes; HB_MEM_USAGE_CPU_READ_OFTEN has higher priority than HB_MEM_USAGE_CPU_READ_NEVER
HB_MEM_USAGE_CPU_WRITE_NEVER CPU will not write this memory; memory will not be allocated with write permission
HB_MEM_USAGE_CPU_WRITE_OFTEN CPU frequently writes this memory; memory will be allocated with write permission (read permission is automatically added)
HB_MEM_USAGE_CPU_WRITE_MASK Mask to extract write-related attributes; HB_MEM_USAGE_CPU_WRITE_OFTEN has higher priority than HB_MEM_USAGE_CPU_WRITE_NEVER
HB_MEM_USAGE_HW_CIM Indicates memory is used by Camera Interface Module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_PYRAMID Indicates memory is used by Pyramid module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_GDC Indicates memory is used as input buffer for Geometric Distortion Correction module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_GDC_OUT Indicates memory is used as output buffer for Geometric Distortion Correction module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_STITCH Indicates memory is used by Stitch module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_OPTICAL_FLOW Indicates memory is used by Optical Flow module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_BPU Indicates memory is used by BPU module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_ISP Indicates memory is used by ISP module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_DISPLAY Indicates memory is used by Display module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_VIDEO_CODEC Indicates memory is used by Video Codec module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_JPEG_CODEC Indicates memory is used by JPEG Codec module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_VDSP Indicates memory is used by VDSP module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_IPC Indicates memory is used by IPC module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_PCIE Indicates memory is used by PCIe module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_YNR Indicates memory is used by YNR module; does not affect allocation, used for debug info
HB_MEM_USAGE_HW_MASK Mask to extract hardware-related attributes; attributes under this mask are mutually exclusive, priority decreases from top to bottom; if multiple or invalid attributes are specified, defaults to "other"
HB_MEM_USAGE_MAP_INITIALIZED Memory needs to be initialized; allocated memory will be zero-initialized. If neither MAP_INITIALIZED nor MAP_UNINITIALIZED is specified, DMA heap is initialized by default while RESERVED heap is not. This attribute is mutually exclusive with HB_MEM_USAGE_MAP_UNINITIALIZED and has higher priority
HB_MEM_USAGE_MAP_UNINITIALIZED Memory does not need initialization; allocated memory remains uninitialized. This attribute is mutually exclusive with HB_MEM_USAGE_MAP_INITIALIZED and has lower priority
HB_MEM_USAGE_CACHED Indicates the buffer has cache attribute
HB_MEM_USAGE_GRAPHIC_CONTIGUOUS_BUF Specifies that graphic buffer should be allocated with physically contiguous memory
HB_MEM_USAGE_MEM_POOL Indicates the buffer is used for memory pool; users need not specify this parameter when allocating buffer; even if specified, it is ignored internally
HB_MEM_USAGE_MEM_SHARE_POOL Indicates the buffer is used for memory share pool; users need not specify this parameter when allocating buffer; even if specified, it is ignored internally
HB_MEM_USAGE_TRIVIAL_MASK Mask to extract miscellaneous attributes; except HB_MEM_USAGE_MAP_INITIALIZED and HB_MEM_USAGE_MAP_UNINITIALIZED being mutually exclusive, others can coexist
HB_MEM_USAGE_PRIV_HEAP_DMA Specifies memory allocation from DMA heap
HB_MEM_USAGE_PRIV_HEAP_RESERVERD Specifies memory allocation from Carveout heap; the first is the original definition (for backward compatibility), the second is the latest definition; use the latter
HB_MEM_USAGE_PRIV_HEAP_2_RESERVERD Specifies memory allocation from Carveout heap2; the first is the original definition (for backward compatibility), the second is the latest definition; use the latter
HB_MEM_USAGE_PRIV_MASK Mask to extract private attributes; attributes under this mask are mutually exclusive, priority decreases from top to bottom; if multiple attributes are specified, higher priority ones take precedence; if an invalid value is specified, defaults to allocation from DMA heap

5.4.5.8. hbmem Image Formats

Item Description
MEM_PIX_FMT_NONE Invalid format
MEM_PIX_FMT_RGB565 packed RGB 5:6:5, 16bpp
MEM_PIX_FMT_RGB24 packed RGB 8:8:8, 24bpp
MEM_PIX_FMT_BGR24 packed RGB 8:8:8, 24bpp
MEM_PIX_FMT_ARGB packed ARGB 8:8:8:8, 32bpp
MEM_PIX_FMT_RGBA packed RGBA 8:8:8:8, 32bpp
MEM_PIX_FMT_ABGR packed ABGR 8:8:8:8, 32bpp
MEM_PIX_FMT_BGRA packed BGRA 8:8:8:8
MEM_PIX_FMT_YUV420P planar YUV 4:2:0, 12bpp (1 Cr & Cb sample per 2x2 Y samples)
MEM_PIX_FMT_NV12 planar YUV 4:2:0, 12bpp (1 plane for Y and 1 plane for the UV component)
MEM_PIX_FMT_NV21 planar YUV 4:2:0, 12bpp (1 plane for Y and 1 plane for the VU component)
MEM_PIX_FMT_YUV422P planar YUV 4:2:2, 16bpp (1 Cr & Cb sample per 2x1 Y samples)
MEM_PIX_FMT_NV16 planar YUV 4:2:2, 16bpp (interleaved chroma: first byte U, then V)
MEM_PIX_FMT_NV61 planar YUV 4:2:2, 16bpp (interleaved chroma: first byte V, then U)
MEM_PIX_FMT_YUYV422 packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
MEM_PIX_FMT_YVYU422 packed YUV 4:2:2, 16bpp, Y0 Cr Y1 Cb
MEM_PIX_FMT_UYVY422 packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
MEM_PIX_FMT_VYUY422 packed YUV 4:2:2, 16bpp, Cr Y0 Cb Y1
MEM_PIX_FMT_YUV444 packed YUV 4:4:4, 24bpp
MEM_PIX_FMT_YUV444P planar YUV 4:4:4, 24bpp
MEM_PIX_FMT_NV24 YUV 4:4:4, 24bpp (interleaved chroma: first byte U, then V)
MEM_PIX_FMT_NV42 YUV 4:4:4, 24bpp (interleaved chroma: first byte V, then U)
MEM_PIX_FMT_YUV440P planar YUV 4:4:0
MEM_PIX_FMT_YUV400 Gray Y, YUV 4:0:0
MEM_PIX_FMT_RAW8 raw8 format
MEM_PIX_FMT_RAW10 raw10 format
MEM_PIX_FMT_RAW12 raw12 format
MEM_PIX_FMT_RAW14 raw14 format
MEM_PIX_FMT_RAW16 raw16 format
MEM_PIX_FMT_RAW20 raw20 format
MEM_PIX_FMT_RAW24 raw24 format
MEM_PIX_FMT_TOTAL total format count

5.4.6. hbmem Return Values

Reference for error codes.

Error Code Macro Definition Description
0xFF000001 HB_MEM_ERR_UNKNOWN Unknown error
0xFF000002 HB_MEM_ERR_INVALID_PARAMS Invalid parameters
0xFF000003 HB_MEM_ERR_INVALID_FD Invalid file descriptor
0xFF000004 HB_MEM_ERR_INVALID_VADDR Invalid virtual address
0xFF000005 HB_MEM_ERR_INSUFFICIENT_MEM Insufficient memory resources
0xFF000006 HB_MEM_ERR_TOO_MANY_FD Too many file descriptors opened
0xFF000007 HB_MEM_ERR_TIMEOUT Timeout
0xFF000008 HB_MEM_ERR_MODULE_NOT_FOUND Memory module not opened
0xFF000009 HB_MEM_ERR_MODULE_OPEN_FAIL Failed to open memory module
0xFF00000A HB_MEM_ERR_QUEUE_NOT_FOUND Memory queue not created
0xFF00000B HB_MEM_ERR_QUEUE_DESTROYED Memory queue has been destroyed
0xFF00000C HB_MEM_ERR_QUEUE_WRONG_SLOT Invalid memory queue slot index
0xFF00000D HB_MEM_ERR_QUEUE_NO_AVAILABLE_SLOT No available slot in memory queue
0xFF00000E HB_MEM_ERR_QUEUE_ALREADY_EXIST Memory queue already exists
0xFF00000F HB_MEM_ERR_POOL_NOT_FOUND Memory pool not opened
0xFF000010 HB_MEM_ERR_POOL_DESTROYED Memory pool has been destroyed
0xFF000011 HB_MEM_ERR_POOL_BUSY Buffers in the memory pool have not been released
0xFF000012 HB_MEM_ERR_WAIT_SHARE_FAILURE Failed to wait for share client status
0xFF000013 HB_MEM_ERR_NOT_ALLOW Operation not allowed
0xFF000014 HB_MEM_ERR_CHECK_VER_FAIL Version check failed
0xFF000015 HB_MEM_ERR_REGISTER_FAIL Failed to register graphic buffer group
0xFF000016 HB_MEM_ERR_INVALID_GROUPID Invalid group ID