Skip to content

Commit

Permalink
Revert "[CP] Fix mem leak issue"
Browse files Browse the repository at this point in the history
This reverts commit ed8ffa5.

Change-Id: I71080ecf9987fa2834037b5389df5d80d04e471c
  • Loading branch information
peiranzh authored and gfxbot committed Oct 17, 2019
1 parent 6d71f76 commit fb1103f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 80 deletions.
123 changes: 46 additions & 77 deletions media_driver/linux/common/cp/shared/cplib_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "mos_utilities.h"

std::unordered_map<const char*, void*> CPLibUtils::m_symbols;
std::mutex CPLibUtils::m_referenceMutex;
int CPLibUtils::m_referenceCount = 0;

void * CPLibUtils::m_phandle = nullptr;
const char *CPLibUtils::CPLIB_PATH = "cplib.so";
Expand All @@ -49,106 +47,77 @@ const char *CPLibUtils::FUNC_SETUP_MOS_APO_SWITCH = "Setup_MosApogeiosSwitch"

bool CPLibUtils::LoadCPLib(VADriverContextP ctx)
{
m_referenceMutex.lock();

m_phandle = dlopen(CPLIB_PATH, RTLD_NOW | RTLD_LOCAL);

if(nullptr == m_phandle)
{
CPLIB_ASSERTMESSAGE("Failed to open %s %s", CPLIB_PATH, dlerror());
m_referenceMutex.unlock();
CPLIB_NORMALMESSAGE("Failed to open %s %s", CPLIB_PATH, dlerror());
return false;
}
else
{
++m_referenceCount;
if(1 == m_referenceCount)
{
m_symbols.clear();

m_symbols[FUNC_GET_CPLIB_MAJOR_VERSION] = dlsym(m_phandle, FUNC_GET_CPLIB_MAJOR_VERSION);
m_symbols[FUNC_INIT_CPLIB] = dlsym(m_phandle, FUNC_INIT_CPLIB);
m_symbols[FUNC_RELEASE_CPLIB] = dlsym(m_phandle, FUNC_RELEASE_CPLIB);
m_symbols[FUNC_CREATE_DDICP] = dlsym(m_phandle, FUNC_CREATE_DDICP);
m_symbols[FUNC_DELETE_DDICP] = dlsym(m_phandle, FUNC_DELETE_DDICP);
m_symbols[FUNC_CREATE_MHWCP] = dlsym(m_phandle, FUNC_CREATE_MHWCP);
m_symbols[FUNC_DELETE_MHWCP] = dlsym(m_phandle, FUNC_DELETE_MHWCP);
m_symbols[FUNC_CREATE_MOSCP] = dlsym(m_phandle, FUNC_CREATE_MOSCP);
m_symbols[FUNC_DELETE_MOSCP] = dlsym(m_phandle, FUNC_DELETE_MOSCP);
m_symbols[FUNC_CREATE_MEDIALIBVACAPSCP] = dlsym(m_phandle, FUNC_CREATE_MEDIALIBVACAPSCP);
m_symbols[FUNC_DELETE_MEDIALIBVACAPSCP] = dlsym(m_phandle, FUNC_DELETE_MEDIALIBVACAPSCP);
m_symbols[FUNC_CREATE_SECUREDECODE] = dlsym(m_phandle, FUNC_CREATE_SECUREDECODE);
m_symbols[FUNC_DELETE_SECUREDECODE] = dlsym(m_phandle, FUNC_DELETE_SECUREDECODE);
m_symbols[FUNC_SETUP_MOS_APO_SWITCH] = dlsym(m_phandle, FUNC_SETUP_MOS_APO_SWITCH);

// confirm if all symbols exported from CPLIB are exist
for(auto item : m_symbols)
{
if(nullptr == item.second)
{
CPLIB_ASSERTMESSAGE("Symbol: %s not found in CPLIB", item.first);
m_referenceMutex.unlock();
UnloadCPLib(ctx);
return false;
}
}
m_symbols.clear();

using FuncTypeGetVersion = uint32_t (*)();
FuncTypeGetVersion func = reinterpret_cast<FuncTypeGetVersion>(m_symbols[FUNC_GET_CPLIB_MAJOR_VERSION]);
m_symbols[FUNC_GET_CPLIB_MAJOR_VERSION] = dlsym(m_phandle, FUNC_GET_CPLIB_MAJOR_VERSION);
m_symbols[FUNC_INIT_CPLIB] = dlsym(m_phandle, FUNC_INIT_CPLIB);
m_symbols[FUNC_RELEASE_CPLIB] = dlsym(m_phandle, FUNC_RELEASE_CPLIB);
m_symbols[FUNC_CREATE_DDICP] = dlsym(m_phandle, FUNC_CREATE_DDICP);
m_symbols[FUNC_DELETE_DDICP] = dlsym(m_phandle, FUNC_DELETE_DDICP);
m_symbols[FUNC_CREATE_MHWCP] = dlsym(m_phandle, FUNC_CREATE_MHWCP);
m_symbols[FUNC_DELETE_MHWCP] = dlsym(m_phandle, FUNC_DELETE_MHWCP);
m_symbols[FUNC_CREATE_MOSCP] = dlsym(m_phandle, FUNC_CREATE_MOSCP);
m_symbols[FUNC_DELETE_MOSCP] = dlsym(m_phandle, FUNC_DELETE_MOSCP);
m_symbols[FUNC_CREATE_MEDIALIBVACAPSCP] = dlsym(m_phandle, FUNC_CREATE_MEDIALIBVACAPSCP);
m_symbols[FUNC_DELETE_MEDIALIBVACAPSCP] = dlsym(m_phandle, FUNC_DELETE_MEDIALIBVACAPSCP);
m_symbols[FUNC_CREATE_SECUREDECODE] = dlsym(m_phandle, FUNC_CREATE_SECUREDECODE);
m_symbols[FUNC_DELETE_SECUREDECODE] = dlsym(m_phandle, FUNC_DELETE_SECUREDECODE);
m_symbols[FUNC_SETUP_MOS_APO_SWITCH] = dlsym(m_phandle, FUNC_SETUP_MOS_APO_SWITCH);

if(REQUIRED_CPLIB_MAJOR_VERSION != func())
// confirm if all symbols exported from CPLIB are exist
for(auto item : m_symbols)
{
if(nullptr == item.second)
{
CPLIB_ASSERTMESSAGE("The CPLIB version does not meet the require");
m_referenceMutex.unlock();
CPLIB_NORMALMESSAGE("Symbol: %s not found in CPLIB", item.first);
UnloadCPLib(ctx);
return false;
}
}

using FuncTypeInitCplib = bool (*)(VADriverContextP ctx);
bool initialized = false;
InvokeCpFunc<FuncTypeInitCplib>(initialized, FUNC_INIT_CPLIB, ctx);
if (!initialized)
{
CPLIB_ASSERTMESSAGE("Failed to initialized CPLIB");
m_referenceMutex.unlock();
UnloadCPLib(ctx);
return false;
}
using FuncType = uint32_t (*)();
FuncType func = reinterpret_cast<FuncType>(m_symbols[FUNC_GET_CPLIB_MAJOR_VERSION]);

if(REQUIRED_CPLIB_MAJOR_VERSION != func())
{
CPLIB_NORMALMESSAGE("The CPLIB version does not meet the require");
UnloadCPLib(ctx);
return false;
}
}

m_referenceMutex.unlock();
using FuncType = bool (*)(VADriverContextP ctx);
bool initialized = false;
InvokeCpFunc<FuncType>(initialized, FUNC_INIT_CPLIB, ctx);
if (!initialized)
{
CPLIB_NORMALMESSAGE("Failed to initialized CPLIB");
UnloadCPLib(ctx);
return false;
}

CPLIB_NORMALMESSAGE("CPLIB Loaded Successfully");
return true;
}

void CPLibUtils::UnloadCPLib(VADriverContextP ctx)
{
m_referenceMutex.lock();
--m_referenceCount;
using FuncType = void (*)(VADriverContextP ctx);
InvokeCpFunc<FuncType>(FUNC_RELEASE_CPLIB, ctx);

if(m_referenceCount < 0)
{
CPLIB_ASSERTMESSAGE("Invalid m_referenceCount");
}
else if(0 == m_referenceCount)
if(nullptr != m_phandle)
{
if(m_symbols.empty())
{
CPLIB_ASSERTMESSAGE("m_symbols is empty");
m_referenceMutex.unlock();
return;
}

using FuncType = void (*)(VADriverContextP ctx);
InvokeCpFunc<FuncType>(FUNC_RELEASE_CPLIB, ctx);

if(nullptr != m_phandle)
{
m_symbols.clear();
if(0 != dlclose(m_phandle)) // dlclose will return 0 if execution sucecceed
CPLIB_ASSERTMESSAGE("Failed to close CPLIB %s", dlerror());
}
m_symbols.clear();
if(0 != dlclose(m_phandle)) // dlclose will return 0 if execution sucecceed
CPLIB_ASSERTMESSAGE("Failed to close CPLIB %s", dlerror());
}
m_referenceMutex.unlock();
}
3 changes: 0 additions & 3 deletions media_driver/linux/common/cp/shared/cplib_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#include <dlfcn.h>
#include <unordered_map>
#include <mutex>

#define REQUIRED_CPLIB_MAJOR_VERSION 1

Expand Down Expand Up @@ -103,8 +102,6 @@ class CPLibUtils
static const char* FUNC_SETUP_MOS_APO_SWITCH;

static std::unordered_map<const char*, void*> m_symbols;
static std::mutex m_referenceMutex;
static int m_referenceCount;

private:
static void* m_phandle;
Expand Down

0 comments on commit fb1103f

Please sign in to comment.