1 2/* 3 * Copyright 2010 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 11#ifndef GrAllocPool_DEFINED 12#define GrAllocPool_DEFINED 13 14#include "GrNoncopyable.h" 15 16class GrAllocPool : GrNoncopyable { 17public: 18 GrAllocPool(size_t blockSize = 0); 19 ~GrAllocPool(); 20 21 /** 22 * Frees all blocks that have been allocated with alloc(). 23 */ 24 void reset(); 25 26 /** 27 * Returns a block of memory bytes size big. This address must not be 28 * passed to realloc/free/delete or any other function that assumes the 29 * address was allocated by malloc or new (because it hasn't). 30 */ 31 void* alloc(size_t bytes); 32 33 /** 34 * Releases the most recently allocated bytes back to allocpool. 35 */ 36 void release(size_t bytes); 37 38private: 39 struct Block; 40 41 Block* fBlock; 42 size_t fMinBlockSize; 43 44#if GR_DEBUG 45 int fBlocksAllocated; 46 void validate() const; 47#else 48 void validate() const {} 49#endif 50}; 51 52template <typename T> class GrTAllocPool { 53public: 54 GrTAllocPool(int count) : fPool(count * sizeof(T)) {} 55 56 void reset() { fPool.reset(); } 57 T* alloc() { return (T*)fPool.alloc(sizeof(T)); } 58 59private: 60 GrAllocPool fPool; 61}; 62 63#endif 64 65