当前位置:   article > 正文

c++---构建内存管理器_c++编写内存管理器

c++编写内存管理器
#include<iostream>
#include<sys/types.h>
#include<unistd.h>
#include<time.h>

using namespace std;


class IMemoryManager
{
public:
virtual void* allocate(size_t) = 0;
virtual void free(void*deleted) = 0;
};


class MemoryManager:public IMemoryManager
{
struct FreeStore
{
FreeStore *next;
};
void expandPoolSize();
void cleanUp();
FreeStore* freeStoreHead;


public:
MemoryManager()
{
freeStoreHead = 0;
expandPoolSize();
}
virtual ~MemoryManager()
{
cleanUp();
}
virtual void* allocate(size_t);
virtual void free(void* deleted);
};


MemoryManager gMemoryManager;
class Complex
{
public:
Complex(double a,double b):r(a),c(b){}
inline void* operator new(size_t);
inline void operator delete(void*deleted);
private:
double r;
double c;
};


inline void* MemoryManager::allocate(size_t size)
{
if(0 == freeStoreHead)
expandPoolSize();
FreeStore* head = freeStoreHead;
freeStoreHead = head->next;
return head;
}


inline void MemoryManager ::free(void* deleted)
{
FreeStore* head = static_cast<FreeStore*>(deleted);
head->next = freeStoreHead;
freeStoreHead = head;
}


void* Complex::operator new(size_t size)
{
return gMemoryManager.allocate(size);
}


void Complex::operator delete(void* pointerToDelete)
{
gMemoryManager.free(pointerToDelete);
}


#define POOLSIZE 32


void MemoryManager::expandPoolSize()
{
size_t size = (sizeof(Complex) > sizeof(FreeStore*))? sizeof(Complex):sizeof(FreeStore*);


FreeStore* head = reinterpret_cast<FreeStore*>(new char[size]);
freeStoreHead = head;


for(int i = 0;i<POOLSIZE;i++)
{
head->next = reinterpret_cast<FreeStore*>(new char[size]);
head = head->next;
}
head->next = 0;
}


void MemoryManager::cleanUp()
{
FreeStore* nextPtr = freeStoreHead;
for(;nextPtr;nextPtr = freeStoreHead)
{
freeStoreHead = freeStoreHead->next;
delete[]nextPtr;
}
}


int main()
{
time_t start,end;
start = time(NULL);
Complex* array[1000];
for(int i =0;i < 50000;i++ )
{
for(int j = 0;j < 1000;j++)
{
array[j] = new Complex(i,j);
}
for(int j = 0;j < 1000;j++)
{
delete array[j];
}
}
end = time(NULL);


cout<<"time "<<difftime(end,start)<<endl;


return 0;

}



对比运行时间


#include<iostream>
#include<unistd.h>
#include<time.h>


using namespace std;


class Complex
{
        public:
                Complex(double a,double b):r(a),c(b){}
        private:
                double r;
                double c;
};


int main()
{
        time_t start,end;
        start = time(NULL);
        Complex* array[1000];
        for(int i =0;i < 50000;i++ )
        {
                for(int j = 0;j < 1000;j++)
                {
                        array[j] = new Complex(i,j);
                }
                for(int j = 0;j < 1000;j++)
                {
                        delete array[j];
                }
        }
        end = time(NULL);

        cout<<"time "<<difftime(end,start)<<endl;

        return 0;
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/1015309
推荐阅读
相关标签
  

闽ICP备14008679号