From d1cafd9f0160545cc1257d51297fd7ef708ebe4a Mon Sep 17 00:00:00 2001 From: foredward Date: Thu, 11 Sep 2025 11:57:09 +0800 Subject: [PATCH 1/2] ext mem optimize Signed-off-by: foredward --- .../napi/ethernet/customeap/eap_event_mgr.cpp | 18 ++++++------------ .../js/napi/ethernet/customeap/eap_event_mgr.h | 2 +- .../napi/ethernet/customeap/eap_napi_event.cpp | 14 +++++--------- .../src/proxy/net_eap_callback_stub.cpp | 2 ++ .../include/proxy/net_eap_callback_stub.h | 2 ++ .../ethernetmanager/include/net_eap_handler.h | 2 +- 6 files changed, 17 insertions(+), 23 deletions(-) diff --git a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp index 08429a9b..1efd0a49 100644 --- a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp +++ b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp @@ -166,7 +166,6 @@ void NetEapPostBackCallback::EventNotify(const std::shared_ptr & NETMANAGER_EXT_LOGE("asyncEvent is null!"); return; } - NETMANAGER_EXT_LOGI("Enter eap postback event notify, msgId: %{public}d", asyncEvent->msgId_); auto sendTask = std::bind(&NetEapPostBackCallback::SendTask, this, asyncEvent); if (napi_status::napi_ok != napi_send_event(asyncEvent->env_, sendTask, napi_eprio_immediate)) { NETMANAGER_EXT_LOGE("%{public}s, Failed to SendEvent", __func__); @@ -177,23 +176,18 @@ void NetManagerNapiAbilityStatusChange::OnAddSystemAbility(int32_t systemAbility { NETMANAGER_EXT_LOGI("NetManagerNapiAbilityStatusChange OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId); - std::vector event; switch (systemAbilityId) { case WIFI_DEVICE_SA_ID: - case COMM_ETHERNET_MANAGER_SYS_ABILITY_ID: { + case COMM_ETHERNET_MANAGER_SYS_ABILITY_ID EapEventMgr::GetInstance().RegCustomEapHandler(NetType::WLAN0, RegTriggerMode::SA_LAUNCH); break; - } default: - NETMANAGER_EXT_LOGI("OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId); return; } } void NetManagerNapiAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) { - NETMANAGER_EXT_LOGI("NetManagerNapiAbilityStatusChange OnRemoveSystemAbility systemAbilityId:%{public}d", - systemAbilityId); } EapEventMgr &EapEventMgr::GetInstance() @@ -236,8 +230,8 @@ int32_t EapEventMgr::RegCustomEapHandler(napi_env env, NetType netType, uint32_t if (netTypeMapIter == eventRegisterInfo_.end()) { NETMANAGER_EXT_LOGI("%{public}s, new netType!", __func__); TypeMapRegObj mapObj; - mapObj[composeParam] = std::vector{regObj}; - eventRegisterInfo_[netType] = mapObj; + mapObj.emplace(composeParam, std::vector{regObj}); + eventRegisterInfo_.emplace(netType, mapObj); } else { NETMANAGER_EXT_LOGI("%{public}s, exist netType!", __func__); TypeMapRegObj mapObj = netTypeMapIter->second; @@ -249,8 +243,8 @@ int32_t EapEventMgr::RegCustomEapHandler(napi_env env, NetType netType, uint32_t NETMANAGER_EXT_LOGE("%{public}s, RegisterInfo Exceeding the maximum value!", __func__); return EAP_ERRCODE_INTERNAL_ERROR; } - mapObj[composeParam] = std::vector{regObj}; - eventRegisterInfo_[netType] = mapObj; + mapObj.emplace(composeParam, std::vector{regObj}); + eventRegisterInfo_.emplace(netType, mapObj); } else { auto vecIter = std::find_if(iter->second.begin(), iter->second.end(), [®Obj] (const RegObj &obj) { return regObj.m_regEnv == obj.m_regEnv;}); @@ -345,7 +339,7 @@ int32_t EapEventMgr::ReplyCustomEapData(CustomResult result, const sptr eapData); } -std::map EapEventMgr::GetRegisterInfoMap() +std::map& EapEventMgr::GetRegisterInfoMap() { return eventRegisterInfo_; } diff --git a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h index 584712c8..026375a7 100644 --- a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h +++ b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h @@ -136,7 +136,7 @@ public: private: sptr eapPostBackCallback_ = nullptr; OHOS::sptr mSaStatusListener = nullptr; - std::map eventRegisterInfo_; + std::map& eventRegisterInfo_; }; } // namespace NetManagerStandard } // namespace OHOS diff --git a/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp b/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp index fa206b87..728f5ad3 100644 --- a/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp +++ b/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp @@ -18,7 +18,8 @@ namespace OHOS { namespace NetManagerStandard { - + +static std::mutex g_mutex; namespace { #ifdef NET_EXTENSIBLE_AUTHENTICATION @@ -207,11 +208,8 @@ napi_value ReplyCustomEapData(napi_env env, napi_callback_info info) return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_RESULT); } CustomResult customResult = static_cast(replyResult); - sptr eapData = new (std::nothrow) EapData(); - if (eapData == nullptr) { - NETMANAGER_EXT_LOGE("%{public}s, eapData is nullptr", __func__); - return EapNapiReturn(env, false, EAP_ERRCODE_INTERNAL_ERROR); - } + std::unique_lock lock(g_mutex); + sptr eapData = sptr::MakeSptr(); eapData->msgId = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "msgId"); eapData->bufferLen = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "bufferLen"); NapiUtils::GetVectorUint8Property(env, argv[ARG_INDEX_1], "eapBuffer", eapData->eapBuffer); @@ -225,9 +223,7 @@ napi_value ReplyCustomEapData(napi_env env, napi_callback_info info) return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_SIZE_OF_EAPDATA); } NETMANAGER_EXT_LOGI("%{public}s, result:%{public}d, msgId:%{public}d, bufferLen:%{public}d, buffsize:%{public}zu, " - "eapCode:%{public}d, eapType:%{public}d ", - __func__, static_cast(customResult), eapData->msgId, eapData->bufferLen, eapData->eapBuffer.size(), - eapData->eapCode, eapData->eapType); + __func__, static_cast(customResult), eapData->msgId, eapData->bufferLen, eapData->eapBuffer.size()); int32_t ret = EapEventMgr::GetInstance().ReplyCustomEapData(customResult, eapData); return EapNapiReturn(env, ret == EAP_ERRCODE_SUCCESS, ret); #endif diff --git a/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp b/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp index 66981c5b..52be968d 100644 --- a/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp +++ b/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp @@ -29,6 +29,7 @@ NetEapPostbackCallbackStub::NetEapPostbackCallbackStub() int32_t NetEapPostbackCallbackStub::OnEapSupplicantResult(MessageParcel &data, MessageParcel &reply) { + std::unique_lock lock(postbackMtx_); int32_t netType = 0; if (!data.ReadInt32(netType)) { return NETMANAGER_ERR_READ_DATA_FAIL; @@ -131,6 +132,7 @@ int32_t NetRegisterEapCallbackStub::OnRegisterCustomEapCallback(MessageParcel &d int32_t NetRegisterEapCallbackStub::OnReplyCustomEapDataEvent(MessageParcel &data, MessageParcel &reply) { + std::unique_lock lock(replyMtx_); int32_t result = -1; if (!data.ReadInt32(result)) { return NETMANAGER_ERR_READ_DATA_FAIL; diff --git a/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h b/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h index c617eb3f..0095f473 100644 --- a/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h +++ b/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h @@ -39,6 +39,7 @@ private: private: std::map memberFuncMap_; + std::mutex postbackMtx_; }; class NetRegisterEapCallbackStub : public IRemoteStub { @@ -59,6 +60,7 @@ private: private: std::map memberFuncMap_; + std::mutex replyMtx_; }; } // namespace NetManagerStandard diff --git a/services/ethernetmanager/include/net_eap_handler.h b/services/ethernetmanager/include/net_eap_handler.h index 9dff9c33..366caf80 100644 --- a/services/ethernetmanager/include/net_eap_handler.h +++ b/services/ethernetmanager/include/net_eap_handler.h @@ -54,7 +54,7 @@ private: private: std::map nTMapMsgId_; std::map> regEapCallBack_; - sptr postbackCallback_; + sptr postbackCallback_ = nullptr; std::mutex callbackMutex_; std::mutex mutex_; #ifdef NET_EXTENSIBLE_AUTHENTICATION -- Gitee From f30aff544c940d0716028a905aa0f2e7ca9d6e5a Mon Sep 17 00:00:00 2001 From: foredward Date: Thu, 11 Sep 2025 11:57:09 +0800 Subject: [PATCH 2/2] ext mem optimize Signed-off-by: foredward --- .../napi/ethernet/customeap/eap_event_mgr.cpp | 18 ++++++------------ .../js/napi/ethernet/customeap/eap_event_mgr.h | 2 +- .../napi/ethernet/customeap/eap_napi_event.cpp | 16 ++++++---------- .../src/proxy/net_eap_callback_stub.cpp | 2 ++ .../include/proxy/net_eap_callback_stub.h | 2 ++ .../ethernetmanager/include/net_eap_handler.h | 2 +- 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp index 08429a9b..1efd0a49 100644 --- a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp +++ b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.cpp @@ -166,7 +166,6 @@ void NetEapPostBackCallback::EventNotify(const std::shared_ptr & NETMANAGER_EXT_LOGE("asyncEvent is null!"); return; } - NETMANAGER_EXT_LOGI("Enter eap postback event notify, msgId: %{public}d", asyncEvent->msgId_); auto sendTask = std::bind(&NetEapPostBackCallback::SendTask, this, asyncEvent); if (napi_status::napi_ok != napi_send_event(asyncEvent->env_, sendTask, napi_eprio_immediate)) { NETMANAGER_EXT_LOGE("%{public}s, Failed to SendEvent", __func__); @@ -177,23 +176,18 @@ void NetManagerNapiAbilityStatusChange::OnAddSystemAbility(int32_t systemAbility { NETMANAGER_EXT_LOGI("NetManagerNapiAbilityStatusChange OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId); - std::vector event; switch (systemAbilityId) { case WIFI_DEVICE_SA_ID: - case COMM_ETHERNET_MANAGER_SYS_ABILITY_ID: { + case COMM_ETHERNET_MANAGER_SYS_ABILITY_ID EapEventMgr::GetInstance().RegCustomEapHandler(NetType::WLAN0, RegTriggerMode::SA_LAUNCH); break; - } default: - NETMANAGER_EXT_LOGI("OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId); return; } } void NetManagerNapiAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) { - NETMANAGER_EXT_LOGI("NetManagerNapiAbilityStatusChange OnRemoveSystemAbility systemAbilityId:%{public}d", - systemAbilityId); } EapEventMgr &EapEventMgr::GetInstance() @@ -236,8 +230,8 @@ int32_t EapEventMgr::RegCustomEapHandler(napi_env env, NetType netType, uint32_t if (netTypeMapIter == eventRegisterInfo_.end()) { NETMANAGER_EXT_LOGI("%{public}s, new netType!", __func__); TypeMapRegObj mapObj; - mapObj[composeParam] = std::vector{regObj}; - eventRegisterInfo_[netType] = mapObj; + mapObj.emplace(composeParam, std::vector{regObj}); + eventRegisterInfo_.emplace(netType, mapObj); } else { NETMANAGER_EXT_LOGI("%{public}s, exist netType!", __func__); TypeMapRegObj mapObj = netTypeMapIter->second; @@ -249,8 +243,8 @@ int32_t EapEventMgr::RegCustomEapHandler(napi_env env, NetType netType, uint32_t NETMANAGER_EXT_LOGE("%{public}s, RegisterInfo Exceeding the maximum value!", __func__); return EAP_ERRCODE_INTERNAL_ERROR; } - mapObj[composeParam] = std::vector{regObj}; - eventRegisterInfo_[netType] = mapObj; + mapObj.emplace(composeParam, std::vector{regObj}); + eventRegisterInfo_.emplace(netType, mapObj); } else { auto vecIter = std::find_if(iter->second.begin(), iter->second.end(), [®Obj] (const RegObj &obj) { return regObj.m_regEnv == obj.m_regEnv;}); @@ -345,7 +339,7 @@ int32_t EapEventMgr::ReplyCustomEapData(CustomResult result, const sptr eapData); } -std::map EapEventMgr::GetRegisterInfoMap() +std::map& EapEventMgr::GetRegisterInfoMap() { return eventRegisterInfo_; } diff --git a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h index 584712c8..026375a7 100644 --- a/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h +++ b/frameworks/js/napi/ethernet/customeap/eap_event_mgr.h @@ -136,7 +136,7 @@ public: private: sptr eapPostBackCallback_ = nullptr; OHOS::sptr mSaStatusListener = nullptr; - std::map eventRegisterInfo_; + std::map& eventRegisterInfo_; }; } // namespace NetManagerStandard } // namespace OHOS diff --git a/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp b/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp index fa206b87..0ac3250e 100644 --- a/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp +++ b/frameworks/js/napi/ethernet/customeap/eap_napi_event.cpp @@ -18,7 +18,8 @@ namespace OHOS { namespace NetManagerStandard { - + +static std::mutex g_mutex; namespace { #ifdef NET_EXTENSIBLE_AUTHENTICATION @@ -207,11 +208,8 @@ napi_value ReplyCustomEapData(napi_env env, napi_callback_info info) return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_RESULT); } CustomResult customResult = static_cast(replyResult); - sptr eapData = new (std::nothrow) EapData(); - if (eapData == nullptr) { - NETMANAGER_EXT_LOGE("%{public}s, eapData is nullptr", __func__); - return EapNapiReturn(env, false, EAP_ERRCODE_INTERNAL_ERROR); - } + std::unique_lock lock(g_mutex); + sptr eapData = sptr::MakeSptr(); eapData->msgId = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "msgId"); eapData->bufferLen = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "bufferLen"); NapiUtils::GetVectorUint8Property(env, argv[ARG_INDEX_1], "eapBuffer", eapData->eapBuffer); @@ -224,10 +222,8 @@ napi_value ReplyCustomEapData(napi_env env, napi_callback_info info) NETMANAGER_EXT_LOGE("bufferLen is mismatch buffer size."); return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_SIZE_OF_EAPDATA); } - NETMANAGER_EXT_LOGI("%{public}s, result:%{public}d, msgId:%{public}d, bufferLen:%{public}d, buffsize:%{public}zu, " - "eapCode:%{public}d, eapType:%{public}d ", - __func__, static_cast(customResult), eapData->msgId, eapData->bufferLen, eapData->eapBuffer.size(), - eapData->eapCode, eapData->eapType); + NETMANAGER_EXT_LOGI("%{public}s, result:%{public}d, msgId:%{public}d, bufferLen:%{public}d, buffsize:%{public}zu", + __func__, static_cast(customResult), eapData->msgId, eapData->bufferLen, eapData->eapBuffer.size()); int32_t ret = EapEventMgr::GetInstance().ReplyCustomEapData(customResult, eapData); return EapNapiReturn(env, ret == EAP_ERRCODE_SUCCESS, ret); #endif diff --git a/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp b/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp index 66981c5b..52be968d 100644 --- a/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp +++ b/frameworks/native/ethernetclient/src/proxy/net_eap_callback_stub.cpp @@ -29,6 +29,7 @@ NetEapPostbackCallbackStub::NetEapPostbackCallbackStub() int32_t NetEapPostbackCallbackStub::OnEapSupplicantResult(MessageParcel &data, MessageParcel &reply) { + std::unique_lock lock(postbackMtx_); int32_t netType = 0; if (!data.ReadInt32(netType)) { return NETMANAGER_ERR_READ_DATA_FAIL; @@ -131,6 +132,7 @@ int32_t NetRegisterEapCallbackStub::OnRegisterCustomEapCallback(MessageParcel &d int32_t NetRegisterEapCallbackStub::OnReplyCustomEapDataEvent(MessageParcel &data, MessageParcel &reply) { + std::unique_lock lock(replyMtx_); int32_t result = -1; if (!data.ReadInt32(result)) { return NETMANAGER_ERR_READ_DATA_FAIL; diff --git a/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h b/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h index c617eb3f..0095f473 100644 --- a/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h +++ b/interfaces/innerkits/ethernetclient/include/proxy/net_eap_callback_stub.h @@ -39,6 +39,7 @@ private: private: std::map memberFuncMap_; + std::mutex postbackMtx_; }; class NetRegisterEapCallbackStub : public IRemoteStub { @@ -59,6 +60,7 @@ private: private: std::map memberFuncMap_; + std::mutex replyMtx_; }; } // namespace NetManagerStandard diff --git a/services/ethernetmanager/include/net_eap_handler.h b/services/ethernetmanager/include/net_eap_handler.h index 9dff9c33..366caf80 100644 --- a/services/ethernetmanager/include/net_eap_handler.h +++ b/services/ethernetmanager/include/net_eap_handler.h @@ -54,7 +54,7 @@ private: private: std::map nTMapMsgId_; std::map> regEapCallBack_; - sptr postbackCallback_; + sptr postbackCallback_ = nullptr; std::mutex callbackMutex_; std::mutex mutex_; #ifdef NET_EXTENSIBLE_AUTHENTICATION -- Gitee