상세 컨텐츠

본문 제목

메모리 풀 예제1

본문

반응형
 

 

 

 

 

=================================

=================================

=================================

 

 

 

 

 
 

출처

http://www.myevan.net/phpBB/viewtopic.php?t=2298

#pragma once

#define null 0

template<typename T, int MAXOBJECT=100> class MemoryPool
{   
protected:
        vector<T*> vectorData;
        vector<T*> vectorFree;

        byte * buffer;

        unsigned int usedObject;
        unsigned int maxObject;

        static T* newInstance()
        {
                return (T*)::operator new(sizeof(T));
        }

        static void deleteInstance(T* object)
        {
                ::operator delete(object);          
        }

public:
        MemoryPool()
        {
                usedObject = 0;
                maxObject = MAXOBJECT;
                buffer = null;

                if (maxObject>0)
                        CreatePool();
        }

        ~MemoryPool()
        {
                if (buffer)
                {
                        delete [] buffer;
                        buffer = NULL;

                        usedObject = 0;
                }

                for_each(vectorData.begin(), vectorData.end(), deleteInstance); // 메모리가 삭제된 시점에서 소멸자 호출? 음...

                vectorData.clear();
                vectorFree.clear();         
        }

        void CreatePool()
        {
                if (maxObject > 0)
                {
                        if (buffer)
                        delete [] buffer;

                        buffer = new byte[sizeof(T)*maxObject];

                        vectorData.reserve(maxObject);
                        vectorFree.reserve(maxObject);
                }           
        }

        T* Alloc()
        {
                T* newObject = null;

                if (vectorFree.empty())
                {
                        if (usedObject < maxObject)
                        {
                                newObject = reinterpret_cast<T*>(&buffer[usedObject++]);
                        }
                        else
                        {
                                newObject = newInstance();                
                                vectorData.push_back(newObject);
                        }
                        return newObject;
                }
                else
                {
                        newObject = vectorFree.back();
                        vectorFree.pop_back();
                        return newObject;
                }
        }

        void Free(T* freeObject)
        {           
                vectorFree.push_back(freeObject);
        }

        void FreeAll()
        {
                vectorFree = vectorData;
                usedObject = 0;
        }

        unsigned long GetCount()
        {
                return vectorData.size() + usedObject;
        }
};

template<typename T, int MAXOBJECT=0> class MemoryPooledObject
{
protected:
        static MemoryPool<T, MAXOBJECT> memoryPool;

public:
        MemoryPooledObject()
        {
        }

        virtual ~MemoryPooledObject()
        {
        }

        void * operator new(unsigned int size)
        {
                return memoryPool.Alloc();
        }

        void operator delete(void* freeObject)
        {
                memoryPool.Free((T*)freeObject);
        }    
};

template<typename T, int MAXOBJECT> 
MemoryPool<T,MAXOBJECT> MemoryPooledObject<T,MAXOBJECT>::memoryPool;

사용예*

// 구조체나 기본 자료형의 경우 (생성자, 소멸자 호출 필요 없는 경우)
void Test1()
{
MemoryPool<STemp> pool_kTemp;
STemp* pkTemp = pool_kTemp.Alloc();
pool_kTemp.Free(pkTemp);
}

// 클래스의 경우 상속.
class CData : public MemoryPooledObject<CData, 10> 
{
public:
    CData(const char* c_szTitle) 
    {
        puts("create data");
    }
    virtual ~CData() 
    {
        puts("destroy data");
    } 
};
void Test2()
{
    CData* pkData = new CData("-_-");
        
    delete pkData;
}



 

 

=================================

=================================

=================================

 

 

 

 





 

반응형


관련글 더보기

댓글 영역