一个内存分配器基类

 

   是c++ primer中的例子,做了一些修改,但仍然有内存泄露,再找找办法...。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
#include "stdafx.h"
#include 
<vld.h>
#include 
<iostream>
#include 
<stdexcept>

#define _DEBUG_

/* 
内存分配类,预分配一个对象,并维持一个自由列表
 
*/
template 
<typename T> class CachedObj 
{
public:
    
void* operator new (std::size_t);
    
void operator delete (void*, std::size_t);
    
virtual ~CachedObj();
protected:
    T 
*next;
//private:
public:
    
static void add_to_freelist(T*);
    
static std::allocator<T> alloc_mem;
    
static void releaseMemory();
    
static T *freeList;
    
static const std::size_t chunk;
    
static size_t count;//分配的内存数量
};

using namespace std;

template 
<typename T> void* CachedObj<T>::operator new (size_t sz)
{
    
// new should only be asked to build a T, 
    
// not and object derived from T, 
    
// check that right size is requested
#ifdef _DEBUG_
    std::cout 
<< "CachedObj<T>::operator new" << std::endl;
#endif
    
if (sz != sizeof(T))
        
throw runtime_error("CachedObj: wrong size object in operator new");
    
if (!freeList)
    {
        
// ths list is empty: grab a new chunk of memory
        
// allocates chunk number of objects of type T
        T *array = alloc_mem.allocate(chunk);
        count 
+= chunk;
        
// now set the next pointers in each object in the allocated memory
        for (size_t i = 0; i != chunk; ++i)
        {
            cout
<<"分配内存"<<i<<endl;
            add_to_freelist(
&array[i]);
        }
    }
    T 
*= freeList;
//为了防止派生类中也有next成员,所以在freeList->CachedObj<T>::next用类名进行限制访问next成员
    freeList = freeList->CachedObj<T>::next;
    cout
<<"从freelist取得指针 p="<<p<<endl;
    
return p; // constructor of T will construct the T part of the object
}

template 
<typename T> void CachedObj<T>::operator delete (void *p, size_t)
{
#ifdef _DEBUG_
    std::cout 
<< "CachedObj<T>::operator delete" << std::endl;
#endif
    
if (p != 0)
    {  
// put the "deleted" object back at head of freelist
        add_to_freelist(static_cast<T*>(p));
        cout
<<"增加到自由链表"<<p<<endl;
    }
}

template 
<typename T> void CachedObj<T>::add_to_freelist (T *p)
{
    p
->CachedObj<T>::next = freeList;
    freeList 
= p;
}

template 
<typename T> void CachedObj<T>::releaseMemory ()
{
    
//size_t i = 0;
    
//T* p;

    
////if(count > 0)
    
////{
    
////  alloc_mem.deallocate(freeList, count);
    
////  count = 0;
    
////  freeList = NULL;
    
////}
    //while(i < count)
    
//{
    
//    p = freeList;
    
//    freeList = freeList->CachedObj<T>::next;
    
//    
    
//    //alloc_mem.deallocate(freeList, 1);
    
//    free(p);
    
//}
}

template 
<typename T>  CachedObj<T>::~CachedObj()
{


}

//声明静态的变量和方法
template <typename T> allocator<T> CachedObj<T>::alloc_mem;
template 
<typename T> T* CachedObj<T>::freeList = 0;
template 
<typename T> const size_t CachedObj<T>::chunk = 24;
template 
<typename T> size_t CachedObj<T>::count = 0;

template 
<typename T>
 
class Gtest: public CachedObj< Gtest<T> > 
{
public:
    T t;
    Gtest():t(
0) { }
};

int main () 
{
    Gtest
<int> *= new Gtest<int>;
    delete t;

    Gtest
<float> *= new Gtest<float>;
    delete f;
//可见要对每一个类型进行释放内存
    cout<<Gtest<int>::count<<endl;
    cout
<<Gtest<int>::count<<endl;
    
//cout<<Gtest<float>::count<<endl;
    return 0;
}

 

 

 

 

posted on 2009-11-18 18:35 迈克老狼 阅读( ...) 评论( ...)   编辑 收藏

转载于:https://www.cnblogs.com/mikewolf2009/articles/1605577.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值