配置 AUTOSAR Adaptive 服务数据的内存分配
为了发送服务事件数据,AUTOSAR Adaptive 平台支持以下方法:
按引用 - send 函数使用应用程序地址空间中的内存。在 send 函数返回值后,应用程序可以修改事件数据。
通过
ara::com分配的内存 - 应用程序请求ara::com中间件为数据分配内存。此方法可避免通过ara::com中间件产生的数据副本,对于频繁发送或大量数据可能更高效。但应用程序在发送返回值后会失去对内存的访问权限。
要配置事件发送的内存分配,请打开代码映射编辑器。选择输出端口选项卡并检查每个输出端口。当您选择输出端口时,编辑器会显示代码属性 AllocateMemory。要通过 ara::com 分配的内存发送事件数据,请选择值 true。要通过引用发送事件数据,请选择 false。

如果您将 AllocateMemory 设置为 true,则在生成的 C++ 模型代码中,对应的事件发送使用 ara::com 分配的缓冲区。
void autosar_LaneGuidanceModelClass::step()
{
...
ara::com::SampleAllocateePtr<company::chassis::provided::skeleton::events::
rightHazardIndicator::SampleType> *rightHazardIndicatorAllocateePtrRawPtr;
std::shared_ptr< ara::com::SampleAllocateePtr<company::chassis::provided::
skeleton::events::rightHazardIndicator::SampleType> >
rightHazardIndicatorAllocateePtrSharedPtr;
...
rightHazardIndicatorAllocateePtrSharedPtr = std::make_shared< ara::com::
SampleAllocateePtr<company::chassis::provided::skeleton::events::
rightHazardIndicator::SampleType> >
(ProvidedPort->rightHazardIndicator.Allocate());
rightHazardIndicatorAllocateePtrRawPtr =
rightHazardIndicatorAllocateePtrSharedPtr.get();
// Send: '<S8>/Message Send'
*rightHazardIndicatorAllocateePtrRawPtr->get() = rtb_Merge1;
// Send event
ProvidedPort->rightHazardIndicator.Send(std::move
(*rightHazardIndicatorAllocateePtrRawPtr));
}