单线程可变大小内存管理器

class MemoryChunk
{
private:
    MemoryChunk* next;//指向下个内存块
    void * mem;//指向可用的内存
    size_t chunkSize;//该内存块的大小
    size_t bytesAlreadyAllocated;//已经分配的字节数
public:
    MemoryChunk(MemoryChunk *nextChunk ,size_t chunkSize);
    ~MemoryChunk();

    inline void* alloc(size_t size);
    inline void free (void* someElement);
    MemoryChunk* nextMemChunk() {  return next; }
    size_t spaceAvailable() {   return chunkSize - bytesAlreadyAllocated;   }
    enum{   DEFUALT_CHUNK_SIZE = 4096  };
};

MemoryChunk::MemoryChunk(MemoryChunk* nextChunk, size_t reqSize)
{
    chunkSize = (reqSize > DEFUALT_CHUNK_SIZE) ? reqSize : DEFUALT_CHUNK_SIZE;
    next = nextChunk;
    bytesAlreadyAllocated=0;
    mem= new char [chunkSize];
}

MemoryChunk::~MemoryChunk()
{
    delete [] (char*)mem;
}

void* MemoryChunk::alloc(size_t requestSize)
{
    void* addr = (void* )((size_t )mem + bytesAlreadyAllocated );
    bytesAlreadyAllocated+=requestSize;
    return addr;
}

inline void MemoryChunk::free(void* someElement ) {}

//内存池类
class ByteMemoryPool
{
private:
    MemoryChunk* listOfMemoryChunks;
    void expandStorage(size_t reqSize);
public:
    ByteMemoryPool(size_t initSize = MemoryChunk::DEFUALT_CHUNK_SIZE);
    ~ByteMemoryPool();
    inline void* alloc(size_t size);
    inline void free(void* someElement);
};

ByteMemoryPool::ByteMemoryPool(size_t initSize)
{
    listOfMemoryChunks=NULL;
    expandStorage(initSize);
}

ByteMemoryPool::~ByteMemoryPool()
{
    MemoryChunk* memChunk = listOfMemoryChunks;
    while(memChunk)
    {
        listOfMemoryChunks = memChunk->nextMemChunk();
        delete memChunk;
        memChunk = listOfMemoryChunks;
    }
}

inline
    void* ByteMemoryPool::alloc(size_t requestSize)
{
    size_t space = listOfMemoryChunks->spaceAvailable();
    if(space<requestSize)
    {
        expandStorage(requestSize);
    }
    return listOfMemoryChunks->alloc(requestSize);
}

inline
    void ByteMemoryPool::free(void* doomed)
{
    listOfMemoryChunks->free(doomed);
}

void ByteMemoryPool::expandStorage(size_t requestSize)
{
    listOfMemoryChunks = new MemoryChunk(listOfMemoryChunks,requestSize);
}

//调用类
class Rational
{
private:
    int n;
    int d;
    static ByteMemoryPool* memPool;
public:
    Rational(int a=0, int b=1):n(a),d(b){}
    void* operator new (size_t size){   return memPool->alloc(size);    }
    void operator delete (void* doomed, size_t size){   memPool->free(doomed);  }
    static void newMemPool(){   memPool = new ByteMemoryPool;   }
    static void deleteMemPool(){    delete memPool; }
};

ByteMemoryPool* Rational::memPool=0;

int main()
{
    Rational * array[1000];
    Rational::newMemPool();
    for(int j=0;j<500;j++)
    {
        for(int i=0;i<1000;i++)
        {
            array[i]=new Rational(i);
        }
        for(int i=0;i<1000;i++)
        {
            delete array[i];
        }
    }
    Rational::deleteMemPool();
    return 0;
}

 

posted @ 2013-09-23 15:49  calabashdad  阅读(304)  评论(0编辑  收藏  举报