/***************************************************************************\ * * File: AllocPool.inl * * History: * 1/28/2000: JStall: Created * * Copyright (C) 2000 by Microsoft Corporation. All rights reserved. * \***************************************************************************/ #if !defined(BASE__AllocPool_inl__INCLUDED) #define BASE__AllocPool_inl__INCLUDED #pragma once /***************************************************************************\ ***************************************************************************** * * class AllocListNL * ***************************************************************************** \***************************************************************************/ //------------------------------------------------------------------------------ template inline AllocPoolNL::AllocPoolNL() { m_nTop = 0; } //------------------------------------------------------------------------------ template inline AllocPoolNL::~AllocPoolNL() { Destroy(); } //------------------------------------------------------------------------------ template inline void AllocPoolNL::Destroy() { if (m_nTop > 0) { ContextMultiFree(heap::GetHeap(), m_nTop, (void **) m_rgItems, sizeof(T)); m_nTop = 0; } } //------------------------------------------------------------------------------ template inline T * AllocPoolNL::New() { T * ptNew; if (m_nTop <= 0) { // // Not enough items in the pool to hand any new one out, so we need to // allocate more. These will NOT be zero-initialized by the memory // allocator. // ContextMultiAlloc(heap::GetHeap(), &m_nTop, (void **) m_rgItems, cbBlock, sizeof(T)); if (m_nTop == 0) { ptNew = NULL; goto exit; } } // // There is an item in the pool, but we need to "scrub" it before handing // it out. // ptNew = m_rgItems[--m_nTop]; ZeroMemory(ptNew, sizeof(T)); placement_new(ptNew, T); exit: return ptNew; } //------------------------------------------------------------------------------ template inline void AllocPoolNL::Delete(T * pvMem) { if (pvMem == NULL) { return; } placement_delete(pvMem, T); m_rgItems[m_nTop++] = pvMem; if (m_nTop >= cbBlock * 2) { ContextMultiFree(heap::GetHeap(), cbBlock, (void **) &m_rgItems[cbBlock], sizeof(T)); m_nTop -= cbBlock; } } //------------------------------------------------------------------------------ template inline BOOL AllocPoolNL::IsEmpty() const { return m_nTop == 0; } /***************************************************************************\ ***************************************************************************** * * class AllocList * ***************************************************************************** \***************************************************************************/ //------------------------------------------------------------------------------ template inline T * AllocPool::New() { m_lock.Enter(); T * pNew = AllocPoolNL::New(); m_lock.Leave(); return pNew; } //------------------------------------------------------------------------------ template inline void AllocPool::Delete(T * pvMem) { m_lock.Enter(); AllocPoolNL::Delete(pvMem); m_lock.Leave(); } #endif // BASE__AllocPool_inl__INCLUDED