设为首页 加入收藏

TOP

内存对象管理器(基于数组和链表实现)(四)
2019-09-04 00:58:14 】 浏览:128
Tags:内存 对象 管理 基于 实现
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
首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇洛谷 P2725 邮票题解 下一篇洛谷 P1455 搭配购买

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目