ew, 恢复类的虚函数表.
CObj *pstObj = (*pfCreateObj)((unsigned char *)pstObjAllocator->m_pstObjBuffer + uiObjSize * i);
__ASSERT_AND_LOG(pstObj->GetObjectID() == i);
pstObjAllocator->m_astIdxs[i].SetAttachedObj(pstObj);
}
pstObjAllocator->SetCreateObjFunc(pfCreateObj);
return pstObjAllocator;
}
CObjAllocator *CObjAllocator::CreateBySharedMemory(const char *pszKeyFileName, const unsigned char ucKeyPrjID,
size_t uiObjSize, int iObjCount, Function_CreateObj pfCreateObj)
{
if (pszKeyFileName == NULL || uiObjSize <= 0 || iObjCount <= 0 || pfCreateObj == NULL)
{
return NULL;
}
CSharedMemory stSharedMemory;
size_t uiSharedMemorySize = sizeof(CObjAllocator) + uiObjSize * iObjCount +
iObjCount * sizeof(CIdx);
int iRet = stSharedMemory.CreateShmSegment(pszKeyFileName, ucKeyPrjID,
(int)uiSharedMemorySize);
if (iRet)
{
return NULL;
}
//在共享内存的地址上分配CObjAllocator
CObjAllocator *pstObjAllocator = (CObjAllocator *)stSharedMemory.GetFreeMemoryAddress();
if (!pstObjAllocator)
{
return NULL;
}
pstObjAllocator->m_uiObjSize = uiObjSize;
pstObjAllocator->m_iObjCount = iObjCount;
pstObjAllocator->m_pfCreateObjFunc = pfCreateObj;
pstObjAllocator->m_shObjAllocType = EOAT_ALLOC_BY_SHARED_MEMORY;
pstObjAllocator->m_astIdxs = (CIdx *)((unsigned char *)stSharedMemory.GetFreeMemoryAddress() + sizeof(CObjAllocator));
pstObjAllocator->m_pstObjBuffer = (CObj *)((unsigned char *)stSharedMemory.GetFreeMemoryAddress() +
sizeof(CObjAllocator) + iObjCount * sizeof(CIdx));
return pstObjAllocator;
}
CObjAllocator::~CObjAllocator()
{
if (m_shObjAllocType == EOAT_ALLOC_BY_SELF)
{
if (m_astIdxs)
{
delete[] m_astIdxs;
m_astIdxs = NULL;
}
if (m_pstObjBuffer)
{
char *pstObjMem = (char *)m_pstObjBuffer;
delete[] pstObjMem;
m_pstObjBuffer = NULL;
}
}
}
int CObjAllocator::Initialize()
{
if (m_pstObjBuffer == NULL || m_astIdxs == NULL)
{
SetErrorNO(EEN_OBJ_ALLOCATOR__NULL_POINTER);
return -1;
}
if (m_iObjCount <= 0)
{
SetErrorNO(EEN_OBJ_ALLOCATOR__INVALID_OBJ_COUNT);
return -2;
}
//初始化索引数组
int i;
for (i = 0; i < m_iObjCount; ++i)
{
m_astIdxs[i].Initialize();
m_astIdxs[i].SetPrevIdx(i - 1);
m_astIdxs[i].SetNextIdx(i + 1);
}
m_astIdxs[m_iObjCount - 1].SetNextIdx(-1);
//初始化对象数组
for (i = 0; i < m_iObjCount; ++i)
{
CObj *pstObj = (*m_pfCreateObjFunc)((unsigned char *)m_pstObjBuffer + m_uiObjSize * i);
pstObj->m_iObjectID = i;
m_astIdxs[i].SetAttachedObj(pstObj);
}
m_iErrorNO = 0;
m_iFreeHeadIdx = 0;
m_iFreeTailIdx = m_iObjCount - 1;
m_iUsedHeadIdx = -1;
m_iUsedCount = 0;
return 0;
}
int CObjAllocator::CreateObject()
{
if (m_pstObjBuffer == NULL || m_astIdxs == NULL)
{
SetErrorNO(EEN_OBJ_ALLOCATOR__NULL_POINTER);
return -1;
}
if (m_iUsedCount >= m_iObjCount)
{
SetErrorNO(EEN_OBJ_ALLOCATOR__OBJ_IS_FULL);
return -2;
}
if (m_iFreeHeadIdx < 0 || m_iFreeHeadIdx >= m_iObjCount)
{
SetErrorNO(EEN_OBJ_ALLOCATOR__INVALID_OBJ_INDEX);
return -3;
}
//修改空闲链表
int iCurIdx = m_iFreeHeadIdx;
m_iFreeHeadIdx = m_astIdxs[m_iFreeHeadIdx].GetNextIdx();
if (m_iFreeHeadIdx >= 0)
{
m_astIdxs[m_iFreeHea |