/******************
CMemoryPool.cpp
******************/
/*!\file CMemoryPool.cpp
* \brief CMemoryPool implementation.
*/
#include "../BasicIncludes.h"
#include "SMemoryChunk.h"
#include "CMemoryPool.h"
namespace MemPool
{
static const int FREEED_MEMORY_CONTENT = 0xAA ; //!< Value for freed memory
static const int NEW_ALLOCATED_MEMORY_CONTENT = 0xFF ; //!< Initial Value for new allocated memory
/******************
Constructor
******************/
CMemoryPool::CMemoryPool(const std::size_t &sInitialMemoryPoolSize,
const std::size_t &sMemoryChunkSize,
const std::size_t &sMinimalMemorySizeToAllocate,
bool bSetMemoryData)
{
m_ptrFirstChunk = NULL ;
m_ptrLastChunk = NULL ;
m_ptrCursorChunk = NULL ;
m_sTotalMemoryPoolSize = 0 ;
m_sUsedMemoryPoolSize = 0 ;
m_sFreeMemoryPoolSize = 0 ;
m_sMemoryChunkSize = sMemoryChunkSize ;
m_uiMemoryChunkCount = 0 ;
m_uiObjectCount = 0 ;
m_bSetMemoryData = bSetMemoryData ;
m_sMinimalMemorySizeToAllocate = sMinimalMemorySizeToAllocate ;
// Allocate the Initial amount of Memory from the Operating-System...
AllocateMemory(sInitialMemoryPoolSize) ;
}
/******************
Destructor
******************/
CMemoryPool::~CMemoryPool()
{
FreeAllAllocatedMemory() ;
DeallocateAllChunks() ;
// Check for possible Memory-Leaks...
assert((m_uiObjectCount == 0) && "WARNING : Memory-Leak : You have not freed all allocated Memory") ;
}
/******************
GetMemory
******************/
void *CMemoryPool::GetMemory(const std::size_t &sMemorySize)
{
std::size_t sBestMemBlockSize = CalculateBestMemoryBlockSize(sMemorySize) ;
SMemoryChunk *ptrChunk = NULL ;
while(!ptrChunk)
{
// Is a Chunks available to hold the requested amount of Memory ?
ptrChunk = FindChunkSuitableToHoldMemory(sBestMemBlockSize) ;
if(!ptrChunk)
{
// No chunk can be found
// => Memory-Pool is to small. We have to request
// more Memory from the Operating-System....
sBestMemBlockSize = MaxValue(sBestMemBlockSize, CalculateBestMemoryBlockSize(m_sMinimalMemorySizeToAllocate)) ;
AllocateMemory(sBestMemBlockSize) ;
}
}
// Finally, a suitable Chunk was found.
// Adjust the Values of the internal "TotalSize"/"UsedSize" Members and
// the Values of the MemoryChunk itself.
m_sUsedMemoryPoolSize += sBestMemBlockSize ;
m_sFreeMemoryPoolSize -= sBestMemBlockSize ;
m_uiObjectCount++ ;
SetMemoryChunkValues(ptrChunk, sBestMemBlockSize) ;
// eventually, return the Pointer to the User
return ((void *) ptrChunk->Data) ;
}
/******************
FreeMemory
******************/
void CMemoryPool::FreeMemory(void *ptrMemoryBlock, const std::size_t &sMemoryBlockSize)
{
// Search all Chunks for the one holding the "ptrMemoryBlock"-Pointer
// ("SMemoryChunk->Data == ptrMemoryBlock"). Eventually, free that Chunks,
// so it beecomes available to the Memory-Pool again...
SMemoryChunk *ptrChunk = FindChunkHoldingPointerTo(ptrMemoryBlock) ;
if(ptrChunk)
{
//std::cerr << "Freed Chunks OK (Used memPool Size : " << m_sUsedMemoryPoolSize << ")" << std::endl ;
FreeChunks(ptrChunk) ;
}
else
{
assert(false && "ERROR : Requested Pointer not in Memory Pool") ;
}
assert((m_uiObjectCount > 0) && "ERROR : Request to delete more Memory then allocated.") ;
m_uiObjectCount-- ;
}
/******************
AllocateMemory
******************/
bool CMemoryPool::AllocateMemory(const std::size_t &sMemorySize)
{
// This function will allocate *at least* "sMemorySize"-Bytes from the Operating-System.
// How it works :
// Calculate the amount of "SMemoryChunks" needed to manage the requested MemorySize.
// Every MemoryChunk can manage only a certain amount of Memory
// (set by the "m_sMemoryChunkSize"-Member of the Memory-Pool).
//
// Also, calculate the "Best" Memory-Block size to allocate from the
// Operating-System, so that all allocated Memory can be assigned to a
// Memory Chunk.
// Example :
// You want to Allocate 120 Bytes, but every "SMemoryChunk" can only handle
// 50 Bytes ("m_sMemoryChunkSize = 50").
// So, "CalculateNeededChunks()" will return the Number of Chunks needed to
// manage 120 Bytes. Since it is not possible to divide 120 Bytes in to
// 50 Byte Chunks, "CalculateNeededChunks()" returns 3.
// ==> 3 Chunks can Manage 150 Bytes of data (50 * 3 = 150), so
// the requested 120 Bytes will fit into this Block.
// "CalculateBestMemoryBlockSize()" will return the amount of memory needed
// to *perfectly* subdivide the allocated Memory into "m_sMemoryChunkSize" (= 50) Byte
// pieces. -> "CalculateBestMemoryBlockSize()" returns 150.
// So, 150 Bytes of memory are allocated from the Operating-System and
// subdivided into 3 Memory-Chunks (each holding a Pointer to 50 Bytes of the allocated memory).
// Since only 120 Bytes are requested, we have a Memory-Overhead of
// 150 - 120 = 30 Bytes.
// Note, that the Memory-overhead is not a bad thing, because we can use
// that memory later (it remains in the Memory-Pool).
//
unsigned int uiNeededChunks = CalculateNeededChunks(sMemorySize) ;
std::size_t sBestMemBlockSize = CalculateBestMemoryBlockSize(sMemorySize) ;
TByte *ptrNewMemBlock = (TByte *) malloc(sBestMemBlockSize) ; // allocate from Operating System
SMemoryChunk *ptrNewChunks = (SMemoryChunk *) malloc((uiNeededChunks * sizeof(SMemoryChunk))) ; // allocate Chunk-Array to Manage the Memory
assert(((ptrNewMemBlock) && (ptrNewChunks)) && "Error : System ran out of Memory") ;
// Adjust internal Values (Total/Free Memory, etc.)
m_sTotalMemoryPoolSize += sBestMemBlockSize ;
m_sFreeMemoryPoolSize += sBestMemBlockSize ;
m_uiMemoryChunkCount += uiNeededChunks ;
// Usefull for Debugging : Set the Memory-Content to a defined Value
if(m_bSetMemoryData)
{
memset(((void *) ptrNewMemBlock), NEW_ALLOCATED_MEMORY_CONTENT, sBestMemBlockSize) ;
}
// Associate the allocated Memory-Block with the Linked-List of MemoryChunks
return LinkChunksToData(ptrNewChunks, uiNeededChunks, ptrNewMemBlock) ; ;
}
/******************
CalculateNeededChunks
******************/
unsigned int CMemoryPool::CalculateNeededChunks(const std::size_t &sMemorySize)
{
float f = (float) (((float)sMemorySize) / ((float)m_sMemoryChunkSize)) ;
return ((unsigned int) ceil(f)) ;
}
/******************
CalculateBestMemoryBlockSize
******************/
std::size_t CMemoryPool::CalculateBestMemoryBlockSize(const std::size_t &sRequestedMemoryBlockSize)
{
unsigned int uiNeededChunks = CalculateNeededChunks(sRequestedMemoryBlockSize) ;
return std::size_t((uiNeededChunks * m_sMemoryChunkSize)) ;
}
/******************
FreeChunks
******************/
void CMemoryPool::FreeChunks(SMemoryChunk *ptrChunk)
{
// Make the Used Memory of the given Chunk available
// to the Memory Pool again.
SMemoryChunk *ptrCurrentChunk = ptrChunk ;
unsigned int uiChunkCount = CalculateNeededChunks(ptrCurrentChunk->UsedSize);
for(unsigned int i = 0; i < uiChunkCount; i++)
{
if(ptrCurrentChunk)
{
// Step 1 : Set the allocated Memory to 'FREEED_MEMORY_CONTENT'
// Note : This is fully Optional, but usefull for debugging
if(m_bSetMemoryData)
{
memset(((void *) ptrCurrentChunk->Data), FREEED_MEMORY_CONTENT, m_sMemoryChunkSize) ;
}
// Step 2 : Set the Used-Size to Zero
ptrCurrentChunk->UsedSize = 0 ;
// Step 3 : Adjust Memory-Pool Values and goto next Chunk
m_sUsedMemoryPoolSize -= m_sMemoryChunkSize ;
ptrCurrentChunk = ptrCurrentChunk->Next ;
}
}
}
/******************
FindChunkSuitableToHoldMemory
******************/
SMemoryChunk *CMemoryPo
没有合适的资源?快使用搜索试试~ 我知道了~
Msrc.rar_内存池
共58个文件
tlog:18个
h:4个
pdb:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 64 浏览量
2022-09-19
19:43:47
上传
评论
收藏 15.24MB RAR 举报
温馨提示
一个内存池的实现代码,非常强大好用,可以根据自己的需要修改成
资源推荐
资源详情
资源评论
收起资源包目录
Msrc.rar (58个子文件)
MemoryPool_src
MemoryPool.sln 881B
C++ 内存池 -- C++ Memory Pool - 060的专栏 - 博客频道 - CSDN_NET.mht 961KB
MemoryPool.suo 16KB
UpgradeLog.XML 6KB
_UpgradeReport_Files
UpgradeReport.xslt 12KB
UpgradeReport_Minus.gif 69B
UpgradeReport.css 3KB
UpgradeReport_Plus.gif 71B
MemoryPool.prj 5KB
内存分配模块 nedmalloc - OPEN 开发经验库.mht 485KB
MakeDoxygenDoku.bat 497B
MemoryPool.sdf 26.33MB
MemoryPool.dsw 571B
Doxyfile 9KB
MemoryPool.sln.old 909B
src
BasicIncludes.h 526B
main.cpp 6KB
MemPool
SMemoryChunk.h 979B
CMemoryPool.h 8KB
IMemoryBlock.h 1KB
CMemoryPool.cpp 14KB
MemoryPool.vcxproj 6KB
MemoryPool.vcxproj.filters 2KB
MemoryPool.vcproj 4KB
MemoryPool.vcxproj.user 143B
ipch
memorypool-932af29d
memorypool-37b9bdff.ipch 44.06MB
Debug
MemoryPool.exe.intermediate.manifest 381B
vc100.idb 611KB
MemoryPool.pdb 2.8MB
CMemoryPool.obj 350KB
link.4876-cvtres.write.1.tlog 2B
link.4876.read.1.tlog 2B
MemoryPool.exe 656KB
CL.write.1.tlog 1KB
MemoryPool_manifest.rc 210B
CL.read.1.tlog 27KB
mt.read.1.tlog 350B
rc.write.1.tlog 330B
rc.read.1.tlog 322B
MemoryPool.lastbuildstate 73B
MemoryPool.ilk 1.68MB
mt.command.1.tlog 412B
MemoryPool.exe.embed.manifest 406B
cl.command.1.tlog 1KB
link-cvtres.read.1.tlog 2B
main.obj 309KB
MemoryPool.exe.embed.manifest.res 472B
link.write.1.tlog 1024B
link.4876.write.1.tlog 2B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
rc.command.1.tlog 578B
link.read.1.tlog 4KB
MemoryPool.log 3KB
link.4876-cvtres.read.1.tlog 2B
mt.write.1.tlog 350B
vc100.pdb 276KB
MemoryPool.dsp 5KB
共 58 条
- 1
资源评论
御道御小黑
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功