自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
HIDL MemoryBlock
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
HIDL MemoryBlock
是建構在 hidl_memory
、HIDL
@1.0::IAllocator
和 HIDL @1.0::IMapper
上的抽象層。這項工具專為 HIDL 服務而設計,這些服務具有多個記憶體區塊,可共用單一記憶體堆積區。
在應用程式中使用 MemoryBlock
可大幅減少 mmap
/munmap
和使用者空間區隔錯誤的數量,進而提升效能。例如:
- 使用每個緩衝區分配的
hidl_memory
,平均為 238 us/1 個分配。
- 使用
MemoryBlock
並共用單一 hidl_memory
的平均分配量為 2.82 us/1。
建築
HIDL MemoryBlock
架構包括具備多個記憶體區塊共用單一記憶體堆積的 HIDL 服務:
圖 1. HIDL MemoryBlock 架構
一般使用
本節提供使用 MemoryBlock
的範例,首先宣告 HAL,然後實作 HAL。
宣告 HAL
以下是 IFoo HAL 的範例:
import android.hidl.memory.block@1.0::MemoryBlock;
interface IFoo {
getSome() generates(MemoryBlock block);
giveBack(MemoryBlock block);
};
Android.bp
如下所示:
hidl_interface {
...
srcs: [
"IFoo.hal",
],
interfaces: [
"android.hidl.memory.block@1.0",
...
};
實作 HAL
如要實作範例 HAL,請按照下列步驟操作:
取得 hidl_memory
(詳情請參閱 HIDL C++)。
#include <android/hidl/allocator/1.0/IAllocator.h>
using ::android::hidl::allocator::V1_0::IAllocator;
using ::android::hardware::hidl_memory;
...
sp<IAllocator> allocator = IAllocator::getService("ashmem");
allocator->allocate(2048, [&](bool success, const hidl_memory& mem)
{
if (!success) { /* error */ }
// you can now use the hidl_memory object 'mem' or pass it
}));
使用已取得的 hidl_memory
建立 HidlMemoryDealer
例項:
#include <hidlmemory/HidlMemoryDealer.h>
using ::android::hardware::HidlMemoryDealer
/* The mem argument is acquired in the Step1, returned by the ashmemAllocator->allocate */
sp<HidlMemoryDealer> memory_dealer = HidlMemoryDealer::getInstance(mem);
分配 MemoryBlock
,這是使用 HIDL 定義的結構體。
MemoryBlock
範例:
struct MemoryBlock {
IMemoryToken token;
uint64_t size;
uint64_t offset;
};
使用 MemoryDealer
分配 MemoryBlock
的範例:
#include <android/hidl/memory/block/1.0/types.h>
using ::android::hidl::memory::block::V1_0::MemoryBlock;
Return<void> Foo::getSome(getSome_cb _hidl_cb) {
MemoryBlock block = memory_dealer->allocate(1024);
if(HidlMemoryDealer::isOk(block)){
_hidl_cb(block);
...
釋放 MemoryBlock
:
Return<void> Foo::giveBack(const MemoryBlock& block) {
memory_dealer->deallocate(block.offset);
...
操控資料:
#include <hidlmemory/mapping.h>
#include <android/hidl/memory/1.0/IMemory.h>
using ::android::hidl::memory::V1_0::IMemory;
sp<IMemory> memory = mapMemory(block);
uint8_t* data =
static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
設定 Android.bp
:
shared_libs: [
"android.hidl.memory@1.0",
"android.hidl.memory.block@1.0"
"android.hidl.memory.token@1.0",
"libhidlbase",
"libhidlmemory",
查看流程,判斷是否需要 lockMemory
。
通常,MemoryBlock
會使用參照計數來維護共用 hidl_memory
,而這項作業會在其 MemoryBlock
instances is mapped and is
munmap()-ed when nothing refers to it. To keep
hidl_memoryalways mapped, you can use
lockMemory, a RAII style object
that keeps the corresponding
hidl_memory` 第一次對應至整個鎖定週期時,以 mmap()
形式執行。例子:
#include <hidlmemory/mapping.h>
sp<RefBase> lockMemory(const sp<IMemoryToken> key);
擴充用途
本節將詳細說明 MemoryBlock
的擴充用法。
使用參照計數管理 MemoryBlock
在大多數情況下,使用 MemoryBlock
最有效率的方式是明確地配置/釋放。不過,在複雜的應用程式中,使用參照計數進行垃圾收集可能會是較好的做法。如要為 MemoryBlock
計算參照計數,您可以將 MemoryBlock
與繫結器物件繫結,這有助於計算參照數,並在計數減少至零時釋放 MemoryBlock
。
宣告 HAL
宣告 HAL 時,請說明含有 MemoryBlock
例項和 IBase 的 HIDL 結構體:
import android.hidl.memory.block@1.0::MemoryBlock;
struct MemoryBlockAllocation {
MemoryBlock block;
IBase refcnt;
};
使用 MemoryBlockAllocation
取代 MemoryBlock
,並移除方法,以便傳回 MemoryBlock
。系統會透過 MemoryBlockAllocation
的參照計數來釋放。例子:
interface IFoo {
allocateSome() generates(MemoryBlockAllocation allocation);
};
實作 HAL
HAL 的服務端實作範例:
class MemoryBlockRefCnt: public virtual IBase {
MemoryBlockRefCnt(uint64_t offset, sp<MemoryDealer> dealer)
: mOffset(offset), mDealer(dealer) {}
~MemoryBlockRefCnt() {
mDealer->deallocate(mOffset);
}
private:
uint64_t mOffset;
sp<MemoryDealer> mDealer;
};
Return<void> Foo::allocateSome(allocateSome_cb _hidl_cb) {
MemoryBlockAllocation allocation;
allocation.block = memory_dealer->allocate(1024);
if(HidlMemoryDealer::isOk(block)){
allocation.refcnt= new MemoryBlockRefCnt(...);
_hidl_cb(allocation);
HAL 用戶端實作範例:
ifoo->allocateSome([&](const MemoryBlockAllocation& allocation){
...
);
部分應用程式需要額外資料才能綁定已分配的 MemoryBlock
。您可以透過兩種方法附加及擷取中繼資料:
如果應用程式存取中繼資料的頻率與區塊本身相同,請附加中繼資料,並在結構體中傳遞所有中繼資料。例子:
import android.hidl.memory.block@1.0::MemoryBlock;
struct MemoryBlockWithMetaData{
MemoryBlock block;
MetaDataStruct metaData;
};
如果應用程式存取中繼資料的頻率遠低於區塊,透過介面被動傳遞中繼資料會更有效率。例子:
import android.hidl.memory.block@1.0::MemoryBlock;
struct MemoryBlockWithMetaData{
MemoryBlock block;
IMetaData metaData;
};
接下來,請使用 MemoryDealer
將中繼資料繫結至 MemoryBlock
。例子:
MemoryBlockWithMetaData memory_block;
memory_block.block = dealer->allocate(size);
if(HidlMemoryDealer::isOk(block)){
memory_block.metaData = new MetaData(...);
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# HIDL MemoryBlock\n\nThe HIDL `MemoryBlock` is an abstract layer built on `hidl_memory`, `HIDL\n@1.0::IAllocator`, and `HIDL @1.0::IMapper`. It is designed for HIDL services\nthat have multiple memory blocks to share a single memory heap.\n\nPerformance improvements\n------------------------\n\nUsing `MemoryBlock` in apps can significantly reduce the number of\n`mmap`/`munmap` and user space segmentation faults, thus improving performance.\nFor example:\n\n- Using per `hidl_memory` for each buffer allocation averages 238 us/1 allocation.\n- Using `MemoryBlock` and sharing a single `hidl_memory` averages 2.82 us/1 allocation.\n\nArchitecture\n------------\n\nThe HIDL `MemoryBlock` architecture includes HIDL services with multiple memory\nblocks sharing a single memory heap:\n\n**Figure 1.** HIDL MemoryBlock architecture\n\nNormal usage\n------------\n\nThis section provides an example of using `MemoryBlock` by first declaring the\nHAL then implementing the HAL.\n\n### Declare the HAL\n\nFor the following example IFoo HAL: \n\n import android.hidl.memory.block@1.0::MemoryBlock;\n\n interface IFoo {\n getSome() generates(MemoryBlock block);\n giveBack(MemoryBlock block);\n };\n\nThe `Android.bp` is as follows: \n\n hidl_interface {\n ...\n srcs: [\n \"IFoo.hal\",\n ],\n interfaces: [\n \"android.hidl.memory.block@1.0\",\n ...\n };\n\n### Implement the HAL\n\nTo implement the example HAL:\n\n1. Get the `hidl_memory` (for details, refer to [HIDL\n C++](/docs/core/architecture/hidl-cpp)).\n\n #include \u003candroid/hidl/allocator/1.0/IAllocator.h\u003e\n\n using ::android::hidl::allocator::V1_0::IAllocator;\n using ::android::hardware::hidl_memory;\n ...\n sp\u003cIAllocator\u003e allocator = IAllocator::getService(\"ashmem\");\n allocator-\u003eallocate(2048, [&](bool success, const hidl_memory& mem)\n {\n if (!success) { /* error */ }\n // you can now use the hidl_memory object 'mem' or pass it\n }));\n\n2. Make a `HidlMemoryDealer` instance with the acquired `hidl_memory`:\n\n #include \u003chidlmemory/HidlMemoryDealer.h\u003e\n\n using ::android::hardware::HidlMemoryDealer\n /* The mem argument is acquired in the Step1, returned by the ashmemAllocator-\u003eallocate */\n sp\u003cHidlMemoryDealer\u003e memory_dealer = HidlMemoryDealer::getInstance(mem);\n\n3. Allocate `MemoryBlock`, which is a struct defined with HIDL.\n\n Example `MemoryBlock`: \n\n struct MemoryBlock {\n IMemoryToken token;\n uint64_t size;\n uint64_t offset;\n };\n\n Example using the `MemoryDealer` to allocate a `MemoryBlock`: \n\n #include \u003candroid/hidl/memory/block/1.0/types.h\u003e\n\n using ::android::hidl::memory::block::V1_0::MemoryBlock;\n\n Return\u003cvoid\u003e Foo::getSome(getSome_cb _hidl_cb) {\n MemoryBlock block = memory_dealer-\u003eallocate(1024);\n if(HidlMemoryDealer::isOk(block)){\n _hidl_cb(block);\n ...\n\n4. Deallocate `MemoryBlock`:\n\n Return\u003cvoid\u003e Foo::giveBack(const MemoryBlock& block) {\n memory_dealer-\u003edeallocate(block.offset);\n ...\n\n5. Manipulate the data:\n\n #include \u003chidlmemory/mapping.h\u003e\n #include \u003candroid/hidl/memory/1.0/IMemory.h\u003e\n\n using ::android::hidl::memory::V1_0::IMemory;\n\n sp\u003cIMemory\u003e memory = mapMemory(block);\n uint8_t* data =\n\n static_cast\u003cuint8_t*\u003e(static_cast\u003cvoid*\u003e(memory-\u003egetPointer()));\n\n6. Config `Android.bp`:\n\n shared_libs: [\n \"android.hidl.memory@1.0\",\n\n \"android.hidl.memory.block@1.0\"\n\n \"android.hidl.memory.token@1.0\",\n \"libhidlbase\",\n \"libhidlmemory\",\n\n7. Review the flow to determine if you need to `lockMemory`.\n\n Normally, `MemoryBlock` uses reference count to maintain the shared\n `hidl_memory` which is `mmap()`-ed the first time one of its `MemoryBlock\n instances is mapped and is`munmap()`-ed when nothing refers to it. To keep`hidl_memory`always mapped, you can use`lockMemory`, a RAII style object\n that keeps the corresponding`hidl_memory\\` mapped throughout the lock\n lifecycle. Example: \n\n #include \u003chidlmemory/mapping.h\u003e\n\n sp\u003cRefBase\u003e lockMemory(const sp\u003cIMemoryToken\u003e key);\n\nExtended usage\n--------------\n\nThis section provides details about the extended usage of `MemoryBlock`.\n\n### Use reference count to manage MemoryBlock\n\nIn most situations, the most efficient way to use `MemoryBlock` is to explicitly\nallocate/deallocate. However, in complicated apps using reference count\nfor garbage collection might be a better idea. To have reference count on\n`MemoryBlock`, you can bind `MemoryBlock` with a binder object, which helps to\ncount the references and deallocate the `MemoryBlock` when the count decreases\nto zero.\n\n### Declare the HAL\n\nWhen declaring the HAL, describe a HIDL struct that contains a `MemoryBlock`\ninstance and an IBase: \n\n import android.hidl.memory.block@1.0::MemoryBlock;\n\n struct MemoryBlockAllocation {\n MemoryBlock block;\n IBase refcnt;\n };\n\nUse `MemoryBlockAllocation` to replace `MemoryBlock` and remove the method\nto give back `MemoryBlock`. It's deallocated by reference counting\nwith `MemoryBlockAllocation`. Example: \n\n interface IFoo {\n allocateSome() generates(MemoryBlockAllocation allocation);\n };\n\n### Implement the HAL\n\nExample of the service side implementation of the HAL: \n\n class MemoryBlockRefCnt: public virtual IBase {\n MemoryBlockRefCnt(uint64_t offset, sp\u003cMemoryDealer\u003e dealer)\n : mOffset(offset), mDealer(dealer) {}\n ~MemoryBlockRefCnt() {\n mDealer-\u003edeallocate(mOffset);\n }\n private:\n uint64_t mOffset;\n sp\u003cMemoryDealer\u003e mDealer;\n };\n\n Return\u003cvoid\u003e Foo::allocateSome(allocateSome_cb _hidl_cb) {\n MemoryBlockAllocation allocation;\n allocation.block = memory_dealer-\u003eallocate(1024);\n if(HidlMemoryDealer::isOk(block)){\n allocation.refcnt= new MemoryBlockRefCnt(...);\n _hidl_cb(allocation);\n\nExample of the client side implementation of the HAL: \n\n ifoo-\u003eallocateSome([&](const MemoryBlockAllocation& allocation){\n ...\n );\n\n### Attach and retrieve metadata\n\nSome apps need additional data to bind with the allocated `MemoryBlock`.\nYou can append and retrieve metadata using two methods:\n\n- If the app accesses the metadata as often as the block itself,\n append the metadata and pass them all in a struct. Example:\n\n import android.hidl.memory.block@1.0::MemoryBlock;\n\n struct MemoryBlockWithMetaData{\n MemoryBlock block;\n MetaDataStruct metaData;\n };\n\n- If the app accesses the metadata much less frequently than the\n block, it is more efficient to pass the metadata passively with an\n interface. Example:\n\n import android.hidl.memory.block@1.0::MemoryBlock;\n\n struct MemoryBlockWithMetaData{\n MemoryBlock block;\n IMetaData metaData;\n };\n\n Next, bind the metadata with the `MemoryBlock` using `MemoryDealer`.\n Example: \n\n MemoryBlockWithMetaData memory_block;\n memory_block.block = dealer-\u003eallocate(size);\n if(HidlMemoryDealer::isOk(block)){\n memory_block.metaData = new MetaData(...);"]]