Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: C++ Memory ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 34 of 41 Topic 9525 of 9823
Post > Topic >>

Re: C++ Memory Management Innovation: GC Allocator

by xushiwei <xushiweizh@[EMAIL PROTECTED] > Apr 30, 2008 at 04:13 AM

On 4月30日, 上午7时43分, "Chris Thomasson" <cris...@[EMAIL PROTECTED]
>
wrote:
> I am interested in increasing the granularity of region allocators. I
need
> to look at your source-code when I get some time.

It's very simple. Here is the source code:

template <class _Policy>
class region_alloc
{
private:
	typedef typename _Policy::allocator_type _Alloc;
	enum { MemBlockSize = _Policy::MemBlockSize };
	enum { HeaderSize = sizeof(void*) };
	enum { BlockSize = MemBlockSize - HeaderSize };

private:
	struct _MemBlock
	{
		_MemBlock* pPrev;
		char buffer[BlockSize];
	};
	struct _DestroyNode
	{
		_DestroyNode* pPrev;
		destructor_t fnDestroy;
	};

	char* m_begin;
	char* m_end;
	_DestroyNode* m_destroyChain;
	_Alloc m_alloc;

private:
	const region_alloc& operator=(const region_alloc&);

	_MemBlock* BOOST_MEMORY_CALL _ChainHeader() const
	{
		return (_MemBlock*)(m_begin - HeaderSize);
	}

	void BOOST_MEMORY_CALL _Init()
	{
		_MemBlock* pNew = (_MemBlock*)m_alloc.allocate(sizeof(_MemBlock));
		pNew->pPrev = NULL;
		m_begin = pNew->buffer;
		m_end = (char*)pNew + m_alloc.alloc_size(pNew);
	}

public:
	region_alloc() : m_destroyChain(NULL)
	{
		_Init();
	}
	~region_alloc()
	{
		clear();
	}

	void BOOST_MEMORY_CALL clear()
	{
		while (m_destroyChain)
		{
			_DestroyNode* curr = m_destroyChain;
			m_destroyChain = m_destroyChain->pPrev;
			curr->fnDestroy(curr + 1);
		}
		_MemBlock* pHeader = _ChainHeader();
		while (pHeader)
		{
			_MemBlock* curr = pHeader;
			pHeader = pHeader->pPrev;
			m_alloc.deallocate(curr);
		}
		m_begin = m_end = (char*)HeaderSize;
	}

	void* BOOST_MEMORY_CALL allocate(size_t cb)
	{
		if ((size_t)(m_end - m_begin) < cb)
		{
			if (cb >= BlockSize)
			{
				_MemBlock* pHeader = _ChainHeader();
				_MemBlock* pNew = (_MemBlock*)m_alloc.allocate(HeaderSize + cb);
				if (pHeader)
				{
					pNew->pPrev = pHeader->pPrev;
					pHeader->pPrev = pNew;
				}
				else
				{
					m_end = m_begin = pNew->buffer;
					pNew->pPrev = NULL;
				}
				return pNew->buffer;
			}
			else
			{
				_MemBlock* pNew = (_MemBlock*)m_alloc.allocate(sizeof(_MemBlock));
				pNew->pPrev = _ChainHeader();
				m_begin = pNew->buffer;
				m_end = (char*)pNew + m_alloc.alloc_size(pNew);
			}
		}
		return m_end -= cb;
	}

	void* BOOST_MEMORY_CALL allocate(size_t cb, destructor_t fn)
	{
		_DestroyNode* pNode = (_DestroyNode*)allocate(sizeof(_DestroyNode) +
cb);
		pNode->fnDestroy = fn;
		pNode->pPrev = m_destroyChain;
		m_destroyChain = pNode;
		return pNode + 1;
	}

	void BOOST_MEMORY_CALL deallocate(void* p, size_t cb)
	{
		// no action
	}
};


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 41 Posts in Topic:
[Proposal for ISO C/C++] C++ Memory Management Innovation: GC A
xushiwei <xushiweizh@[  2008-04-21 12:13:18 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
"Chris Thomasson&quo  2008-04-21 15:39:15 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
"Chris Thomasson&quo  2008-04-21 16:13:16 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
Alberto Ganesh Barbati &l  2008-04-21 20:43:17 
Re: C++ Memory Management Innovation: GC Allocator
marlow.andrew@[EMAIL PROT  2008-04-21 20:43:18 
Re: C++ Memory Management Innovation: GC Allocator
Lance Diduck <lancedid  2008-04-21 20:43:17 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 00:08:55 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
"Chris Thomasson&quo  2008-04-22 10:52:14 
Re: C++ Memory Management Innovation: GC Allocator
Lance Diduck <lancedid  2008-04-22 11:17:48 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 11:17:25 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 11:13:27 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 15:19:26 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 15:19:18 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 15:22:50 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 15:43:17 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 22:26:21 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 22:25:42 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 22:32:36 
Re: C++ Memory Management Innovation: GC Allocator
Francis Glassborow <fr  2008-04-23 14:13:19 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-23 14:43:18 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-24 01:43:35 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-25 03:42:49 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:47:43 
Re: C++ Memory Management Innovation: GC Allocator
"Stephen Howe"   2008-04-25 10:48:18 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:47:13 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:45:25 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:44:50 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-25 16:15:02 
Re: C++ Memory Management Innovation: GC Allocator
Yongwei Wu <wuyongwei@  2008-04-28 15:58:48 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-28 23:26:31 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-28 23:27:55 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-29 17:43:31 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-30 04:13:27 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-30 04:13:33 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-30 17:03:54 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-30 21:55:29 
Re: C++ Memory Management Innovation: GC Allocator
Michael Kilburn <crusa  2008-05-06 08:22:59 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-05-06 08:20:19 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-05-07 00:31:29 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-05-07 11:03:52 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-05-08 03:28:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 1:45:48 CDT 2008.