michael@0: /* michael@0: * Copyright 2010 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrAllocPool_DEFINED michael@0: #define GrAllocPool_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: class GrAllocPool : public SkNoncopyable { michael@0: public: michael@0: GrAllocPool(size_t blockSize = 0); michael@0: ~GrAllocPool(); michael@0: michael@0: /** michael@0: * Frees all blocks that have been allocated with alloc(). michael@0: */ michael@0: void reset(); michael@0: michael@0: /** michael@0: * Returns a block of memory bytes size big. This address must not be michael@0: * passed to realloc/free/delete or any other function that assumes the michael@0: * address was allocated by malloc or new (because it hasn't). michael@0: */ michael@0: void* alloc(size_t bytes); michael@0: michael@0: /** michael@0: * Releases the most recently allocated bytes back to allocpool. michael@0: */ michael@0: void release(size_t bytes); michael@0: michael@0: private: michael@0: struct Block; michael@0: michael@0: Block* fBlock; michael@0: size_t fMinBlockSize; michael@0: michael@0: #ifdef SK_DEBUG michael@0: int fBlocksAllocated; michael@0: void validate() const; michael@0: #else michael@0: void validate() const {} michael@0: #endif michael@0: }; michael@0: michael@0: template class GrTAllocPool { michael@0: public: michael@0: GrTAllocPool(int count) : fPool(count * sizeof(T)) {} michael@0: michael@0: void reset() { fPool.reset(); } michael@0: T* alloc() { return (T*)fPool.alloc(sizeof(T)); } michael@0: michael@0: private: michael@0: GrAllocPool fPool; michael@0: }; michael@0: michael@0: #endif