template
<
int
ALIGN
>
class
MemPage
{
public:
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline MemPage( UINT _size, MemPage *oldPage )
{
UINT asize = MAX( _size, MEMORY_PAGE_SIZE );
head = _mm_malloc( asize, ALIGN );
page = (BYTE *) head;
_next = oldPage;
size = asize;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline MemPage( void *ptr, UINT _size, MemPage *oldPage )
{
head = NULL;
page = (BYTE *) ptr;
_next = oldPage;
size = _size;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline ~MemPage()
{
if( head )
_mm_free( head );
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline UINT available()
{
return size;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline void *alloc( UINT _size )
{
void *ptr = page;
page += _size;
size -= _size;
return ptr;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline MemPage *next()
{
return _next;
}
private:
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
union
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
struct
{
void *head;
BYTE *page;
MemPage *_next;
UINT size;
};
__m128 aligned;
};
}
;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
template
<
typename T,
int
ALIGN
>
class
MemPool
{
public:
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline MemPool()
{
thePage = NULL;
theChunk = NULL;
allocated = 0;
inited = 0;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline void init()
{
thePage = NULL;
theChunk = NULL;
allocated = 0;
inited ++;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline void destory()
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( inited == 1 )
{
MemPage< ALIGN > *cPage;
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while( ( cPage = thePage ) != NULL )
{
thePage = cPage->next();
delete cPage;
}
}
inited --;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline void *alloc( UINT size )
{
void *ptr;
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( theChunk == NULL || size > theChunk->available() )
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( thePage == NULL || size > thePage->available() )
{
if( !inited )
init();
thePage = new MemPage< ALIGN > ( size, thePage );
}
ptr = thePage->alloc( size );
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
MemPage< ALIGN > *cPage = theChunk;
ptr = cPage->alloc( size );
theChunk = cPage->next();
}
allocated ++;
return ptr;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline void dealloc( void * ptr )
{
allocated --;
if( allocated == 0 )
destory();
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inline void dealloc( void * ptr, UINT size )
{
if( size > sizeof(MemPage< ALIGN >) )
theChunk = new (ptr) MemPage< ALIGN > ( (BYTE *)ptr + sizeof(MemPage< ALIGN >),
size - sizeof(MemPage< ALIGN >), theChunk );
allocated --;
if( allocated == 0 )
destory();
}
private:
MemPage< ALIGN > *thePage, *theChunk;
UINT inited, allocated;
}
;
用我的渲染器测试了一下,速度还是很快的。。。
转载于:https://www.cnblogs.com/len3d/archive/2007/07/12/815183.html