只能一次申请一个对象,没有线程锁。

template <typename T, size_t PAGE>
class pool_allocator
{
public:
    typedef pool_allocator    this_type;
    typedef T                 value_type;
    typedef value_type*       pointer;
    typedef const value_type* const_pointer;
    typedef value_type&       reference;
    typedef const value_type& const_reference;
    typedef std::size_t       size_type;
    typedef std::ptrdiff_t    difference_type;
 
    const static size_t PAGE_SIZE = PAGE;
private:
    struct node{
        node* next;
        //byte_t value[sizeof(value_type)];
        value_type value;
    };
    node* entry;
    static std::vector<void*> poolbuf;
public:
    pool_allocator() : entry()
    {
    }
 
    pool_allocator(const this_type&) : entry()
    {
    }
 
    ~pool_allocator()
    {
        this->dispose();
    }
 
    this_type& operator=(const this_type&)
    {
        return *this;
    }
 
    bool operator==(const this_type&)
    {
        return true;
    }
 
    bool operator!=(const this_type&)
    {
        return false;
    }
 
    pointer allocate(size_type size = 1, const_pointer = 0)
    {
        assert(size == 1);
        if(!entry){
            node* buf = allocate_buffer();
            entry = buf;
        }
        node* n = entry;
        entry = entry->next;
        return this->address(n);
    }
 
    pointer allocate(size_type n, size_type alignment, const_pointer = 0)
    {
        assert(false);
        return null;
    }
 
    void deallocate(pointer p, size_type = 0)
    {
        //node * n = reinterpret_cast<node*>(p);
        node * n = reinterpret_cast<node*>(reinterpret_cast<byte_t*>(p) - sizeof(node*));
        n->next = entry;
        entry = n;
    }
 
    pointer reallocate(pointer ptr, size_type n)
    {
        assert(false);
        return null;
    }
 
    void dispose()
    {
        for(size_type i = 0; i < poolbuf.size(); ++i)
        {
            deallocate_buffer(poolbuf[i]);
        }
        poolbuf.clear();
        entry = null;
    }
 
    size_type max_size()const
    {
        return static_cast<size_type>(-1)/sizeof(T);
    }
 
    size_type size()const
    {
        return poolbuf.size() * PAGE_SIZE;
    }
 
    size_type free_size()const
    {
        size_type n = 0;
        node* p = entry;
        while(p){
            ++n;
            p = p->next;
        }
        return n;
    }
 
    void construct(pointer p, const value_type& x)
    {
        //new(p) value_type(x);
    }
 
    void construct(pointer p)
    {
        //new (p) value_type();
    }
 
    void destroy(pointer p)
    {
        //p->~T();
    }
private:
    node* allocate_buffer()
    {
        node* buf = new node[PAGE_SIZE];
        //node* buf = static_cast<node*>(malloc(PAGE_SIZE * sizeof(node)));
 
        for(size_type i=0; i<PAGE_SIZE; ++i){
            buf[i].next = buf + i + 1;
        }
        buf[PAGE_SIZE - 1].next = null;
        poolbuf.push_back(buf);
        return buf;
    }
 
    void deallocate_buffer(void* &buf)
    {
        node* p = static_cast<node*>(buf);
        delete []p;
        //free(buf);
        buf = null;
    }
 
    pointer address(node* n)
    {
        //return reinterpret_cast<pointer>(n->value);
        return &n->value;
    }
};
 
template<typename T, size_t P>
std::vector<void*> pool_allocator<T, P>::poolbuf = std::vector<void*>();