src/PoolAllocator.h
changeset 195 c1ba166f3fc4
parent 91 5f4402180204
child 263 ba85a47ee414
     1.1 --- a/src/PoolAllocator.h	Mon Sep 17 00:10:01 2012 -0700
     1.2 +++ b/src/PoolAllocator.h	Sat Nov 17 18:41:01 2012 -0800
     1.3 @@ -58,7 +58,7 @@
     1.4      PoolAllocator();
     1.5      ~PoolAllocator();
     1.6  
     1.7 -    void *allocate(size_t bytes) {
     1.8 +    void *rawAllocate(size_t bytes) {
     1.9          // Guarantee malloc alignment.
    1.10          size_t actualBytes = Align(bytes, kMallocAlignment);
    1.11          if (!last_ || (size_t(last_->end - last_->ptr) < actualBytes))
    1.12 @@ -68,6 +68,15 @@
    1.13          return ptr;
    1.14      }
    1.15  
    1.16 +    template <typename T>
    1.17 +    T *alloc(size_t count = 1) {
    1.18 +        void *ptr = rawAllocate(count * sizeof(T));
    1.19 +        if (!ptr)
    1.20 +            return NULL;
    1.21 +
    1.22 +        return reinterpret_cast<T *>(ptr);
    1.23 +    }
    1.24 +
    1.25      char *enter();
    1.26      void leave(char *position);
    1.27  };
    1.28 @@ -93,10 +102,10 @@
    1.29  {
    1.30    public:
    1.31      void *operator new(size_t size, PoolAllocator &pool) {
    1.32 -        return pool.allocate(size);
    1.33 +        return pool.rawAllocate(size);
    1.34      }
    1.35      void *operator new [](size_t size, PoolAllocator &pool) {
    1.36 -        return pool.allocate(size);
    1.37 +        return pool.rawAllocate(size);
    1.38      }
    1.39      
    1.40      // Using delete on pool-allocated objects is illegal.