关闭

新简单内存池

379人阅读 评论(0) 收藏 举报

#ifndef __MEMORY_POOL_H__
#define __MEMORY_POOL_H__

#include <iostream>
using namespace std;

#ifndef   nullptr
#define   nullptr   (NULL)
#endif

class Memory_Pool
{
private:
    struct Storage
    {
        size_t    index;
        Storage * next;
    };

private:
    Memory_Pool();
    ~Memory_Pool();

private:
    void adjust(size_t & size);

public:
    void * malloc(size_t size);
    void free(void * ptr);

public:
    static Memory_Pool memory_pool;

private:
    enum { POOL_COUNT = 20 };
    Storage * m_pool[POOL_COUNT];
};

#endif
#include <cassert>
#include <cstdlib>
#include "memorypool.h"

Memory_Pool::Memory_Pool()
{
    for (int i = 0; i < POOL_COUNT; ++i) {
        m_pool[i] = nullptr;
    }
}

Memory_Pool::~Memory_Pool()
{
   for (int i = 0; i < POOL_COUNT; ++i) {
        Storage * head = m_pool[i];
        while (head != nullptr) {
            Storage * next = head->next;
            ::free(head);
            head = next;
        }
    }
}

void Memory_Pool::adjust(size_t & size)
{
    size += sizeof(Storage);
    size += 7;
    size >>= 3;
    size <<= 3;
}

void * Memory_Pool::malloc(size_t size)
{
    adjust(size);

    size_t index = ((size >> 3) - 1);
    assert(index >= 0);

    Storage * pstorage = nullptr;
    if (index < POOL_COUNT && nullptr != m_pool[index]) {
        pstorage = m_pool[index];
        m_pool[index] = pstorage->next;
        pstorage->next = nullptr;
        assert(pstorage->index == index);
    }
    else {
        pstorage = (Storage *)::malloc(size); // ::operator new/[](size);
        if (nullptr == pstorage) {
            return nullptr;
        }
        pstorage->index = index;
        pstorage->next = nullptr;
    }

    return (void *)((char *)pstorage + sizeof(Storage));
}

void Memory_Pool::free(void * ptr)
{
    if (nullptr == ptr) {
        return;
    }

    struct Storage * pstorage = 
                     (Storage *)((char *)ptr - sizeof(struct Storage));
    assert(pstorage->index >= 0);
    assert(nullptr == pstorage->next);

    if (pstorage->index >= POOL_COUNT) {
        ::free(pstorage); // ::operator delete/[](pstorage);
    }
    else {
        pstorage->next = m_pool[pstorage->index];
        m_pool[pstorage->index] = pstorage;
    }
}
#include <iostream>
#include <string>
#include "memorypool.h"
using namespace std;

Memory_Pool Memory_Pool::memory_pool;

void * Malloc(size_t size)
{
    char * ptr = (char *)Memory_Pool::memory_pool.malloc(size);
    cout << "new(" << size << "): " << (int)ptr << endl;
    return ptr;
}

void Free(void * ptr)
{
    cout << "delete(" << (int)ptr << ")" << endl;
    Memory_Pool::memory_pool.free(ptr);
}

class NewDelBase
{
public:
    static void * operator new(size_t size)
    {
        return Malloc(size);
    }

    static void operator delete(void * ptr)
    {
        Free(ptr);
    }

    static void * operator new[] (size_t size)
    {
        return Malloc(size);
    }

    static void operator delete[] (void * ptr)
    {
        Free(ptr);
    }
};

class Age
{
public:
    Age(int age=0) : m_age(age)
    {
        cout << "Age()" << endl;
    }

    ~Age()
    {
        cout << "~Age()" << endl;
    }

private:
    int m_age;
};

class Person : public NewDelBase
{
public:
    Person(int age = 0, const char * name = "") : m_age(age), m_name(name)
    {
        cout << "Person()" << endl;
    }

    ~Person()
    {
        cout << "~Person()" << endl;
    }

    void test()
    {
        int    * pi   = new int;
        double * pd   = new double;
        Age    * page = new Age(24);

        *pi = 1;
        *pd = 2.0;

        delete pi;
        delete pd;
        delete page;
    }

private:
    Age    m_age;
    string m_name;
};

void test1()
{
    cout << "test1" << endl;
    int    * pi   = new int;
    double * pd   = new double;
    Age    * page = new Age(24);

    *pi = 1;
    *pd = 2.0;

    delete pi;
    delete pd;
    delete page;
}

void test2()
{
    cout << "test2" << endl;
    int    * pi   = (int *)Malloc(sizeof(int));
    double * pd   = (double *)Malloc(sizeof(double));
    char   * pbuf = (char *)Malloc(sizeof(Age));
    Age    * page = nullptr;

    *pi   = 1;
    *pd   = 2.0;
    page = new (pbuf) Age(24);

    page->~Age();

    Free(pi);
    Free(pd);
    Free(pbuf);
}

int main()
{
    Person * person = new Person(24, "AdwardInk");
    person->test();
    delete person;

    test1();
    test2();

    Person * people = new Person[3];
    delete [] people;

    return 0;
}
objects=test.o memorypool.o
test:$(objects)
	g++ -o test $(objects)

test.o:test.cpp memorypool.h
	g++ -c test.cpp
memorypool.o:memorypool.cpp memorypool.h
	g++ -c memorypool.cpp

rebuild:clean test

clean:
	-rm test $(objects)

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:155170次
    • 积分:3258
    • 等级:
    • 排名:第10419名
    • 原创:167篇
    • 转载:10篇
    • 译文:0篇
    • 评论:41条
    文章分类
    最新评论