Click here to Skip to main content
15,894,343 members
Articles / Programming Languages / C++

A sample of memory pool

Rate me:
Please Sign up or sign in to vote.
1.48/5 (7 votes)
26 Mar 20071 min read 28.8K   972   20  
Memory Pool
#pragma once
#include <list>
using namespace std;

class BufferPool
{
public:
	BufferPool(size_t size, size_t count);
	virtual ~BufferPool(void);
public:
	///allocate memory
	char* Allocate(size_t size);
	///release memory
	void Release(char* buf);
public:
	size_t GetBufferSize() const { return _bufferSize; };
	size_t GetBufferCount() const { return _bufferCount; };
	size_t GetFreeListSize() const { return _freeList.size(); };
	size_t Size() const { return _bufferCount * _bufferSize; };
private:
	bool IsValidAddr(char*);
	size_t CalSize(char* buf);
	char* MyNew(size_t size);
	void MyDelete(char* buf);
private:
	CRITICAL_SECTION _cs;
	size_t _bufferSize;///< the size of one buffer unit
	size_t _bufferCount;///< the count of all buffer units
	char * _firstAddr;///< the started address of memory pool
	char * _lastAddr;///< the ended address of memory pool
	list<char*> _freeList;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Bony Chen, a senior software engineer, specializes in C++, COM, C#, ASP.net, ISAPI. He has nearly seven years of software development experience, and has successfully developed many influential software products, such as SPES, CSO, QQ (a famous IM tool in China).
And now he is focusing on P2P technology. He aims at being an expert in software development, software consulting, software components and computer science.
If you have any opinions or questions in software area, please contact bonyren@163.com.

Comments and Discussions