Convert StatementList to a PoolList; convert PoolList to Vector.
authorDavid Anderson <dvander@alliedmods.net>
Sat Jul 07 20:58:13 2012 -0700 (2012-07-07)
changeset 56abb4ae5bc915
parent 55 e073a9fe8e57
child 57 8f1e74d35718
Convert StatementList to a PoolList; convert PoolList to Vector.
jitcraft/AST.h
jitcraft/Parser.cpp
jitcraft/PoolAllocator.h
     1.1 --- a/jitcraft/AST.h	Sat Jul 07 20:53:10 2012 -0700
     1.2 +++ b/jitcraft/AST.h	Sat Jul 07 20:58:13 2012 -0700
     1.3 @@ -154,7 +154,7 @@
     1.4  };
     1.5  
     1.6  typedef PoolList<Statement *> StatementList;
     1.7 -typedef Vector<Expression *, PoolAllocationPolicy> ExpressionList;
     1.8 +typedef PoolList<Expression *> ExpressionList;
     1.9  
    1.10  class VariableDeclaration : public Statement
    1.11  {
     2.1 --- a/jitcraft/Parser.cpp	Sat Jul 07 20:53:10 2012 -0700
     2.2 +++ b/jitcraft/Parser.cpp	Sat Jul 07 20:58:13 2012 -0700
     2.3 @@ -168,7 +168,7 @@
     2.4  Expression *
     2.5  Parser::call(Expression *callee)
     2.6  {
     2.7 -    ExpressionList *arguments = new ExpressionList();
     2.8 +    ExpressionList *arguments = new (pool_) ExpressionList();
     2.9  
    2.10      SourcePosition pos = scanner_.begin();
    2.11      expect(TOK_LPAREN);
    2.12 @@ -584,7 +584,7 @@
    2.13  
    2.14      ExpressionList *postDimensions = NULL;
    2.15      if (scanner_.peek() == TOK_LBRACKET) {
    2.16 -        postDimensions = new ExpressionList();
    2.17 +        postDimensions = new (pool_) ExpressionList();
    2.18          while (match(TOK_LBRACKET)) {
    2.19              Expression *dim = expression();
    2.20              if (!dim)
    2.21 @@ -646,7 +646,8 @@
    2.22          Statement *statement = localStatement();
    2.23          if (statement == NULL)
    2.24              return NULL;
    2.25 -        statements->append(pool_, statement);
    2.26 +        if (!statements->append(statement))
    2.27 +            return NULL;
    2.28      }
    2.29  
    2.30      if (!scope) {
    2.31 @@ -939,7 +940,8 @@
    2.32          if (!statement)
    2.33              break;
    2.34  
    2.35 -        statements->append(pool_, statement);
    2.36 +        if (!statements->append(statement))
    2.37 +            return NULL;
    2.38      }
    2.39  
    2.40      return new (pool_) ParseTree(scope_, statements, unbound_);
     3.1 --- a/jitcraft/PoolAllocator.h	Sat Jul 07 20:53:10 2012 -0700
     3.2 +++ b/jitcraft/PoolAllocator.h	Sat Jul 07 20:58:13 2012 -0700
     3.3 @@ -22,6 +22,7 @@
     3.4  #include <stddef.h>
     3.5  #include <limits.h>
     3.6  #include "Utility.h"
     3.7 +#include "Vector.h"
     3.8  
     3.9  namespace jc {
    3.10  
    3.11 @@ -107,66 +108,6 @@
    3.12      }
    3.13  };
    3.14  
    3.15 -// Simple vector<> lookalike that can use the pool allocator.
    3.16 -template <typename T>
    3.17 -class PoolList : public PoolObject
    3.18 -{
    3.19 -    T *t_;
    3.20 -    size_t length_;
    3.21 -    size_t max_;
    3.22 -
    3.23 -  private:
    3.24 -    void ensure(PoolAllocator &pool, size_t n) {
    3.25 -        if (max_ - length_ >= n)
    3.26 -            return;
    3.27 -
    3.28 -        size_t newMax = max_;
    3.29 -        if (newMax == 0)
    3.30 -            newMax = 8;
    3.31 -        while (newMax - length_ < n) {
    3.32 -            // Make sure the request does not overflow.
    3.33 -            if (newMax >= INT_MAX)
    3.34 -                throw std::bad_alloc();
    3.35 -            newMax *= 2;
    3.36 -        }
    3.37 -
    3.38 -        // Ensure no overflow.
    3.39 -        if (newMax > UINT_MAX / sizeof(T))
    3.40 -            throw std::bad_alloc();
    3.41 -
    3.42 -        T *new_ = (T *)pool.allocate(newMax * sizeof(T));
    3.43 -        for (size_t i = 0; i < length_; i++)
    3.44 -            new (&new_[i]) T(t_[i]);
    3.45 -
    3.46 -        t_ = new_;
    3.47 -        max_ = newMax;
    3.48 -    }
    3.49 -
    3.50 -  public:
    3.51 -    PoolList()
    3.52 -      : t_(NULL),
    3.53 -        length_(NULL),
    3.54 -        max_(NULL)
    3.55 -    {
    3.56 -    }
    3.57 -
    3.58 -  public:
    3.59 -    void append(PoolAllocator &pool, const T &t) {
    3.60 -        ensure(pool, 1);
    3.61 -        new (&t_[length_]) T(t);
    3.62 -        length_++;
    3.63 -    }
    3.64 -
    3.65 -    size_t length() const {
    3.66 -        return length_;
    3.67 -    }
    3.68 -
    3.69 -    T &at(size_t i) {
    3.70 -        assert(i < length());
    3.71 -        return t_[i];
    3.72 -    }
    3.73 -};
    3.74 -
    3.75  class PoolAllocationPolicy
    3.76  {
    3.77    protected:
    3.78 @@ -178,6 +119,18 @@
    3.79      void free(void *ptr);
    3.80  };
    3.81  
    3.82 +template <typename T>
    3.83 +class PoolList
    3.84 +  : public Vector<T, PoolAllocationPolicy>,
    3.85 +    public PoolObject
    3.86 +{
    3.87 +  public:
    3.88 +    PoolList()
    3.89 +      : Vector(PoolAllocationPolicy())
    3.90 +    {
    3.91 +    }
    3.92 +};
    3.93 +
    3.94  }
    3.95  
    3.96  #endif // _include_jitcraft_pool_allocator_h_