class CMemoryManager
{
public:
    CMemoryManager()
    {
        memory = NULL;
        size = 0;
    }
    CMemoryManager(size_t size)
    {
        this -> size = size;
        memory = VirtualAlloc(NULL,size,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
        if ( memory == NULL )
        {
            throw Exception("Memory malloc failed!");
        }
    }

    CMemoryManager(const CMemoryManager &another)
    {
        if ( this -> memory != NULL )
        {
            destroyMemory();
        }
        this -> size = another.size;
        this -> memory = VirtualAlloc(NULL,this -> size,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
        if ( this -> memory == NULL )
        {
            throw Exception("Memory malloc failed!");
        }
        CopyMemory(this -> memory,another.getMemory(),this->size);
    }


    PVOID applyMemory(size_t size)
    {
        if ( memory != NULL )
        {
            VirtualFree(memory,this -> size,MEM_RELEASE);
            memory = NULL;
            this -> size = 0;
        }
        this -> size = size;
        memory = VirtualAlloc(NULL,size,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
        if ( memory == NULL )
        {
            throw Exception("Memory malloc failed!");
        }
        return this -> memory ;
    }

    void destroyMemory()
    {
        if ( memory != NULL )
        {
            VirtualFree(memory,size,MEM_RELEASE);
            memory = NULL;
            size = 0;
        }
    }

    PVOID getMemory()
    {
        return this -> memory;
    }

    size_t getMemorySize()
    {
        return this -> size;
    }

    ~CMemoryManager()
    {
        destroyMemory();
    }
private:
    PVOID memory;
    size_t size;
};