1 /* 2 Copyright 2010 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 18 #ifndef GrAllocPool_DEFINED 19 #define GrAllocPool_DEFINED 20 21 #include "GrNoncopyable.h" 22 23 class GrAllocPool : GrNoncopyable { 24 public: 25 GrAllocPool(size_t blockSize = 0); 26 ~GrAllocPool(); 27 28 /** 29 * Frees all blocks that have been allocated with alloc(). 30 */ 31 void reset(); 32 33 /** 34 * Returns a block of memory bytes size big. This address must not be 35 * passed to realloc/free/delete or any other function that assumes the 36 * address was allocated by malloc or new (because it hasn't). 37 */ 38 void* alloc(size_t bytes); 39 40 /** 41 * Releases the most recently allocated bytes back to allocpool. 42 */ 43 void release(size_t bytes); 44 45 private: 46 struct Block; 47 48 Block* fBlock; 49 size_t fMinBlockSize; 50 51 #if GR_DEBUG 52 int fBlocksAllocated; 53 void validate() const; 54 #else validate()55 void validate() const {} 56 #endif 57 }; 58 59 template <typename T> class GrTAllocPool { 60 public: GrTAllocPool(int count)61 GrTAllocPool(int count) : fPool(count * sizeof(T)) {} 62 reset()63 void reset() { fPool.reset(); } alloc()64 T* alloc() { return (T*)fPool.alloc(sizeof(T)); } 65 66 private: 67 GrAllocPool fPool; 68 }; 69 70 #endif 71 72