设为首页 加入收藏

TOP

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

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目