我写的一个简单的内存管理器

ExpandedBlockStart.gif ContractedBlock.gif template <   int  ALIGN  >   class  MemPage  dot.gif {
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif    inline MemPage( UINT _size, MemPage 
*oldPage ) dot.gif{
InBlock.gif        UINT    asize 
= MAX( _size, MEMORY_PAGE_SIZE );
InBlock.gif        head 
= _mm_malloc( asize, ALIGN );
InBlock.gif        page 
= (BYTE *) head;
InBlock.gif        _next 
= oldPage;
InBlock.gif        size 
= asize;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline MemPage( 
void *ptr, UINT _size, MemPage *oldPage ) dot.gif{
InBlock.gif        head 
= NULL;
InBlock.gif        page 
= (BYTE *) ptr;
InBlock.gif        _next 
= oldPage;
InBlock.gif        size 
= _size;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline 
~MemPage() dot.gif{
InBlock.gif        
if( head )
InBlock.gif            _mm_free( head );
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline UINT    available() 
dot.gif{
InBlock.gif        
return size;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline 
void    *alloc( UINT _size ) dot.gif{
InBlock.gif        
void *ptr = page;
InBlock.gif        page 
+= _size;
InBlock.gif        size 
-= _size;
InBlock.gif        
return ptr;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline MemPage    
*next() dot.gif{
InBlock.gif        
return _next;
ExpandedSubBlockEnd.gif    }

InBlock.gif
private:
ExpandedSubBlockStart.gifContractedSubBlock.gif    union 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
struct dot.gif{
InBlock.gif            
void        *head;
InBlock.gif            BYTE        
*page;
InBlock.gif            MemPage        
*_next;
InBlock.gif            UINT        size;
ExpandedSubBlockEnd.gif        }
;
InBlock.gif        __m128            aligned;
ExpandedSubBlockEnd.gif    }
;
ExpandedBlockEnd.gif}
;
None.gif
ExpandedBlockStart.gifContractedBlock.giftemplate 
<  typename T,  int  ALIGN  >   class  MemPool  dot.gif {
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif    inline MemPool() 
dot.gif{
InBlock.gif        thePage 
= NULL;
InBlock.gif        theChunk 
= NULL;
InBlock.gif        allocated 
= 0;
InBlock.gif        inited 
= 0;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline 
void init() dot.gif{
InBlock.gif        thePage 
= NULL;
InBlock.gif        theChunk 
= NULL;
InBlock.gif        allocated 
= 0;
InBlock.gif        inited 
++;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline 
void destory() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if( inited == 1 ) dot.gif{
InBlock.gif            MemPage
< ALIGN >    *cPage;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
while( ( cPage = thePage ) != NULL ) dot.gif{
InBlock.gif                thePage 
= cPage->next();
InBlock.gif                delete cPage;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        inited 
--;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline 
void    *alloc( UINT size ) dot.gif{
InBlock.gif        
void    *ptr;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if( theChunk == NULL || size > theChunk->available() ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( thePage == NULL || size > thePage->available() ) dot.gif{
InBlock.gif                
if!inited )
InBlock.gif                    init();
InBlock.gif                thePage 
= new MemPage< ALIGN > ( size, thePage );
ExpandedSubBlockEnd.gif            }

InBlock.gif            ptr 
= thePage->alloc( size );
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
InBlock.gif            MemPage
< ALIGN >    *cPage = theChunk;
InBlock.gif            ptr 
= cPage->alloc( size );
InBlock.gif            theChunk 
= cPage->next();
ExpandedSubBlockEnd.gif        }

InBlock.gif        allocated 
++;
InBlock.gif        
return ptr;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline 
void    dealloc( void * ptr ) dot.gif{
InBlock.gif        allocated 
--;
InBlock.gif        
if( allocated == 0 )
InBlock.gif            destory();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    inline 
void    dealloc( void * ptr, UINT size ) dot.gif{
InBlock.gif        
if( size > sizeof(MemPage< ALIGN >) )
InBlock.gif            theChunk 
= new (ptr) MemPage< ALIGN > ( (BYTE *)ptr + sizeof(MemPage< ALIGN >),
InBlock.gif                                                    size 
- sizeof(MemPage< ALIGN >), theChunk );
InBlock.gif        allocated 
--;
InBlock.gif        
if( allocated == 0 )
InBlock.gif            destory();
ExpandedSubBlockEnd.gif    }

InBlock.gif
private:
InBlock.gif    MemPage
< ALIGN >    *thePage, *theChunk;
InBlock.gif    UINT                inited, allocated;
ExpandedBlockEnd.gif}
;



用我的渲染器测试了一下,速度还是很快的。。。

posted on 2007-07-12 10:53 Len3d 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/len3d/archive/2007/07/12/815183.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值