)
// {
// return NULL;
// }
//
// return (void*)pThis;
//}
CObjAllocator::CObjAllocator(size_t uiObjSize, int iObjCount, Function_CreateObj pfCreateObj)
{
__ASSERT_AND_LOG(uiObjSize > 0 && iObjCount > 0 && pfCreateObj);
m_shObjAllocType = EOAT_ALLOC_BY_SELF;
m_iObjCount = iObjCount;
m_uiObjSize = uiObjSize;
m_pfCreateObjFunc = pfCreateObj;
m_astIdxs = new CIdx[m_iObjCount];
size_t uiObjMemorySize = uiObjSize * iObjCount;
char *pstObjMem = new char[uiObjMemorySize];
m_pstObjBuffer = (CObj *)pstObjMem;
__ASSERT_AND_LOG(m_astIdxs && m_pstObjBuffer);
Initialize();
}
size_t CObjAllocator::CountSize(size_t uiObjSize, int iObjCount)
{
return sizeof(CObjAllocator) + uiObjSize * iObjCount + iObjCount * sizeof(CIdx);
}
CObjAllocator *CObjAllocator::CreateByGivenMemory(char *pszMemoryAddress, size_t uiMemorySize, size_t uiObjSize,
int iObjCount, Function_CreateObj pfCreateObj)
{
if (pszMemoryAddress == NULL || uiObjSize <= 0 || iObjCount <= 0 || pfCreateObj == NULL)
{
TRACESVR("%p, %d, %d, %p.\n", pszMemoryAddress, (int)uiObjSize, iObjCount, pfCreateObj);
return NULL;
}
size_t uiSharedMemorySize = sizeof(CObjAllocator) + uiObjSize * iObjCount +
iObjCount * sizeof(CIdx);
if (uiSharedMemorySize > uiMemorySize)
{
TRACESVR("ObjAllocator: alloc size %lu > sh size %lu.\n", (unsigned long)uiSharedMemorySize, (unsigned long)uiMemorySize);
return NULL;
}
//在指定的内存地址上分配CObjAllocator
CObjAllocator *pstObjAllocator = (CObjAllocator *)pszMemoryAddress;
if (!pstObjAllocator)
{
TRACESVR("ObjAllocator: pstObjAllocator is NULL.\n");
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 *)pszMemoryAddress + sizeof(CObjAllocator));
pstObjAllocator->m_pstObjBuffer = (CObj *)((unsigned char *)pszMemoryAddress + sizeof(CObjAllocator) + iObjCount * sizeof(CIdx));
pstObjAllocator->Initialize();
return pstObjAllocator;
}
CObjAllocator *CObjAllocator::ResumeByGivenMemory(char *pszMemoryAddress,
size_t uiMemorySize, size_t uiObjSize, int iObjCount, Function_CreateObj pfCreateObj)
{
if ((NULL == pszMemoryAddress) || (uiObjSize <= 0) || (iObjCount <= 0))
{
return NULL;
}
size_t uiSharedMemorySize = sizeof(CObjAllocator) + uiObjSize * iObjCount + sizeof(CIdx) * iObjCount;
if (uiSharedMemorySize > uiMemorySize)
{
return NULL;
}
CObjAllocator *pstObjAllocator = (CObjAllocator *)pszMemoryAddress;
if ((pstObjAllocator->m_uiObjSize != uiObjSize) || (pstObjAllocator->m_iObjCount != iObjCount))
{
return NULL;
}
pstObjAllocator->m_shObjAllocType = EOAT_ALLOC_BY_SHARED_MEMORY;
pstObjAllocator->m_astIdxs = (CIdx *)((unsigned char *)pszMemoryAddress + sizeof(CObjAllocator));
pstObjAllocator->m_pstObjBuffer = (CObj *)((unsigned char *)pszMemoryAddress + sizeof(CObjAllocator) + iObjCount * sizeof(CIdx));
int i;
// 重新绑定obj和idx
for (i = 0; i < iObjCount; ++i)
{
// 调用placement-n |