jitcraft/PoolAllocator.h
changeset 56 abb4ae5bc915
parent 55 e073a9fe8e57
child 59 b7d61ddb3c8d
     1.1 --- a/jitcraft/PoolAllocator.h	Sat Jul 07 20:53:10 2012 -0700
     1.2 +++ b/jitcraft/PoolAllocator.h	Sat Jul 07 20:58:13 2012 -0700
     1.3 @@ -22,6 +22,7 @@
     1.4  #include <stddef.h>
     1.5  #include <limits.h>
     1.6  #include "Utility.h"
     1.7 +#include "Vector.h"
     1.8  
     1.9  namespace jc {
    1.10  
    1.11 @@ -107,66 +108,6 @@
    1.12      }
    1.13  };
    1.14  
    1.15 -// Simple vector<> lookalike that can use the pool allocator.
    1.16 -template <typename T>
    1.17 -class PoolList : public PoolObject
    1.18 -{
    1.19 -    T *t_;
    1.20 -    size_t length_;
    1.21 -    size_t max_;
    1.22 -
    1.23 -  private:
    1.24 -    void ensure(PoolAllocator &pool, size_t n) {
    1.25 -        if (max_ - length_ >= n)
    1.26 -            return;
    1.27 -
    1.28 -        size_t newMax = max_;
    1.29 -        if (newMax == 0)
    1.30 -            newMax = 8;
    1.31 -        while (newMax - length_ < n) {
    1.32 -            // Make sure the request does not overflow.
    1.33 -            if (newMax >= INT_MAX)
    1.34 -                throw std::bad_alloc();
    1.35 -            newMax *= 2;
    1.36 -        }
    1.37 -
    1.38 -        // Ensure no overflow.
    1.39 -        if (newMax > UINT_MAX / sizeof(T))
    1.40 -            throw std::bad_alloc();
    1.41 -
    1.42 -        T *new_ = (T *)pool.allocate(newMax * sizeof(T));
    1.43 -        for (size_t i = 0; i < length_; i++)
    1.44 -            new (&new_[i]) T(t_[i]);
    1.45 -
    1.46 -        t_ = new_;
    1.47 -        max_ = newMax;
    1.48 -    }
    1.49 -
    1.50 -  public:
    1.51 -    PoolList()
    1.52 -      : t_(NULL),
    1.53 -        length_(NULL),
    1.54 -        max_(NULL)
    1.55 -    {
    1.56 -    }
    1.57 -
    1.58 -  public:
    1.59 -    void append(PoolAllocator &pool, const T &t) {
    1.60 -        ensure(pool, 1);
    1.61 -        new (&t_[length_]) T(t);
    1.62 -        length_++;
    1.63 -    }
    1.64 -
    1.65 -    size_t length() const {
    1.66 -        return length_;
    1.67 -    }
    1.68 -
    1.69 -    T &at(size_t i) {
    1.70 -        assert(i < length());
    1.71 -        return t_[i];
    1.72 -    }
    1.73 -};
    1.74 -
    1.75  class PoolAllocationPolicy
    1.76  {
    1.77    protected:
    1.78 @@ -178,6 +119,18 @@
    1.79      void free(void *ptr);
    1.80  };
    1.81  
    1.82 +template <typename T>
    1.83 +class PoolList
    1.84 +  : public Vector<T, PoolAllocationPolicy>,
    1.85 +    public PoolObject
    1.86 +{
    1.87 +  public:
    1.88 +    PoolList()
    1.89 +      : Vector(PoolAllocationPolicy())
    1.90 +    {
    1.91 +    }
    1.92 +};
    1.93 +
    1.94  }
    1.95  
    1.96  #endif // _include_jitcraft_pool_allocator_h_