未验证 提交 a668776f 编写于 作者: 马根堂 提交者: Gitee

remoteobject 内存管理优化

Signed-off-by: N马根堂 <magentang4@huawei.com>
上级 febbe275
......@@ -55,6 +55,7 @@ static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjec
IPCObjectProxy::IPCObjectProxy(int handle, std::u16string descriptor, int proto)
: IRemoteObject(std::move(descriptor)), handle_(handle), proto_(proto), isFinishInit_(false), isRemoteDead_(false)
{
ZLOGD(LABEL, "[mem_free]IPCObjectProxy create");
ExtendObjectLifetime();
}
......
......@@ -96,7 +96,7 @@ public:
private:
std::mutex mutex_;
std::u16string descriptor_;
sptr<JRemoteObject> cachedObject_;
wptr<JRemoteObject> cachedObject_;
};
/*
......@@ -250,7 +250,7 @@ sptr<JRemoteObject> JRemoteObjectHolder::Get(jobject object)
// so it will not be freed util this reference released.
sptr<JRemoteObject> remoteObject = nullptr;
if (cachedObject_ != nullptr) {
remoteObject = cachedObject_;
remoteObject = cachedObject_.promote();
}
if (remoteObject == nullptr) {
......
......@@ -64,7 +64,7 @@ private:
std::mutex mutex_;
napi_env env_ = nullptr;
std::u16string descriptor_;
sptr<NAPIRemoteObject> cachedObject_;
wptr<NAPIRemoteObject> cachedObject_;
napi_ref localInterfaceRef_;
int32_t attachCount_;
};
......
......@@ -53,6 +53,7 @@ inline T *ConvertNativeValueTo(NativeValue *value)
static void RemoteObjectHolderFinalizeCb(napi_env env, void *data, void *hint)
{
ZLOGI(LOG_LABEL, "[mem_free]RemoteObjectHolderFinalizeCb inter");
NAPIRemoteObjectHolder *holder = reinterpret_cast<NAPIRemoteObjectHolder *>(data);
if (holder == nullptr) {
ZLOGW(LOG_LABEL, "RemoteObjectHolderFinalizeCb null holder");
......@@ -61,12 +62,14 @@ static void RemoteObjectHolderFinalizeCb(napi_env env, void *data, void *hint)
holder->Lock();
int32_t curAttachCount = holder->DecAttachCount();
if (curAttachCount == 0) {
ZLOGI(LOG_LABEL, "[mem_free]RemoteObjectHolderFinalizeCb release holder");
delete holder;
}
}
static void *RemoteObjectDetachCb(NativeEngine *engine, void *value, void *hint)
{
ZLOGI(LOG_LABEL, "[mem_free]RemoteObjectDetachCb inter");
(void)engine;
(void)hint;
return value;
......@@ -74,6 +77,7 @@ static void *RemoteObjectDetachCb(NativeEngine *engine, void *value, void *hint)
static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void *hint)
{
ZLOGI(LOG_LABEL, "[mem_free]RemoteObjectAttachCb inter");
(void)hint;
NAPIRemoteObjectHolder *holder = reinterpret_cast<NAPIRemoteObjectHolder *>(value);
if (holder == nullptr) {
......@@ -115,6 +119,7 @@ static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void
napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
{
ZLOGI(LOG_LABEL, "[mem_free]RemoteObject_JS_Constructor inter");
// new napi remote object
size_t argc = 2;
size_t expectedArgc = 1;
......@@ -155,6 +160,7 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_value thisVar, const std::u16string &descriptor)
: IPCObjectStub(descriptor)
{
ZLOGI(LOG_LABEL, "[mem_free]NAPIRemoteObject create");
env_ = env;
thisVar_ = thisVar;
napi_create_reference(env, thisVar_, 1, &thisVarRef_);
......@@ -163,7 +169,7 @@ NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_value thisVar, const std::
NAPIRemoteObject::~NAPIRemoteObject()
{
ZLOGI(LOG_LABEL, "NAPIRemoteObject Destructor");
ZLOGI(LOG_LABEL, "[mem_free]NAPIRemoteObject Destructor");
if (thisVarRef_ != nullptr) {
napi_status status = napi_delete_reference(env_, thisVarRef_);
NAPI_ASSERT_RETURN_VOID(env_, status == napi_ok, "failed to delete ref to js RemoteObject");
......
......@@ -20,10 +20,13 @@
namespace OHOS {
NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor)
: env_(env), descriptor_(descriptor), cachedObject_(nullptr), localInterfaceRef_(nullptr), attachCount_(1)
{}
{
ZLOGI(LOG_LABEL, "[mem_free]NAPIRemoteObjectHolder create");
}
NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
{
ZLOGI(LOG_LABEL, "[mem_free]NAPIRemoteObjectHolder free");
// free the reference of object.
cachedObject_ = nullptr;
if (localInterfaceRef_ != nullptr) {
......@@ -33,12 +36,13 @@ NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject)
{
ZLOGI(LOG_LABEL, "[mem_free]NAPIRemoteObjectHolder Get");
std::lock_guard<std::mutex> lockGuard(mutex_);
// grab an strong reference to the object,
// so it will not be freed util this reference released.
sptr<NAPIRemoteObject> remoteObject = nullptr;
if (cachedObject_ != nullptr) {
remoteObject = cachedObject_;
remoteObject = cachedObject_.promote();
}
if (remoteObject == nullptr) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册